Skip to content

Commit

Permalink
Fix default scoping merge to delay until needed
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Aug 22, 2012
1 parent 3c82d17 commit 45b56df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/mongoid/scoping.rb
Expand Up @@ -296,8 +296,8 @@ def #{name}(*args)
# #
# @since 3.0.5 # @since 3.0.5
def process_default_scope(value) def process_default_scope(value)
if default_scoping if existing = default_scoping
default_scoping.call.merge(value.to_proc.call).to_proc ->{ existing.call.merge(value.to_proc.call) }
else else
value.to_proc value.to_proc
end end
Expand Down
25 changes: 20 additions & 5 deletions spec/mongoid/scoping_spec.rb
Expand Up @@ -70,23 +70,38 @@
end end


let(:proc_criteria) do let(:proc_criteria) do
-> { Band.where(active: true) } ->{ Band.where(active: true) }
end

let(:rand_criteria) do
->{ Band.gt(likes: rand(100)) }
end end


before do before do
Band.default_scope criteria Band.default_scope criteria
Band.default_scope additional_criteria Band.default_scope additional_criteria
Band.default_scope proc_criteria Band.default_scope proc_criteria
Band.default_scope rand_criteria
end end


after do after do
Band.default_scoping = nil Band.default_scoping = nil
end end


it "adds the default scope to the class" do it "adds the first default scope" do
Band.default_scoping.call.should eq( Band.default_scoping.call.selector["name"].should eq("Depeche Mode")
criteria.merge(additional_criteria).merge(proc_criteria.call) end
)
it "adds the additional default scope" do
Band.default_scoping.call.selector["origin"].should eq("England")
end

it "adds the proc default scope" do
Band.default_scoping.call.selector["active"].should be_true
end

it "delays execution of the merge until called" do
Band.all.selector["likes"].should_not eq(Band.all.selector["likes"])
end end


it "flags as being default scoped" do it "flags as being default scoped" do
Expand Down

0 comments on commit 45b56df

Please sign in to comment.