Skip to content

Commit

Permalink
Many relations need to implement #scoped. Fixes #1028.
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Aug 3, 2011
1 parent 1a73ef2 commit 8623b94
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/mongoid/relations/many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ def respond_to?(name, include_private = false)
[].respond_to?(name, include_private) || super
end

# This is public access to the relation's criteria.
#
# @example Get the scoped relation.
# relation.scoped
#
# @return [ Criteria ] The scoped criteria.
#
# @since 2.1.0
def scoped
criteria
end

# Gets the document as a serializable hash, used by ActiveModel's JSON and
# XML serializers. This override is just to be able to pass the :include
# and :except options to get associations in the hash.
Expand Down
19 changes: 19 additions & 0 deletions spec/functional/mongoid/relations/embedded/many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,25 @@
end
end

describe "#scoped" do

let(:person) do
Person.new
end

let(:scoped) do
person.addresses.scoped
end

it "returns the relation criteria" do
scoped.should be_a(Mongoid::Criteria)
end

it "returns with an empty selector" do
scoped.selector.should be_empty
end
end

[ :size, :length ].each do |method|

describe "##{method}" do
Expand Down
19 changes: 19 additions & 0 deletions spec/functional/mongoid/relations/referenced/many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,25 @@
end
end

describe "#scoped" do

let(:person) do
Person.new
end

let(:scoped) do
person.posts.scoped
end

it "returns the relation criteria" do
scoped.should be_a(Mongoid::Criteria)
end

it "returns with an empty selector" do
scoped.selector.should eq({ "person_id" => person.id })
end
end

[ :size, :length ].each do |method|

describe "##{method}" do
Expand Down
19 changes: 19 additions & 0 deletions spec/functional/mongoid/relations/referenced/many_to_many_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,25 @@
end
end

describe "#scoped" do

let(:person) do
Person.new
end

let(:scoped) do
person.preferences.scoped
end

it "returns the relation criteria" do
scoped.should be_a(Mongoid::Criteria)
end

it "returns with an empty selector" do
scoped.selector.should eq({ :_id => { "$in" => [] }})
end
end

[ :size, :length ].each do |method|

describe "##{method}" do
Expand Down

0 comments on commit 8623b94

Please sign in to comment.