Skip to content

Commit 754b30b

Browse files
authored
MONGOID-5229 Add Feature Flag: .with_scope should restore previous scope (#5143)
* MONGOID-5229 Add Feature Flag: .with_scope should restore previous scope * MONGOID-5229 add config tests * MONGOID-5229 add note to docs about global flags * MONGOID-5229 fix spacing
1 parent c18f7e7 commit 754b30b

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

source/reference/configuration.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ for details on driver options.
292292
# (default: true)
293293
raise_not_found_error: true
294294

295+
# In Mongoid 7.3.3 and earlier, there was a bug when using with_scope
296+
# that after the function returned, any previous scopes were lost.
297+
# Turning this flag on fixes that bug. (default: false)
298+
restore_previous_scope: false
299+
295300
# Raise an error when defining a scope with the same name as an
296301
# existing method. (default: false)
297302
scope_overwrite_exception: false

source/reference/queries.txt

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ The opposite of ``only``, ``without`` causes the specified fields to be omitted:
851851
.. code-block:: ruby
852852

853853
Band.without(:name)
854-
# =>
854+
# =>
855855
# #<Mongoid::Criteria
856856
# selector: {}
857857
# options: {:fields=>{"name"=>0}}
@@ -864,15 +864,15 @@ as its ``id`` alias) cannot be omitted via ``without``:
864864
.. code-block:: ruby
865865

866866
Band.without(:name, :id)
867-
# =>
867+
# =>
868868
# #<Mongoid::Criteria
869869
# selector: {}
870870
# options: {:fields=>{"name"=>0}}
871871
# class: Band
872872
# embedded: false>
873873

874874
Band.without(:name, :_id)
875-
# =>
875+
# =>
876876
# #<Mongoid::Criteria
877877
# selector: {}
878878
# options: {:fields=>{"name"=>0}}
@@ -1191,10 +1191,10 @@ Mongoid also has some helpful methods on criteria.
11911191

11921192
*Get a list of distinct values for a single field. Note this will always hit
11931193
the database for the distinct values.*
1194-
1194+
11951195
*This method accepts the dot notation, thus permitting referencing
11961196
fields in embedded associations.*
1197-
1197+
11981198
*This method respects :ref:`field aliases <field-aliases>`,
11991199
including those defined in embedded documents.*
12001200

@@ -1206,14 +1206,14 @@ Mongoid also has some helpful methods on criteria.
12061206
distinct(:name)
12071207

12081208
Band.distinct('cities.name')
1209-
1209+
12101210
# Assuming an aliased field:
12111211
class Manager
12121212
include Mongoid::Document
12131213
embedded_in :band
12141214
field :name, as: :n
12151215
end
1216-
1216+
12171217
# Expands out to "managers.name" in the query:
12181218
Band.distinct('managers.n')
12191219

@@ -1362,10 +1362,10 @@ Mongoid also has some helpful methods on criteria.
13621362

13631363
*Get all the values for the provided field.
13641364
Returns nil for unset fields and for non-existent fields.*
1365-
1365+
13661366
*This method accepts the dot notation, thus permitting referencing
13671367
fields in embedded associations.*
1368-
1368+
13691369
*This method respects :ref:`field aliases <field-aliases>`,
13701370
including those defined in embedded documents.*
13711371

@@ -1457,10 +1457,10 @@ field, a ``Date`` field and an implicit ``Object`` field, and also
14571457
intentionally does not define a field called ``deregistered_at``:
14581458

14591459
.. code-block:: ruby
1460-
1460+
14611461
class Voter
14621462
include Mongoid::Document
1463-
1463+
14641464
field :born_on, type: Date
14651465
field :registered_at, type: Time
14661466
field :voted_at
@@ -1473,7 +1473,7 @@ values, respectively, are straightforward:
14731473

14741474
Voter.where(born_on: Date.today).selector
14751475
# => {"born_on"=>2020-12-18 00:00:00 UTC}
1476-
1476+
14771477
Voter.where(registered_at: Time.now).selector
14781478
# => {"registered_at"=>2020-12-19 04:33:36.939788067 UTC}
14791479

@@ -1484,13 +1484,13 @@ in all possible scenarios:
14841484

14851485
Voter.where(born_on: Date.today).selector
14861486
# => {"born_on"=>2020-12-18 00:00:00 UTC}
1487-
1487+
14881488
Voter.where(registered_at: Date.today).selector
14891489
# => {"registered_at"=>2020-12-18 00:00:00 -0500}
1490-
1490+
14911491
Voter.where(voted_at: Date.today).selector
14921492
# => {"voted_at"=>Fri, 18 Dec 2020}
1493-
1493+
14941494
Voter.where(deregistered_at: Date.today).selector
14951495
# => {"deregistered_at"=>2020-12-18 00:00:00 UTC}
14961496

@@ -1715,38 +1715,38 @@ that would affect its visibility within the scoped association.
17151715

17161716
class Band
17171717
include Mongoid::Document
1718-
1718+
17191719
field :name
17201720
field :active
17211721
field :touring
1722-
1722+
17231723
default_scope ->{ where(active: true) }
17241724
end
1725-
1725+
17261726
Band.where(name: 'Infected Mushroom')
1727-
# =>
1727+
# =>
17281728
# #<Mongoid::Criteria
17291729
# selector: {"active"=>true, "name"=>"Infected Mushroom"}
17301730
# options: {}
17311731
# class: Band
17321732
# embedded: false>
1733-
1733+
17341734
Band.where(name: 'Infected Mushroom').or(touring: true)
1735-
# =>
1735+
# =>
17361736
# #<Mongoid::Criteria
17371737
# selector: {"$or"=>[{"active"=>true, "name"=>"Infected Mushroom"}, {"touring"=>true}]}
17381738
# options: {}
17391739
# class: Band
17401740
# embedded: false>
1741-
1741+
17421742
Band.or(touring: true)
1743-
# =>
1743+
# =>
17441744
# #<Mongoid::Criteria
17451745
# selector: {"$or"=>[{"active"=>true}, {"touring"=>true}]}
17461746
# options: {}
17471747
# class: Band
17481748
# embedded: false>
1749-
1749+
17501750
In the last example, you might expect the two conditions
17511751
(``active: true`` and ``touring: true``) to be combined with an ``$and``,
17521752
but because the ``Band`` class already has the scope applied to it,
@@ -1772,15 +1772,22 @@ at runtime:
17721772
criteria = Band.with_scope(Band.english) do
17731773
Band.all
17741774
end
1775-
1775+
17761776
criteria
1777-
# =>
1777+
# =>
17781778
# #<Mongoid::Criteria
17791779
# selector: {"country"=>"England"}
17801780
# options: {}
17811781
# class: Band
17821782
# embedded: false>
17831783

1784+
.. note::
1785+
1786+
In Mongoid versions 7.3.3 and earlier, there is a bug in ``with_scope``
1787+
where the previous scope is lost after the function returns. Set the
1788+
``Mongoid::restore_previous_scope`` global flag to true in order to get the
1789+
correct functionality.
1790+
17841791

17851792
Class Methods
17861793
-------------

0 commit comments

Comments
 (0)