Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix default scoping merge to delay until needed

  • Loading branch information...
commit 45b56df91b7ec95f575b7d18bfca104e8b0a2ff1 1 parent 3c82d17
@durran durran authored
Showing with 22 additions and 7 deletions.
  1. +2 −2 lib/mongoid/scoping.rb
  2. +20 −5 spec/mongoid/scoping_spec.rb
View
4 lib/mongoid/scoping.rb
@@ -296,8 +296,8 @@ def #{name}(*args)
#
# @since 3.0.5
def process_default_scope(value)
- if default_scoping
- default_scoping.call.merge(value.to_proc.call).to_proc
+ if existing = default_scoping
+ ->{ existing.call.merge(value.to_proc.call) }
else
value.to_proc
end
View
25 spec/mongoid/scoping_spec.rb
@@ -70,23 +70,38 @@
end
let(:proc_criteria) do
- -> { Band.where(active: true) }
+ ->{ Band.where(active: true) }
+ end
+
+ let(:rand_criteria) do
+ ->{ Band.gt(likes: rand(100)) }
end
before do
Band.default_scope criteria
Band.default_scope additional_criteria
Band.default_scope proc_criteria
+ Band.default_scope rand_criteria
end
after do
Band.default_scoping = nil
end
- it "adds the default scope to the class" do
- Band.default_scoping.call.should eq(
- criteria.merge(additional_criteria).merge(proc_criteria.call)
- )
+ it "adds the first default scope" do
+ Band.default_scoping.call.selector["name"].should eq("Depeche Mode")
+ 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
it "flags as being default scoped" do
Please sign in to comment.
Something went wrong with that request. Please try again.