mongoid 0.7.8 → 0.7.9
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongoid/criteria.rb +27 -17
- data/lib/mongoid/document.rb +2 -3
- data/mongoid.gemspec +1 -1
- data/spec/integration/mongoid/document_spec.rb +6 -2
- data/spec/unit/mongoid/criteria_spec.rb +81 -15
- data/spec/unit/mongoid/document_spec.rb +4 -4
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.9
|
data/lib/mongoid/criteria.rb
CHANGED
@@ -64,8 +64,9 @@ module Mongoid #:nodoc:
|
|
64
64
|
#
|
65
65
|
# Returns: <tt>Integer</tt>
|
66
66
|
def count(klass = nil)
|
67
|
+
return @count if @count
|
67
68
|
@klass = klass if klass
|
68
|
-
return @klass.collection.find(@selector, @options).count
|
69
|
+
return @klass.collection.find(@selector, @options.dup).count
|
69
70
|
end
|
70
71
|
|
71
72
|
# Adds a criterion to the +Criteria+ that specifies values that are not allowed
|
@@ -100,13 +101,17 @@ module Mongoid #:nodoc:
|
|
100
101
|
# objects of the type of class provided.
|
101
102
|
def execute(klass = nil)
|
102
103
|
@klass = klass if klass
|
103
|
-
filter_options
|
104
104
|
if type == :first
|
105
|
-
attributes = klass.collection.find_one(@selector, @options)
|
106
|
-
attributes ? @klass.instantiate(attributes) : nil
|
105
|
+
attributes = klass.collection.find_one(@selector, @options.dup)
|
106
|
+
return attributes ? @klass.instantiate(attributes) : nil
|
107
107
|
else
|
108
|
-
attributes = @klass.collection.find(@selector, @options)
|
109
|
-
|
108
|
+
attributes = @klass.collection.find(@selector, @options.dup)
|
109
|
+
if attributes
|
110
|
+
@count = attributes.count
|
111
|
+
return attributes.collect { |doc| @klass.instantiate(doc) }
|
112
|
+
else
|
113
|
+
return []
|
114
|
+
end
|
110
115
|
end
|
111
116
|
end
|
112
117
|
|
@@ -123,7 +128,9 @@ module Mongoid #:nodoc:
|
|
123
128
|
#
|
124
129
|
# Returns: <tt>self</tt>
|
125
130
|
def extras(extras)
|
126
|
-
@options = extras
|
131
|
+
@options = extras
|
132
|
+
filter_options
|
133
|
+
self
|
127
134
|
end
|
128
135
|
|
129
136
|
GROUP_REDUCE = "function(obj, prev) { prev.group.push(obj); }"
|
@@ -234,9 +241,7 @@ module Mongoid #:nodoc:
|
|
234
241
|
# will replace it with a skip parameter and return the same value. Defaults
|
235
242
|
# to 20 if nothing was provided.
|
236
243
|
def offset
|
237
|
-
|
238
|
-
offset = (page * per_page) - per_page
|
239
|
-
@options[:skip] ||= offset
|
244
|
+
@options[:skip]
|
240
245
|
end
|
241
246
|
|
242
247
|
# Adds a criterion to the +Criteria+ that specifies the sort order of
|
@@ -258,15 +263,16 @@ module Mongoid #:nodoc:
|
|
258
263
|
# Either returns the page option and removes it from the options, or
|
259
264
|
# returns a default value of 1.
|
260
265
|
def page
|
261
|
-
@options[:
|
266
|
+
if @options[:skip] && @options[:limit]
|
267
|
+
(@options[:skip].to_i + @options[:limit].to_i) / @options[:limit].to_i
|
268
|
+
else
|
269
|
+
1
|
270
|
+
end
|
262
271
|
end
|
263
272
|
|
264
273
|
# Returns the number of results per page or the default of 20.
|
265
274
|
def per_page
|
266
|
-
|
267
|
-
skip = (page * num) - num
|
268
|
-
@options[:skip] = skip
|
269
|
-
num
|
275
|
+
(@options[:limit] || 20).to_i
|
270
276
|
end
|
271
277
|
|
272
278
|
# Adds a criterion to the +Criteria+ that specifies the fields that will
|
@@ -351,8 +357,12 @@ module Mongoid #:nodoc:
|
|
351
357
|
|
352
358
|
protected
|
353
359
|
def filter_options
|
354
|
-
@options.delete(:page)
|
355
|
-
@options.delete(:per_page)
|
360
|
+
page_num = @options.delete(:page)
|
361
|
+
per_page_num = @options.delete(:per_page)
|
362
|
+
if (page_num || per_page_num)
|
363
|
+
@options[:limit] = (per_page_num || 20).to_i
|
364
|
+
@options[:skip] = (page_num || 1).to_i * @options[:limit] - @options[:limit]
|
365
|
+
end
|
356
366
|
end
|
357
367
|
|
358
368
|
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -172,9 +172,8 @@ module Mongoid #:nodoc:
|
|
172
172
|
# Returns paginated array of docs.
|
173
173
|
def paginate(params = {})
|
174
174
|
criteria = Criteria.translate(:all, params)
|
175
|
-
|
176
|
-
|
177
|
-
pager.total_entries = results.size
|
175
|
+
results = criteria.execute(self)
|
176
|
+
WillPaginate::Collection.create(criteria.page, criteria.per_page, criteria.count) do |pager|
|
178
177
|
pager.replace(results)
|
179
178
|
end
|
180
179
|
end
|
data/mongoid.gemspec
CHANGED
@@ -114,6 +114,12 @@ describe Mongoid::Document do
|
|
114
114
|
Person.paginate(:per_page => 20, :page => 2).length.should == 10
|
115
115
|
end
|
116
116
|
|
117
|
+
it "returns a proper count" do
|
118
|
+
@criteria = Mongoid::Criteria.translate(:all, { :per_page => 20, :page => 1 })
|
119
|
+
@criteria.execute(Person)
|
120
|
+
@criteria.count.should == 30
|
121
|
+
end
|
122
|
+
|
117
123
|
end
|
118
124
|
|
119
125
|
describe "#reload" do
|
@@ -274,10 +280,8 @@ describe Mongoid::Document do
|
|
274
280
|
end
|
275
281
|
|
276
282
|
it "creates new versions" do
|
277
|
-
p @comment
|
278
283
|
@from_db = Comment.find(@comment.id)
|
279
284
|
@from_db.version.should == 6
|
280
|
-
p @from_db
|
281
285
|
@from_db.versions.size.should == 5
|
282
286
|
end
|
283
287
|
|
@@ -56,19 +56,35 @@ describe Mongoid::Criteria do
|
|
56
56
|
|
57
57
|
describe "#count" do
|
58
58
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
59
|
+
context "when criteria has not been executed" do
|
60
|
+
|
61
|
+
before do
|
62
|
+
@criteria.instance_variable_set(:@count, 34)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "returns a count from the cursor" do
|
66
|
+
@criteria.count.should == 34
|
67
|
+
end
|
68
|
+
|
66
69
|
end
|
67
70
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
71
|
+
context "when criteria has been executed" do
|
72
|
+
|
73
|
+
before do
|
74
|
+
@criteria = Mongoid::Criteria.new(:all, Person)
|
75
|
+
@selector = { :test => "Testing" }
|
76
|
+
@criteria.where(@selector)
|
77
|
+
@collection = mock
|
78
|
+
@cursor = mock
|
79
|
+
Person.expects(:collection).returns(@collection)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "returns the count from the cursor without creating the documents" do
|
83
|
+
@collection.expects(:find).with(@selector, {}).returns(@cursor)
|
84
|
+
@cursor.expects(:count).returns(10)
|
85
|
+
@criteria.count.should == 10
|
86
|
+
end
|
87
|
+
|
72
88
|
end
|
73
89
|
|
74
90
|
end
|
@@ -93,8 +109,8 @@ describe Mongoid::Criteria do
|
|
93
109
|
before do
|
94
110
|
@collection = mock
|
95
111
|
Person.expects(:collection).returns(@collection)
|
96
|
-
@collection.expects(:find).with(@criteria.selector, @criteria.options).returns([])
|
97
112
|
@criteria = Mongoid::Criteria.new(:all).extras(:page => 1, :per_page => 20)
|
113
|
+
@collection.expects(:find).with(@criteria.selector, @criteria.options).returns([])
|
98
114
|
end
|
99
115
|
|
100
116
|
it "filters out unused params" do
|
@@ -105,6 +121,23 @@ describe Mongoid::Criteria do
|
|
105
121
|
|
106
122
|
end
|
107
123
|
|
124
|
+
context "when type is :all" do
|
125
|
+
|
126
|
+
before do
|
127
|
+
@collection = mock
|
128
|
+
Person.expects(:collection).returns(@collection)
|
129
|
+
@criteria = Mongoid::Criteria.new(:all).extras(:page => 1, :per_page => 20)
|
130
|
+
@cursor = stub(:count => 44, :collect => [])
|
131
|
+
@collection.expects(:find).with(@criteria.selector, @criteria.options).returns(@cursor)
|
132
|
+
end
|
133
|
+
|
134
|
+
it "adds the count instance variable" do
|
135
|
+
@criteria.execute(Person).should == []
|
136
|
+
@criteria.count.should == 44
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
|
108
141
|
context "when type is :first" do
|
109
142
|
|
110
143
|
context "when documents exist" do
|
@@ -155,6 +188,39 @@ describe Mongoid::Criteria do
|
|
155
188
|
|
156
189
|
describe "#extras" do
|
157
190
|
|
191
|
+
context "filtering" do
|
192
|
+
|
193
|
+
context "when page is provided" do
|
194
|
+
|
195
|
+
it "sets the limit and skip options" do
|
196
|
+
@criteria.extras({ :page => "2" })
|
197
|
+
@criteria.page.should == 2
|
198
|
+
@criteria.options.should == { :skip => 20, :limit => 20 }
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
202
|
+
|
203
|
+
context "when per_page is provided" do
|
204
|
+
|
205
|
+
it "sets the limit and skip options" do
|
206
|
+
@criteria.extras({ :per_page => 45 })
|
207
|
+
@criteria.options.should == { :skip => 0, :limit => 45 }
|
208
|
+
end
|
209
|
+
|
210
|
+
end
|
211
|
+
|
212
|
+
context "when page and per_page both provided" do
|
213
|
+
|
214
|
+
it "sets the limit and skip options" do
|
215
|
+
@criteria.extras({ :per_page => 30, :page => "4" })
|
216
|
+
@criteria.options.should == { :skip => 90, :limit => 30 }
|
217
|
+
@criteria.page.should == 4
|
218
|
+
end
|
219
|
+
|
220
|
+
end
|
221
|
+
|
222
|
+
end
|
223
|
+
|
158
224
|
it "adds the extras to the options" do
|
159
225
|
@criteria.extras({ :skip => 10 })
|
160
226
|
@criteria.options.should == { :skip => 10 }
|
@@ -319,9 +385,9 @@ describe Mongoid::Criteria do
|
|
319
385
|
@criteria = Mongoid::Criteria.new(:all)
|
320
386
|
end
|
321
387
|
|
322
|
-
it "
|
323
|
-
@criteria.offset.should
|
324
|
-
@criteria.options[:skip].should
|
388
|
+
it "returns nil" do
|
389
|
+
@criteria.offset.should be_nil
|
390
|
+
@criteria.options[:skip].should be_nil
|
325
391
|
end
|
326
392
|
|
327
393
|
end
|
@@ -59,7 +59,7 @@ describe Mongoid::Document do
|
|
59
59
|
describe "#all" do
|
60
60
|
|
61
61
|
before do
|
62
|
-
@cursor =
|
62
|
+
@cursor = stub(:count => 100)
|
63
63
|
@people = []
|
64
64
|
end
|
65
65
|
|
@@ -282,7 +282,7 @@ describe Mongoid::Document do
|
|
282
282
|
context "when finding all" do
|
283
283
|
|
284
284
|
before do
|
285
|
-
@cursor =
|
285
|
+
@cursor = stub(:count => 100)
|
286
286
|
@people = []
|
287
287
|
end
|
288
288
|
|
@@ -297,7 +297,7 @@ describe Mongoid::Document do
|
|
297
297
|
context "when sorting" do
|
298
298
|
|
299
299
|
before do
|
300
|
-
@cursor =
|
300
|
+
@cursor = stub(:count => 50)
|
301
301
|
@people = []
|
302
302
|
end
|
303
303
|
|
@@ -550,7 +550,7 @@ describe Mongoid::Document do
|
|
550
550
|
describe "#paginate" do
|
551
551
|
|
552
552
|
before do
|
553
|
-
@criteria = stub(:page => 1, :per_page => "20")
|
553
|
+
@criteria = stub(:page => 1, :per_page => "20", :count => 100)
|
554
554
|
end
|
555
555
|
|
556
556
|
context "when pagination parameters are passed" do
|