mongoid 0.6.2 → 0.6.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongoid/associations/belongs_to_association.rb +0 -4
- data/lib/mongoid/associations/decorator.rb +4 -1
- data/lib/mongoid/associations/has_one_association.rb +0 -4
- data/lib/mongoid/document.rb +7 -2
- data/lib/mongoid/extensions/boolean/conversions.rb +1 -1
- data/lib/mongoid/extensions/date/conversions.rb +1 -1
- data/lib/mongoid/extensions/object/conversions.rb +1 -1
- data/lib/mongoid/field.rb +1 -1
- data/mongoid.gemspec +1 -1
- data/spec/integration/mongoid/document_spec.rb +1 -1
- data/spec/unit/mongoid/associations/belongs_to_association_spec.rb +1 -1
- data/spec/unit/mongoid/associations/decorator_spec.rb +2 -2
- data/spec/unit/mongoid/associations_spec.rb +1 -0
- data/spec/unit/mongoid/commands_spec.rb +1 -1
- data/spec/unit/mongoid/criteria_spec.rb +3 -3
- data/spec/unit/mongoid/document_spec.rb +30 -4
- data/spec/unit/mongoid/extensions/date/conversions_spec.rb +14 -2
- data/spec/unit/mongoid/extensions/object/conversions_spec.rb +10 -6
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.3
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module Mongoid #:nodoc:
|
2
2
|
module Associations #:nodoc:
|
3
3
|
module Decorator #:nodoc:
|
4
|
+
EXTRAS = [:save, :save!, :==]
|
5
|
+
|
4
6
|
def self.included(base)
|
5
7
|
base.class_eval do
|
6
8
|
attr_reader :document
|
@@ -10,7 +12,8 @@ module Mongoid #:nodoc:
|
|
10
12
|
# since we can ask the class for its methods and get an
|
11
13
|
# accurate list.
|
12
14
|
def decorate!
|
13
|
-
document.public_methods(false)
|
15
|
+
meths = document.public_methods(false) + EXTRAS
|
16
|
+
meths.each do |method|
|
14
17
|
(class << self; self; end).class_eval do
|
15
18
|
define_method method do |*args|
|
16
19
|
document.send method, *args
|
data/lib/mongoid/document.rb
CHANGED
@@ -117,6 +117,11 @@ module Mongoid #:nodoc:
|
|
117
117
|
before_save :generate_key
|
118
118
|
end
|
119
119
|
|
120
|
+
# Find the last +Document+ in the collection by reverse id
|
121
|
+
def last
|
122
|
+
find(:first, :conditions => {}, :sort => [[:_id, :asc]])
|
123
|
+
end
|
124
|
+
|
120
125
|
# Returns the primary key field of the +Document+
|
121
126
|
def primary_key
|
122
127
|
@primary_key
|
@@ -217,7 +222,7 @@ module Mongoid #:nodoc:
|
|
217
222
|
|
218
223
|
# Reloads the +Document+ attributes from the database.
|
219
224
|
def reload
|
220
|
-
@attributes = HashWithIndifferentAccess.new(collection.find_one(id))
|
225
|
+
@attributes = HashWithIndifferentAccess.new(collection.find_one(:_id => id))
|
221
226
|
end
|
222
227
|
|
223
228
|
# Returns the id of the Document
|
@@ -252,7 +257,7 @@ module Mongoid #:nodoc:
|
|
252
257
|
values = primary_key.collect { |key| @attributes[key] }
|
253
258
|
@attributes[:_id] = values.join(" ").parameterize.to_s
|
254
259
|
else
|
255
|
-
@attributes[:_id] = Mongo::ObjectID.new unless id
|
260
|
+
@attributes[:_id] = Mongo::ObjectID.new.to_s unless id
|
256
261
|
end
|
257
262
|
end
|
258
263
|
end
|
data/lib/mongoid/field.rb
CHANGED
@@ -26,7 +26,7 @@ module Mongoid #:nodoc:
|
|
26
26
|
# Used for setting an object in the attributes hash. If nil is provided the
|
27
27
|
# default will get returned if it exists.
|
28
28
|
def set(object)
|
29
|
-
object ? type.set(object)
|
29
|
+
object.nil? ? default : type.set(object)
|
30
30
|
end
|
31
31
|
|
32
32
|
# Used for retrieving the object out of the attributes hash.
|
data/mongoid.gemspec
CHANGED
@@ -62,7 +62,7 @@ describe Mongoid::Document do
|
|
62
62
|
|
63
63
|
it "persists a new record to the database" do
|
64
64
|
person = Person.create(:test => "Test")
|
65
|
-
person.id.should be_a_kind_of(
|
65
|
+
person.id.should be_a_kind_of(String)
|
66
66
|
person.attributes[:test].should == "Test"
|
67
67
|
end
|
68
68
|
|
@@ -23,7 +23,7 @@ describe Mongoid::Associations::BelongsToAssociation do
|
|
23
23
|
context "when finding by id" do
|
24
24
|
|
25
25
|
it "returns the document in the array with that id" do
|
26
|
-
name = @association.find(Mongo::ObjectID.new)
|
26
|
+
name = @association.find(Mongo::ObjectID.new.to_s)
|
27
27
|
name.should == @parent
|
28
28
|
end
|
29
29
|
|
@@ -55,9 +55,9 @@ describe Mongoid::Associations::Decorator do
|
|
55
55
|
@decorated = Decorated.new(@person)
|
56
56
|
end
|
57
57
|
|
58
|
-
it "adds all the documents
|
58
|
+
it "adds all the documents methods to the class" do
|
59
59
|
@decorated.decorate!
|
60
|
-
@decorated.should respond_to(:title, :terms, :age, :addresses, :name)
|
60
|
+
@decorated.should respond_to(:title, :terms, :age, :addresses, :name, :save)
|
61
61
|
end
|
62
62
|
|
63
63
|
end
|
@@ -195,13 +195,13 @@ describe Mongoid::Criteria do
|
|
195
195
|
describe "#id" do
|
196
196
|
|
197
197
|
it "adds the _id query to the selector" do
|
198
|
-
id = Mongo::ObjectID.new
|
198
|
+
id = Mongo::ObjectID.new.to_s
|
199
199
|
@criteria.id(id)
|
200
200
|
@criteria.selector.should == { :_id => id }
|
201
201
|
end
|
202
202
|
|
203
203
|
it "returns self" do
|
204
|
-
id = Mongo::ObjectID.new
|
204
|
+
id = Mongo::ObjectID.new.to_s
|
205
205
|
@criteria.id(id.to_s).should == @criteria
|
206
206
|
end
|
207
207
|
|
@@ -413,7 +413,7 @@ describe Mongoid::Criteria do
|
|
413
413
|
context "with a single argument" do
|
414
414
|
|
415
415
|
it "creates a criteria for an object id" do
|
416
|
-
id = Mongo::ObjectID.new
|
416
|
+
id = Mongo::ObjectID.new.to_s
|
417
417
|
criteria = Mongoid::Criteria.translate(id)
|
418
418
|
criteria.selector.should == { :_id => id }
|
419
419
|
end
|
@@ -208,7 +208,7 @@ describe Mongoid::Document do
|
|
208
208
|
context "when an id is passed in" do
|
209
209
|
|
210
210
|
before do
|
211
|
-
@id = Mongo::ObjectID.new
|
211
|
+
@id = Mongo::ObjectID.new.to_s
|
212
212
|
end
|
213
213
|
|
214
214
|
it "delegates to criteria" do
|
@@ -344,6 +344,19 @@ describe Mongoid::Document do
|
|
344
344
|
|
345
345
|
end
|
346
346
|
|
347
|
+
describe "#last" do
|
348
|
+
|
349
|
+
before do
|
350
|
+
@attributes = { :_id => 1, :title => "Sir" }
|
351
|
+
@collection.expects(:find_one).with({}, :sort => [[:_id, :asc]]).returns(@attributes)
|
352
|
+
end
|
353
|
+
|
354
|
+
it "finds the last document by the id" do
|
355
|
+
Person.last.should == Person.new(@attributes)
|
356
|
+
end
|
357
|
+
|
358
|
+
end
|
359
|
+
|
347
360
|
describe "#new" do
|
348
361
|
|
349
362
|
context "with no attributes" do
|
@@ -513,8 +526,8 @@ describe Mongoid::Document do
|
|
513
526
|
|
514
527
|
before do
|
515
528
|
@attributes = { "title" => "Herr" }
|
516
|
-
@person = Person.new(:_id => Mongo::ObjectID.new)
|
517
|
-
@collection.expects(:find_one).with(@person.id).returns(@attributes)
|
529
|
+
@person = Person.new(:_id => Mongo::ObjectID.new.to_s)
|
530
|
+
@collection.expects(:find_one).with(:_id => @person.id).returns(@attributes)
|
518
531
|
end
|
519
532
|
|
520
533
|
it "reloads the object attribtues from the database" do
|
@@ -536,7 +549,7 @@ describe Mongoid::Document do
|
|
536
549
|
describe "#to_param" do
|
537
550
|
|
538
551
|
it "returns the id" do
|
539
|
-
id = Mongo::ObjectID.new
|
552
|
+
id = Mongo::ObjectID.new.to_s
|
540
553
|
Person.new(:_id => id).to_param.should == id.to_s
|
541
554
|
end
|
542
555
|
|
@@ -557,6 +570,19 @@ describe Mongoid::Document do
|
|
557
570
|
|
558
571
|
end
|
559
572
|
|
573
|
+
context "when field has a default value" do
|
574
|
+
|
575
|
+
before do
|
576
|
+
@person = Person.new
|
577
|
+
end
|
578
|
+
|
579
|
+
it "should allow overwriting of the default value" do
|
580
|
+
@person.terms = true
|
581
|
+
@person.terms.should be_true
|
582
|
+
end
|
583
|
+
|
584
|
+
end
|
585
|
+
|
560
586
|
end
|
561
587
|
|
562
588
|
describe "#write_attributes" do
|
@@ -36,8 +36,20 @@ describe Mongoid::Extensions::Date::Conversions do
|
|
36
36
|
|
37
37
|
describe "#get" do
|
38
38
|
|
39
|
-
|
40
|
-
|
39
|
+
context "when value is nil" do
|
40
|
+
|
41
|
+
it "returns nil" do
|
42
|
+
Date.get(nil).should be_nil
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
context "when value is not nil" do
|
48
|
+
|
49
|
+
it "converts the time back to a date" do
|
50
|
+
Date.get(@time).should == @time.to_date
|
51
|
+
end
|
52
|
+
|
41
53
|
end
|
42
54
|
|
43
55
|
end
|
@@ -30,13 +30,17 @@ describe Mongoid::Extensions::Object::Conversions do
|
|
30
30
|
|
31
31
|
describe "#set" do
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
context "when object has attributes" do
|
34
|
+
|
35
|
+
before do
|
36
|
+
@attributes = { "_id" => "test", "title" => "Sir", "age" => 100 }
|
37
|
+
@person = Person.new(@attributes)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "converts the object to a hash" do
|
41
|
+
Person.set(@person).should == @attributes
|
42
|
+
end
|
37
43
|
|
38
|
-
it "converts the object to a hash" do
|
39
|
-
Person.set(@person).should == @attributes
|
40
44
|
end
|
41
45
|
|
42
46
|
end
|