Skip to content

Commit

Permalink
Merge pull request #303 from projecthydra/relation_select
Browse files Browse the repository at this point in the history
Added #select to Relation
  • Loading branch information
dchandekstark committed Jan 7, 2014
2 parents 29217c2 + 1d57e7c commit 5eddd93
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/active_fedora/associations/collection_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def ids_writer(ids)
#send("#{reflection.name}=", ids.collect { |id| reflection.klass.find(id)})
end

def reset
reset_target!
@loaded = false
end

def first(*args)
first_or_last(:first, *args)
end
Expand Down Expand Up @@ -111,11 +116,6 @@ def to_ary
end
alias_method :to_a, :to_ary

def reset
reset_target!
@loaded = false
end

def build(attributes = {}, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| build(attr, &block) }
Expand Down
4 changes: 4 additions & 0 deletions lib/active_fedora/relation/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,9 @@ def extending!(*modules, &block) # :nodoc:

self
end

def select
to_a.select { |*block_args| yield(*block_args) }
end
end
end
42 changes: 42 additions & 0 deletions spec/integration/relation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'

describe ActiveFedora::Base do
before :all do
class Library < ActiveFedora::Base
has_many :books, property: :has_member
end
class Book < ActiveFedora::Base; end
end
after :all do
Library.delete_all
Object.send(:remove_const, :Library)
Object.send(:remove_const, :Book)
end

subject {Library.all}
its(:class) {should eq ActiveFedora::Relation }

describe "#find" do
before do
Library.create
@library = Library.create
end
it "should find one of them" do
expect(subject.find(@library.id)).to eq @library
end
it "should find with a block" do
expect(subject.find { |l| l.id == @library.id}).to eq @library
end
end

describe "#select" do
before do
Library.create
@library = Library.create
end
it "should find with a block" do
expect(subject.select { |l| l.id == @library.id}).to eq [@library]
end
end
end

0 comments on commit 5eddd93

Please sign in to comment.