Skip to content

Commit

Permalink
Return relation for .limit, fixes #352
Browse files Browse the repository at this point in the history
Adds the .scope method to CollectionProxy so that you can chain
relations such as .limit to a CollectionProxy.
  • Loading branch information
awead authored and carolyncole committed Jun 24, 2015
1 parent e79b12b commit 9cf316b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/active_fedora/associations/collection_proxy.rb
Expand Up @@ -842,6 +842,12 @@ def proxy_association
@association
end

# @return [Relation] object for the records in this association
def scope
@association.scope
end
alias spawn scope

def to_ary
load_target.dup
end
Expand Down
14 changes: 11 additions & 3 deletions spec/integration/relation_spec.rb
Expand Up @@ -3,9 +3,11 @@
describe ActiveFedora::Base do
before :all do
class Library < ActiveFedora::Base
has_many :books, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf
has_many :books
end
class Book < ActiveFedora::Base
belongs_to :library, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf
end
class Book < ActiveFedora::Base; end
end

after :all do
Expand Down Expand Up @@ -60,6 +62,12 @@ class Book < ActiveFedora::Base; end
expect{ subject.sort! }.to raise_error NoMethodError
end
end

context "when limit is applied" do
subject { Library.create books: [Book.create, Book.create] }
it "limits the number of books" do
expect(subject.books.limit(1).size).to eq 1
end
end
end
end

28 changes: 28 additions & 0 deletions spec/unit/collection_proxy_spec.rb
@@ -0,0 +1,28 @@
require 'spec_helper'

describe ActiveFedora::Associations::CollectionProxy do

before do
class Book < ActiveFedora::Base
end
class Page < ActiveFedora::Base
end
end

after do
Object.send(:remove_const, :Page)
Object.send(:remove_const, :Book)
end

describe "#spawn" do

let(:reflection) { Book.create_reflection(:has_many, :pages, { predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isMemberOfCollection }, Book) }
let(:association) { ActiveFedora::Associations::HasManyAssociation.new(Book.new, reflection ) }
let(:proxy) { described_class.new(association) }

subject { proxy.spawn }
it { is_expected.to be_instance_of ActiveFedora::Relation }

end

end

0 comments on commit 9cf316b

Please sign in to comment.