mongoid 0.11.8 → 0.11.9
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/HISTORY +19 -0
- data/VERSION +1 -1
- data/lib/mongoid/attributes.rb +4 -3
- data/lib/mongoid/criteria.rb +56 -42
- data/lib/mongoid/document.rb +1 -6
- data/lib/mongoid/extensions/time/conversions.rb +2 -2
- data/mongoid.gemspec +2 -2
- data/spec/integration/mongoid/criteria_spec.rb +6 -6
- data/spec/integration/mongoid/document_spec.rb +9 -0
- data/spec/spec_helper.rb +3 -2
- data/spec/unit/mongoid/attributes_spec.rb +2 -7
- data/spec/unit/mongoid/criteria_spec.rb +6 -6
- data/spec/unit/mongoid/extensions/time/conversions_spec.rb +1 -1
- metadata +2 -2
data/HISTORY
CHANGED
@@ -1,3 +1,22 @@
|
|
1
|
+
=== 0.11.9
|
2
|
+
- Fixed issue with non-us time zones and formats
|
3
|
+
parsing incorrectly.
|
4
|
+
|
5
|
+
- Fixed error when specifying field restrictions
|
6
|
+
in criteria and not providing the _type. It
|
7
|
+
will now automaticall get added if it is not
|
8
|
+
present.
|
9
|
+
|
10
|
+
- Slight cleanup of delegated methods in Document.
|
11
|
+
|
12
|
+
- Dynamic attributes no longer create setters and
|
13
|
+
getters on the class. They can be accessed from
|
14
|
+
the attributes hash directly. If they are used
|
15
|
+
frequently it is preferrable to just add a field
|
16
|
+
to the class manually.
|
17
|
+
|
18
|
+
- Criteria#min no longer always returns 0.0.
|
19
|
+
|
1
20
|
=== 0.11.8
|
2
21
|
- Added #min and #max to criteria which takes a
|
3
22
|
single field argument.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.11.
|
1
|
+
0.11.9
|
data/lib/mongoid/attributes.rb
CHANGED
@@ -14,10 +14,11 @@ module Mongoid #:nodoc:
|
|
14
14
|
# put into the document's attributes.
|
15
15
|
def process(attrs = {})
|
16
16
|
attrs.each_pair do |key, value|
|
17
|
-
|
18
|
-
|
17
|
+
if Mongoid.allow_dynamic_fields && !respond_to?("#{key}=")
|
18
|
+
@attributes[key] = value
|
19
|
+
else
|
20
|
+
send("#{key}=", value) unless value.blank?
|
19
21
|
end
|
20
|
-
send("#{key}=", value) unless value.blank?
|
21
22
|
end
|
22
23
|
end
|
23
24
|
|
data/lib/mongoid/criteria.rb
CHANGED
@@ -100,7 +100,7 @@ module Mongoid #:nodoc:
|
|
100
100
|
#
|
101
101
|
# Returns: <tt>Integer</tt>
|
102
102
|
def count
|
103
|
-
@count ||= @klass.collection.find(@selector,
|
103
|
+
@count ||= @klass.collection.find(@selector, process_options).count
|
104
104
|
end
|
105
105
|
|
106
106
|
# Iterate over each +Document+ in the results. This can take an optional
|
@@ -228,7 +228,7 @@ module Mongoid #:nodoc:
|
|
228
228
|
#
|
229
229
|
# <tt>Criteria.select(:name).where(:name = "Chrissy").last</tt>
|
230
230
|
def last
|
231
|
-
opts =
|
231
|
+
opts = process_options
|
232
232
|
sorting = opts[:sort]
|
233
233
|
sorting = [[:_id, :asc]] unless sorting
|
234
234
|
opts[:sort] = sorting.collect { |option| [ option[0], option[1].invert ] }
|
@@ -253,6 +253,38 @@ module Mongoid #:nodoc:
|
|
253
253
|
@options[:limit] = value; self
|
254
254
|
end
|
255
255
|
|
256
|
+
MIN_REDUCE = "function(obj, prev) { if (prev.min == 'start') { prev.min = obj.[field]; } " +
|
257
|
+
"if (prev.min > obj.[field]) { prev.min = obj.[field]; } }"
|
258
|
+
# Return the min value for a field.
|
259
|
+
#
|
260
|
+
# This will take the internally built selector and options
|
261
|
+
# and pass them on to the Ruby driver's +group()+ method on the collection. The
|
262
|
+
# collection itself will be retrieved from the class provided, and once the
|
263
|
+
# query has returned it will provided a grouping of keys with sums.
|
264
|
+
#
|
265
|
+
# Example:
|
266
|
+
#
|
267
|
+
# <tt>criteria.min(:age)</tt>
|
268
|
+
def min(field)
|
269
|
+
grouped(:min, field.to_s, MIN_REDUCE)
|
270
|
+
end
|
271
|
+
|
272
|
+
MAX_REDUCE = "function(obj, prev) { if (prev.max == 'start') { prev.max = obj.[field]; } " +
|
273
|
+
"if (prev.max < obj.[field]) { prev.max = obj.[field]; } }"
|
274
|
+
# Return the max value for a field.
|
275
|
+
#
|
276
|
+
# This will take the internally built selector and options
|
277
|
+
# and pass them on to the Ruby driver's +group()+ method on the collection. The
|
278
|
+
# collection itself will be retrieved from the class provided, and once the
|
279
|
+
# query has returned it will provided a grouping of keys with sums.
|
280
|
+
#
|
281
|
+
# Example:
|
282
|
+
#
|
283
|
+
# <tt>criteria.max(:age)</tt>
|
284
|
+
def max(field)
|
285
|
+
grouped(:max, field.to_s, MAX_REDUCE)
|
286
|
+
end
|
287
|
+
|
256
288
|
# Merges another object into this +Criteria+. The other object may be a
|
257
289
|
# +Criteria+ or a +Hash+. This is used to combine multiple scopes together,
|
258
290
|
# where a chained scope situation may be desired.
|
@@ -340,7 +372,7 @@ module Mongoid #:nodoc:
|
|
340
372
|
#
|
341
373
|
# <tt>Criteria.select(:name).where(:name = "Chrissy").one</tt>
|
342
374
|
def one
|
343
|
-
attributes = @klass.collection.find_one(@selector,
|
375
|
+
attributes = @klass.collection.find_one(@selector, process_options)
|
344
376
|
attributes ? attributes["_type"].constantize.instantiate(attributes) : nil
|
345
377
|
end
|
346
378
|
|
@@ -421,7 +453,7 @@ module Mongoid #:nodoc:
|
|
421
453
|
@options[:skip] = value; self
|
422
454
|
end
|
423
455
|
|
424
|
-
SUM_REDUCE = "function(obj, prev) { prev.sum += obj.[field]; }"
|
456
|
+
SUM_REDUCE = "function(obj, prev) { if (prev.sum == 'start') { prev.sum = 0; } prev.sum += obj.[field]; }"
|
425
457
|
# Sum the criteria.
|
426
458
|
#
|
427
459
|
# This will take the internally built selector and options
|
@@ -436,36 +468,6 @@ module Mongoid #:nodoc:
|
|
436
468
|
grouped(:sum, field.to_s, SUM_REDUCE)
|
437
469
|
end
|
438
470
|
|
439
|
-
MIN_REDUCE = "function(obj, prev) { if (prev.max > obj.[field]) { prev.max = obj.[field]; } }"
|
440
|
-
# Return the min value for a field.
|
441
|
-
#
|
442
|
-
# This will take the internally built selector and options
|
443
|
-
# and pass them on to the Ruby driver's +group()+ method on the collection. The
|
444
|
-
# collection itself will be retrieved from the class provided, and once the
|
445
|
-
# query has returned it will provided a grouping of keys with sums.
|
446
|
-
#
|
447
|
-
# Example:
|
448
|
-
#
|
449
|
-
# <tt>criteria.min(:age)</tt>
|
450
|
-
def min(field)
|
451
|
-
grouped(:min, field.to_s, MIN_REDUCE)
|
452
|
-
end
|
453
|
-
|
454
|
-
MAX_REDUCE = "function(obj, prev) { if (prev.max < obj.[field]) { prev.max = obj.[field]; } }"
|
455
|
-
# Return the max value for a field.
|
456
|
-
#
|
457
|
-
# This will take the internally built selector and options
|
458
|
-
# and pass them on to the Ruby driver's +group()+ method on the collection. The
|
459
|
-
# collection itself will be retrieved from the class provided, and once the
|
460
|
-
# query has returned it will provided a grouping of keys with sums.
|
461
|
-
#
|
462
|
-
# Example:
|
463
|
-
#
|
464
|
-
# <tt>criteria.max(:age)</tt>
|
465
|
-
def max(field)
|
466
|
-
grouped(:max, field.to_s, MAX_REDUCE)
|
467
|
-
end
|
468
|
-
|
469
471
|
# Translate the supplied arguments into a +Criteria+ object.
|
470
472
|
#
|
471
473
|
# If the passed in args is a single +String+, then it will
|
@@ -535,7 +537,7 @@ module Mongoid #:nodoc:
|
|
535
537
|
# If this is a +Criteria+ to find multiple results, will return an +Array+ of
|
536
538
|
# objects of the type of class provided.
|
537
539
|
def execute
|
538
|
-
attributes = @klass.collection.find(@selector,
|
540
|
+
attributes = @klass.collection.find(@selector, process_options)
|
539
541
|
if attributes
|
540
542
|
@count = attributes.count
|
541
543
|
attributes.collect { |doc| doc["_type"].constantize.instantiate(doc) }
|
@@ -544,6 +546,17 @@ module Mongoid #:nodoc:
|
|
544
546
|
end
|
545
547
|
end
|
546
548
|
|
549
|
+
# Filters the field list. If no fields have been supplied, then it will be
|
550
|
+
# empty. If fields have been defined then _type will be included as well.
|
551
|
+
def process_options
|
552
|
+
fields = @options[:fields]
|
553
|
+
if fields && fields.size > 0 && !fields.include?(:_type)
|
554
|
+
fields << :_type
|
555
|
+
@options[:fields] = fields
|
556
|
+
end
|
557
|
+
@options.dup
|
558
|
+
end
|
559
|
+
|
547
560
|
# Filters the unused options out of the options +Hash+. Currently this
|
548
561
|
# takes into account the "page" and "per_page" options that would be passed
|
549
562
|
# in if using will_paginate.
|
@@ -556,23 +569,24 @@ module Mongoid #:nodoc:
|
|
556
569
|
end
|
557
570
|
end
|
558
571
|
|
559
|
-
# Update the selector setting the operator on the value for each key in the
|
560
|
-
# supplied attributes +Hash+.
|
561
|
-
def update_selector(attributes, operator)
|
562
|
-
attributes.each { |key, value| @selector[key] = { operator => value } }; self
|
563
|
-
end
|
564
|
-
|
565
572
|
# Common functionality for grouping operations. Currently used by min, max
|
566
573
|
# and sum. Will gsub the field name in the supplied reduce function.
|
567
574
|
def grouped(start, field, reduce)
|
568
575
|
collection = @klass.collection.group(
|
569
576
|
nil,
|
570
577
|
@selector,
|
571
|
-
{ start =>
|
578
|
+
{ start => "start" },
|
572
579
|
reduce.gsub("[field]", field),
|
573
580
|
true
|
574
581
|
)
|
575
582
|
collection.first[start.to_s]
|
576
583
|
end
|
584
|
+
|
585
|
+
# Update the selector setting the operator on the value for each key in the
|
586
|
+
# supplied attributes +Hash+.
|
587
|
+
def update_selector(attributes, operator)
|
588
|
+
attributes.each { |key, value| @selector[key] = { operator => value } }; self
|
589
|
+
end
|
590
|
+
|
577
591
|
end
|
578
592
|
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -28,7 +28,7 @@ module Mongoid #:nodoc:
|
|
28
28
|
attr_accessor :association_name, :_parent
|
29
29
|
attr_reader :attributes, :new_record
|
30
30
|
|
31
|
-
delegate :collection, :defaults, :embedded?, :fields, :primary_key, :to =>
|
31
|
+
delegate :collection, :defaults, :embedded?, :fields, :primary_key, :to => "self.class"
|
32
32
|
|
33
33
|
# Define all the callbacks that are accepted by the document.
|
34
34
|
define_callbacks :before_create, :before_destroy, :before_save, :before_update, :before_validation
|
@@ -348,11 +348,6 @@ module Mongoid #:nodoc:
|
|
348
348
|
@attributes[:_type] ||= self.class.name
|
349
349
|
end
|
350
350
|
|
351
|
-
# Convenience method to get the document's class
|
352
|
-
def klass
|
353
|
-
self.class
|
354
|
-
end
|
355
|
-
|
356
351
|
end
|
357
352
|
|
358
353
|
end
|
@@ -5,12 +5,12 @@ module Mongoid #:nodoc:
|
|
5
5
|
module Conversions #:nodoc:
|
6
6
|
def set(value)
|
7
7
|
return nil if value.blank?
|
8
|
-
time = ::Time.parse(value.to_s)
|
8
|
+
time = ::Time.parse(value.is_a?(::Time) ? value.strftime("%Y-%m-%d %H:%M:%S %Z") : value.to_s)
|
9
9
|
time.utc? ? time : time.utc
|
10
10
|
end
|
11
11
|
def get(value)
|
12
12
|
return nil if value.blank?
|
13
|
-
::Time.zone ?
|
13
|
+
::Time.zone ? value.getlocal : value
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
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.11.
|
8
|
+
s.version = "0.11.9"
|
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{2010-01-
|
12
|
+
s.date = %q{2010-01-07}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
@@ -14,8 +14,8 @@ describe Mongoid::Criteria do
|
|
14
14
|
Person.delete_all
|
15
15
|
end
|
16
16
|
|
17
|
-
it "provides
|
18
|
-
Person.
|
17
|
+
it "provides max for the field provided" do
|
18
|
+
Person.max(:age).should == 90.0
|
19
19
|
end
|
20
20
|
|
21
21
|
end
|
@@ -24,7 +24,7 @@ describe Mongoid::Criteria do
|
|
24
24
|
|
25
25
|
before do
|
26
26
|
10.times do |n|
|
27
|
-
Person.create(:title => "Sir", :age => (n * 10), :aliases => ["D", "Durran"])
|
27
|
+
Person.create(:title => "Sir", :age => ((n + 1) * 10), :aliases => ["D", "Durran"])
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
@@ -32,8 +32,8 @@ describe Mongoid::Criteria do
|
|
32
32
|
Person.delete_all
|
33
33
|
end
|
34
34
|
|
35
|
-
it "provides
|
36
|
-
Person.
|
35
|
+
it "provides min for the field provided" do
|
36
|
+
Person.min(:age).should == 10.0
|
37
37
|
end
|
38
38
|
|
39
39
|
end
|
@@ -50,7 +50,7 @@ describe Mongoid::Criteria do
|
|
50
50
|
Person.delete_all
|
51
51
|
end
|
52
52
|
|
53
|
-
it "provides
|
53
|
+
it "provides sum for the field provided" do
|
54
54
|
Person.where(:age.gt => 3).sum(:age).should == 50.0
|
55
55
|
end
|
56
56
|
|
@@ -193,6 +193,15 @@ describe Mongoid::Document do
|
|
193
193
|
|
194
194
|
end
|
195
195
|
|
196
|
+
context "limiting result fields" do
|
197
|
+
|
198
|
+
it "adds the type field to the options" do
|
199
|
+
people = Person.all(:fields => [ :title ])
|
200
|
+
people.first.title.should == "Test"
|
201
|
+
end
|
202
|
+
|
203
|
+
end
|
204
|
+
|
196
205
|
end
|
197
206
|
|
198
207
|
describe "#find_by_id" do
|
data/spec/spec_helper.rb
CHANGED
@@ -48,10 +48,11 @@ class Person
|
|
48
48
|
accepts_nested_attributes_for :addresses, :reject_if => lambda { |attrs| attrs["street"].blank? }
|
49
49
|
accepts_nested_attributes_for :name
|
50
50
|
|
51
|
-
index :
|
52
|
-
index :dob
|
51
|
+
index :age
|
53
52
|
index :addresses
|
53
|
+
index :dob
|
54
54
|
index :name
|
55
|
+
index :title
|
55
56
|
|
56
57
|
has_one_related :game
|
57
58
|
has_many_related :posts
|
@@ -111,20 +111,15 @@ describe Mongoid::Attributes do
|
|
111
111
|
context "when attribute is a string" do
|
112
112
|
|
113
113
|
it "adds a string field" do
|
114
|
-
@person.nofieldstring.should == "Testing"
|
115
|
-
@person.nofieldstring = "Test"
|
116
|
-
@person.nofieldstring.should == "Test"
|
114
|
+
@person.attributes[:nofieldstring].should == "Testing"
|
117
115
|
end
|
118
116
|
|
119
117
|
end
|
120
118
|
|
121
|
-
|
122
119
|
context "when attribute is not a string" do
|
123
120
|
|
124
121
|
it "adds a properly typed field" do
|
125
|
-
@person.nofieldint.should == 5
|
126
|
-
@person.nofieldint = 50
|
127
|
-
@person.nofieldint.should == 50
|
122
|
+
@person.attributes[:nofieldint].should == 5
|
128
123
|
end
|
129
124
|
|
130
125
|
end
|
@@ -495,7 +495,7 @@ describe Mongoid::Criteria do
|
|
495
495
|
describe "#max" do
|
496
496
|
|
497
497
|
before do
|
498
|
-
@reduce = "
|
498
|
+
@reduce = Mongoid::Criteria::MAX_REDUCE.gsub("[field]", "age")
|
499
499
|
@collection = mock
|
500
500
|
Person.expects(:collection).returns(@collection)
|
501
501
|
end
|
@@ -504,7 +504,7 @@ describe Mongoid::Criteria do
|
|
504
504
|
@collection.expects(:group).with(
|
505
505
|
nil,
|
506
506
|
{:_type => { "$in" => ["Doctor", "Person"] } },
|
507
|
-
{:max =>
|
507
|
+
{:max => "start"},
|
508
508
|
@reduce,
|
509
509
|
true
|
510
510
|
).returns([{"max" => 200.0}])
|
@@ -615,7 +615,7 @@ describe Mongoid::Criteria do
|
|
615
615
|
describe "#min" do
|
616
616
|
|
617
617
|
before do
|
618
|
-
@reduce = "
|
618
|
+
@reduce = Mongoid::Criteria::MIN_REDUCE.gsub("[field]", "age")
|
619
619
|
@collection = mock
|
620
620
|
Person.expects(:collection).returns(@collection)
|
621
621
|
end
|
@@ -624,7 +624,7 @@ describe Mongoid::Criteria do
|
|
624
624
|
@collection.expects(:group).with(
|
625
625
|
nil,
|
626
626
|
{:_type => { "$in" => ["Doctor", "Person"] } },
|
627
|
-
{:min =>
|
627
|
+
{:min => "start"},
|
628
628
|
@reduce,
|
629
629
|
true
|
630
630
|
).returns([{"min" => 4.0}])
|
@@ -891,7 +891,7 @@ describe Mongoid::Criteria do
|
|
891
891
|
context "when klass not provided" do
|
892
892
|
|
893
893
|
before do
|
894
|
-
@reduce = "
|
894
|
+
@reduce = Mongoid::Criteria::SUM_REDUCE.gsub("[field]", "age")
|
895
895
|
@collection = mock
|
896
896
|
Person.expects(:collection).returns(@collection)
|
897
897
|
end
|
@@ -900,7 +900,7 @@ describe Mongoid::Criteria do
|
|
900
900
|
@collection.expects(:group).with(
|
901
901
|
nil,
|
902
902
|
{:_type => { "$in" => ["Doctor", "Person"] } },
|
903
|
-
{:sum =>
|
903
|
+
{:sum => "start"},
|
904
904
|
@reduce,
|
905
905
|
true
|
906
906
|
).returns([{"sum" => 50.0}])
|
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.11.
|
4
|
+
version: 0.11.9
|
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: 2010-01-
|
12
|
+
date: 2010-01-07 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|