Skip to content

Commit ca16168

Browse files
authored
Merge pull request #60 from rustagir/DOCSP-44954-scoping
DOCSP-44954: scoping
2 parents 1a5bac2 + 78f7284 commit ca16168

File tree

5 files changed

+446
-6
lines changed

5 files changed

+446
-6
lines changed

snooty.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ intersphinx = [ "https://www.mongodb.com/docs/manual/objects.inv",
88
toc_landing_pages = [
99
"/quick-start-rails",
1010
"/quick-start-sinatra",
11-
"/interact-data"
11+
"/interact-data",
12+
"/interact-data/specify-query"
1213
]
1314

1415
[constants]
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# start-named-scope-1
2+
class Band
3+
include Mongoid::Document
4+
5+
field :country, type: String
6+
field :genres, type: Array
7+
8+
scope :japanese, ->{ where(country: "Japan") }
9+
scope :rock, ->{ where(:genres.in => [ "rock" ]) }
10+
end
11+
# end-named-scope-1
12+
13+
# start-query-named-scope
14+
Band.japanese.rock
15+
# end-query-named-scope
16+
17+
# start-named-scope-2
18+
class Band
19+
include Mongoid::Document
20+
21+
field :name, type: String
22+
field :country, type: String
23+
24+
scope :based_in, ->(country){ where(country: country) }
25+
end
26+
# end-named-scope-2
27+
28+
# start-query-named-scope-2
29+
Band.based_in("Spain")
30+
# end-query-named-scope-2
31+
32+
# start-named-scope-3
33+
class Band
34+
include Mongoid::Document
35+
36+
def self.on_tour
37+
true
38+
end
39+
40+
scope :on_tour, ->{ where(on_tour: true) }
41+
end
42+
# end-named-scope-3
43+
44+
# start-default-scope-1
45+
class Band
46+
include Mongoid::Document
47+
48+
field :name, type: String
49+
field :active, type: Boolean
50+
51+
default_scope -> { where(active: true) }
52+
end
53+
# end-default-scope-1
54+
55+
# start-default-scope-2
56+
class Band
57+
include Mongoid::Document
58+
59+
field :name, type: String
60+
field :on_tour, type: Boolean, default: true
61+
62+
default_scope ->{ where(on_tour: false) }
63+
end
64+
65+
# Creates a new Band instance in which "on_tour" is "false"
66+
Band.new
67+
# end-default-scope-2
68+
69+
# start-unscoped
70+
# Inline example
71+
Band.unscoped.where(name: "Depeche Mode")
72+
73+
# Block example
74+
Band.unscoped do
75+
Band.where(name: "Depeche Mode")
76+
end
77+
# end-unscoped
78+
79+
# start-scope-association
80+
class Label
81+
include Mongoid::Document
82+
83+
field :name, type: String
84+
85+
embeds_many :bands
86+
end
87+
88+
class Band
89+
include Mongoid::Document
90+
91+
field :name, type: String
92+
field :active, default: true
93+
94+
embedded_in :label
95+
default_scope ->{ where(active: true) }
96+
end
97+
# end-scope-association
98+
99+
# start-scope-association-steps
100+
label = Label.new(name: "Hello World Records")
101+
band = Band.new(name: "Ghost Mountain")
102+
label.bands.push(band)
103+
label.bands # Displays the Band because "active" is "true"
104+
band.update_attribute(:active, false) # Updates "active" to "false"
105+
106+
# Displays the "Ghost Mountain" band
107+
label.bands # => {"_id":"...","name":"Ghost Mountain",...}
108+
109+
# Won't display "Ghost Mountain" band after reloading
110+
label.reload.bands # => nil
111+
# end-scope-association-steps
112+
113+
# start-scope-query-behavior
114+
class Band
115+
include Mongoid::Document
116+
117+
field :name
118+
field :touring
119+
field :member_count
120+
121+
default_scope ->{ where(touring: true) }
122+
end
123+
124+
# Combines the condition to the default scope with "and"
125+
Band.where(name: 'Infected Mushroom')
126+
# Interpreted query:
127+
# {"touring"=>true, "name"=>"Infected Mushroom"}
128+
129+
# Combines the first condition to the default scope with "and"
130+
Band.where(name: 'Infected Mushroom').or(member_count: 3)
131+
# Interpreted query:
132+
# {"$or"=>[{"touring"=>true, "name"=>"Infected Mushroom"}, {"member_count"=>3}]}
133+
134+
# Combines the condition to the default scope with "or"
135+
Band.or(member_count: 3)
136+
# Interpreted query:
137+
# {"$or"=>[{"touring"=>true}, {"member_count"=>3}]}
138+
# end-scope-query-behavior
139+
140+
# start-override-scope
141+
class Band
142+
include Mongoid::Document
143+
144+
field :country, type: String
145+
field :genres, type: Array
146+
147+
scope :mexican, ->{ where(country: "Mexico") }
148+
end
149+
# end-override-scope
150+
151+
# start-override-scope-block
152+
Band.with_scope(Band.mexican) do
153+
Band.all
154+
end
155+
# end-override-scope-block
156+
157+
# start-class-methods
158+
class Band
159+
include Mongoid::Document
160+
161+
field :name, type: String
162+
field :touring, type: Boolean, default: true
163+
164+
def self.touring
165+
where(touring: true)
166+
end
167+
end
168+
169+
Band.touring
170+
# end-class-methods

source/interact-data/modify-results.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,12 @@ When you chain sort specifications, the first call defines the first
222222
sorting order and the newest call defines the last sorting order after
223223
the previous sorts have been applied.
224224

225-
.. TODO update link in the following note for scope
226-
227225
.. note:: Sorting in Scopes
228226

229-
If you define a scope on your model that includes a sort specification,
230-
the scope sort takes precedence over the sort specified in a query,
231-
because the default scope is evaluated first.
227+
If you define a :ref:`default scope <mongoid-data-scoping>` on your
228+
model that includes a sort specification, the scope sort takes precedence
229+
over the sort specified in a query, because the default scope is
230+
evaluated first.
232231

233232
.. _mongoid-data-skip-limit:
234233

0 commit comments

Comments
 (0)