mongoid 0.7.4 → 0.7.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/lib/mongoid/criteria.rb +18 -2
- data/lib/mongoid/document.rb +10 -5
- data/mongoid.gemspec +2 -2
- data/spec/unit/mongoid/criteria_spec.rb +69 -18
- data/spec/unit/mongoid/document_spec.rb +23 -6
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.7.
|
1
|
+
0.7.5
|
data/lib/mongoid/criteria.rb
CHANGED
@@ -100,6 +100,7 @@ module Mongoid #:nodoc:
|
|
100
100
|
# objects of the type of class provided.
|
101
101
|
def execute(klass = nil)
|
102
102
|
@klass = klass if klass
|
103
|
+
filter_options
|
103
104
|
if type == :first
|
104
105
|
attributes = klass.collection.find_one(@selector, @options)
|
105
106
|
attributes ? @klass.instantiate(attributes) : nil
|
@@ -233,7 +234,8 @@ module Mongoid #:nodoc:
|
|
233
234
|
# will replace it with a skip parameter and return the same value. Defaults
|
234
235
|
# to 20 if nothing was provided.
|
235
236
|
def offset
|
236
|
-
|
237
|
+
per_page = @options[:per_page] || 20
|
238
|
+
offset = (page * per_page) - per_page
|
237
239
|
@options[:skip] ||= offset
|
238
240
|
end
|
239
241
|
|
@@ -256,7 +258,15 @@ module Mongoid #:nodoc:
|
|
256
258
|
# Either returns the page option and removes it from the options, or
|
257
259
|
# returns a default value of 1.
|
258
260
|
def page
|
259
|
-
@options
|
261
|
+
@options[:page] || 1
|
262
|
+
end
|
263
|
+
|
264
|
+
# Returns the number of results per page or the default of 20.
|
265
|
+
def per_page
|
266
|
+
num = @options[:per_page] || 20
|
267
|
+
skip = (page * num) - num
|
268
|
+
@options[:skip] = skip
|
269
|
+
num
|
260
270
|
end
|
261
271
|
|
262
272
|
# Adds a criterion to the +Criteria+ that specifies the fields that will
|
@@ -339,5 +349,11 @@ module Mongoid #:nodoc:
|
|
339
349
|
@selector = selector; self
|
340
350
|
end
|
341
351
|
|
352
|
+
protected
|
353
|
+
def filter_options
|
354
|
+
@options.delete(:page)
|
355
|
+
@options.delete(:per_page)
|
356
|
+
end
|
357
|
+
|
342
358
|
end
|
343
359
|
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -126,10 +126,15 @@ module Mongoid #:nodoc:
|
|
126
126
|
end
|
127
127
|
|
128
128
|
# Instantiate a new object, only when loaded from the database.
|
129
|
-
def instantiate(
|
130
|
-
|
131
|
-
|
132
|
-
|
129
|
+
def instantiate(attrs = {})
|
130
|
+
attributes = attrs.with_indifferent_access
|
131
|
+
if attributes[:_id]
|
132
|
+
document = allocate
|
133
|
+
document.instance_variable_set(:@attributes, attributes)
|
134
|
+
return document
|
135
|
+
else
|
136
|
+
return new(attributes)
|
137
|
+
end
|
133
138
|
end
|
134
139
|
|
135
140
|
# Defines the field that will be used for the id of this +Document+. This
|
@@ -167,7 +172,7 @@ module Mongoid #:nodoc:
|
|
167
172
|
# Returns paginated array of docs.
|
168
173
|
def paginate(params = {})
|
169
174
|
criteria = Criteria.translate(:all, params)
|
170
|
-
WillPaginate::Collection.create(criteria.page, criteria.
|
175
|
+
WillPaginate::Collection.create(criteria.page, criteria.per_page, 0) do |pager|
|
171
176
|
results = criteria.execute(self)
|
172
177
|
pager.total_entries = results.size
|
173
178
|
pager.replace(results)
|
data/mongoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.7.
|
8
|
+
s.version = "0.7.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Durran Jordan"]
|
12
|
-
s.date = %q{2009-11-
|
12
|
+
s.date = %q{2009-11-15}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
@@ -88,6 +88,23 @@ describe Mongoid::Criteria do
|
|
88
88
|
|
89
89
|
describe "#execute" do
|
90
90
|
|
91
|
+
context "filtering" do
|
92
|
+
|
93
|
+
before do
|
94
|
+
@collection = mock
|
95
|
+
Person.expects(:collection).returns(@collection)
|
96
|
+
@collection.expects(:find).with(@criteria.selector, @criteria.options).returns([])
|
97
|
+
@criteria = Mongoid::Criteria.new(:all).extras(:page => 1, :per_page => 20)
|
98
|
+
end
|
99
|
+
|
100
|
+
it "filters out unused params" do
|
101
|
+
@criteria.execute(Person)
|
102
|
+
@criteria.options[:page].should be_nil
|
103
|
+
@criteria.options[:per_page].should be_nil
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
91
108
|
context "when type is :first" do
|
92
109
|
|
93
110
|
context "when documents exist" do
|
@@ -260,17 +277,11 @@ describe Mongoid::Criteria do
|
|
260
277
|
context "when the per_page option exists" do
|
261
278
|
|
262
279
|
before do
|
263
|
-
@criteria = Mongoid::Criteria.new(:all).extras({ :per_page => 20 })
|
280
|
+
@criteria = Mongoid::Criteria.new(:all).extras({ :per_page => 20, :page => 3 })
|
264
281
|
end
|
265
282
|
|
266
283
|
it "returns the per_page option" do
|
267
|
-
@criteria.offset.should ==
|
268
|
-
end
|
269
|
-
|
270
|
-
it "replaces per_page with skip in the options" do
|
271
|
-
@criteria.offset
|
272
|
-
@criteria.options[:per_page].should be_nil
|
273
|
-
@criteria.options[:skip].should == 20
|
284
|
+
@criteria.offset.should == 40
|
274
285
|
end
|
275
286
|
|
276
287
|
end
|
@@ -289,13 +300,30 @@ describe Mongoid::Criteria do
|
|
289
300
|
|
290
301
|
context "when no option exists" do
|
291
302
|
|
292
|
-
|
293
|
-
|
303
|
+
context "when page option exists" do
|
304
|
+
|
305
|
+
before do
|
306
|
+
@criteria = Mongoid::Criteria.new(:all).extras({ :page => 2 })
|
307
|
+
end
|
308
|
+
|
309
|
+
it "adds the skip option to the options and returns it" do
|
310
|
+
@criteria.offset.should == 20
|
311
|
+
@criteria.options[:skip].should == 20
|
312
|
+
end
|
313
|
+
|
294
314
|
end
|
295
315
|
|
296
|
-
|
297
|
-
|
298
|
-
|
316
|
+
context "when page option does not exist" do
|
317
|
+
|
318
|
+
before do
|
319
|
+
@criteria = Mongoid::Criteria.new(:all)
|
320
|
+
end
|
321
|
+
|
322
|
+
it "adds the skip option to the options and returns it" do
|
323
|
+
@criteria.offset.should == 0
|
324
|
+
@criteria.options[:skip].should == 0
|
325
|
+
end
|
326
|
+
|
299
327
|
end
|
300
328
|
|
301
329
|
end
|
@@ -331,11 +359,6 @@ describe Mongoid::Criteria do
|
|
331
359
|
@criteria.page.should == 5
|
332
360
|
end
|
333
361
|
|
334
|
-
it "deletes the page option from the options" do
|
335
|
-
@criteria.page.should == 5
|
336
|
-
@criteria.options[:page].should be_nil
|
337
|
-
end
|
338
|
-
|
339
362
|
end
|
340
363
|
|
341
364
|
context "when the page option does not exist" do
|
@@ -352,6 +375,34 @@ describe Mongoid::Criteria do
|
|
352
375
|
|
353
376
|
end
|
354
377
|
|
378
|
+
describe "#per_page" do
|
379
|
+
|
380
|
+
context "when the per_page option exists" do
|
381
|
+
|
382
|
+
before do
|
383
|
+
@criteria = Mongoid::Criteria.new(:all).extras({ :per_page => 10 })
|
384
|
+
end
|
385
|
+
|
386
|
+
it "returns the per_page option" do
|
387
|
+
@criteria.per_page.should == 10
|
388
|
+
end
|
389
|
+
|
390
|
+
end
|
391
|
+
|
392
|
+
context "when the per_page option does not exist" do
|
393
|
+
|
394
|
+
before do
|
395
|
+
@criteria = Mongoid::Criteria.new(:all)
|
396
|
+
end
|
397
|
+
|
398
|
+
it "returns 1" do
|
399
|
+
@criteria.per_page.should == 20
|
400
|
+
end
|
401
|
+
|
402
|
+
end
|
403
|
+
|
404
|
+
end
|
405
|
+
|
355
406
|
describe "#select" do
|
356
407
|
|
357
408
|
context "when args are provided" do
|
@@ -371,13 +371,30 @@ describe Mongoid::Document do
|
|
371
371
|
|
372
372
|
describe "#instantiate" do
|
373
373
|
|
374
|
-
|
375
|
-
|
376
|
-
|
374
|
+
context "when document is new" do
|
375
|
+
|
376
|
+
before do
|
377
|
+
@attributes = { :_id => "1", :title => "Sir", :age => 30 }
|
378
|
+
@person = Person.new(@attributes)
|
379
|
+
end
|
380
|
+
|
381
|
+
it "sets the attributes directly" do
|
382
|
+
Person.instantiate(@attributes).should == @person
|
383
|
+
end
|
384
|
+
|
377
385
|
end
|
378
386
|
|
379
|
-
|
380
|
-
|
387
|
+
context "when document is not new" do
|
388
|
+
|
389
|
+
before do
|
390
|
+
@attributes = { :title => "Sir", :age => 30 }
|
391
|
+
@person = Person.new(@attributes)
|
392
|
+
end
|
393
|
+
|
394
|
+
it "instantiates normally" do
|
395
|
+
Person.instantiate(@attributes).id.should_not be_nil
|
396
|
+
end
|
397
|
+
|
381
398
|
end
|
382
399
|
|
383
400
|
end
|
@@ -519,7 +536,7 @@ describe Mongoid::Document do
|
|
519
536
|
describe "#paginate" do
|
520
537
|
|
521
538
|
before do
|
522
|
-
@criteria = stub(:page => 1, :
|
539
|
+
@criteria = stub(:page => 1, :per_page => "20")
|
523
540
|
end
|
524
541
|
|
525
542
|
context "when pagination parameters are passed" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-15 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|