Permalink
Browse files

Provide ability to avoid all named scopes to be applied with default …

…scop no matter what. Closes #1359.
  • Loading branch information...
1 parent ae473a8 commit a6e559eff53f149023c0e611c870e498ecb859d1 @durran durran committed Dec 6, 2011
View
@@ -67,6 +67,9 @@ For instructions on upgrading to newer versions, visit
* \#1381, \#1371 The identity map now functions properly with inherited
documents. (Paul Canavese)
+* \#1359 Provide ability to not have default scope applied to all named
+ scopes via using lambdas.
+
* \#1333 Fixed errors with custom types that exist in namespaces. (Peter Gumeson)
## 2.3.4
View
@@ -6,6 +6,7 @@
require "mongoid/criterion/inclusion"
require "mongoid/criterion/inspection"
require "mongoid/criterion/optional"
+require "mongoid/criterion/scoping"
require "mongoid/criterion/selector"
module Mongoid #:nodoc:
@@ -29,6 +30,7 @@ class Criteria
include Criterion::Inclusion
include Criterion::Inspection
include Criterion::Optional
+ include Criterion::Scoping
attr_accessor \
:documents,
@@ -0,0 +1,21 @@
+# encoding: utf-8
+module Mongoid #:nodoc:
+ module Criterion #:nodoc:
+ module Scoping
+ attr_accessor :unscopable
+
+ # Is the criteria unscopable? This is here as a temporary workaround
+ # until we can rewrite the entire scoping.
+ #
+ # @example Is the criteria unscopable?
+ # criteria.unscopable?
+ #
+ # @return [ true, false ] If default scoping is not allowed.
+ #
+ # @since 2.4.0
+ def unscopable?
+ !!@unscopable
+ end
+ end
+ end
+end
View
@@ -25,7 +25,15 @@ module ClassMethods #:nodoc:
# @since 2.0.0
def criteria(embedded = false, scoped = true)
scope_stack.last || Criteria.new(self, embedded).tap do |crit|
- return crit.fuse(default_scoping) if default_scoping && scoped
+ # @todo: Durran, Need to rewrite all the scoping code for 3.0.0 to
+ # avoid the lambda workaround.
+ if scoped
+ if default_scoping && !crit.unscopable?
+ return crit.fuse(default_scoping)
+ end
+ else
+ crit.unscopable = true
+ end
end
end
@@ -5,9 +5,9 @@ class Acolyte
embeds_many :versions, :as => :memorable
- scope :active, where(:status => "active")
-
default_scope asc(:name)
+ scope :active, lambda { where(:status => "active") }
+ scope :named, lambda { where(:name.exists => true) }
def callback_test?
name == "callback-test"
@@ -4,16 +4,56 @@
describe ".default_scope" do
- subject { Acolyte.all }
+ let(:criteria) do
+ Acolyte.all
+ end
- its(:options) { should == {:sort => [[:name, :asc]]} }
- end
+ it "applies the scope to any criteria" do
+ criteria.options.should eq({ :sort => [[ :name, :asc ]] })
+ end
+
+ context "when combining with a named scope" do
+
+ let(:scoped) do
+ Acolyte.active
+ end
+
+ it "applies the default scope" do
+ scoped.options.should eq({ :sort => [[ :name, :asc ]] })
+ end
+ end
+
+ context "when calling unscoped" do
+
+ let(:unscoped) do
+ Acolyte.unscoped
+ end
+
+ it "does not contain the default scoping" do
+ unscoped.options.should eq({})
+ end
+
+ context "when applying a named scope after" do
+
+ let(:named) do
+ Acolyte.unscoped.active
+ end
+
+ it "does not contain the default scoping" do
+ named.options.should eq({})
+ end
- context "combined with a named scope" do
+ context "when applying multiple scopes after" do
- subject { Acolyte.active }
+ let(:multiple) do
+ named.named
+ end
- its(:options) { should == {:sort => [[:name, :asc]]} }
- its(:selector) { should == {:status => "active"} }
+ it "does not contain the default scoping" do
+ multiple.options.should eq({})
+ end
+ end
+ end
+ end
end
end

0 comments on commit a6e559e

Please sign in to comment.