mongoid 0.8.5 → 0.8.6
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.rb +1 -1
- data/lib/mongoid/extensions/date/conversions.rb +1 -1
- data/lib/mongoid/extensions/hash/accessors.rb +4 -2
- data/mongoid.gemspec +1 -1
- data/spec/unit/mongoid/document_spec.rb +22 -0
- data/spec/unit/mongoid/extensions/date/conversions_spec.rb +38 -2
- data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +31 -0
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.8.
|
1
|
+
0.8.6
|
data/lib/mongoid/associations.rb
CHANGED
@@ -101,7 +101,7 @@ module Mongoid # :nodoc:
|
|
101
101
|
end
|
102
102
|
define_method("#{name}=") do |object|
|
103
103
|
proxy = Associations::Accessor.set(type, self, object, options)
|
104
|
-
|
104
|
+
remove_instance_variable("@#{name}") if instance_variable_defined?("@#{name}")
|
105
105
|
end
|
106
106
|
end
|
107
107
|
end
|
@@ -3,8 +3,10 @@ module Mongoid #:nodoc:
|
|
3
3
|
module Hash #:nodoc:
|
4
4
|
module Accessors #:nodoc:
|
5
5
|
def insert(key, attrs)
|
6
|
-
|
7
|
-
|
6
|
+
if key.singular?
|
7
|
+
self[key] = attrs unless self[key]
|
8
|
+
self[key] = self[key].merge(attrs) if self[key]
|
9
|
+
else
|
8
10
|
if elements = self[key]
|
9
11
|
elements.delete_if { |e| (e[:_id] == attrs[:_id]) } << attrs
|
10
12
|
else
|
data/mongoid.gemspec
CHANGED
@@ -551,6 +551,28 @@ describe Mongoid::Document do
|
|
551
551
|
|
552
552
|
end
|
553
553
|
|
554
|
+
context "on a parent document" do
|
555
|
+
|
556
|
+
context "when the parent has a has many through a has one" do
|
557
|
+
|
558
|
+
before do
|
559
|
+
@owner = PetOwner.new(:title => "Mr")
|
560
|
+
@pet = Pet.new(:name => "Fido")
|
561
|
+
@owner.pet = @pet
|
562
|
+
@vet_visit = VetVisit.new(:date => Date.today)
|
563
|
+
@pet.vet_visits = [@vet_visit]
|
564
|
+
end
|
565
|
+
|
566
|
+
it "does not overwrite child attributes if not in the hash" do
|
567
|
+
@owner.write_attributes({ :pet => { :name => "Bingo" } })
|
568
|
+
@owner.pet.name.should == "Bingo"
|
569
|
+
@owner.pet.vet_visits.size.should == 1
|
570
|
+
end
|
571
|
+
|
572
|
+
end
|
573
|
+
|
574
|
+
end
|
575
|
+
|
554
576
|
context "on a child document" do
|
555
577
|
|
556
578
|
context "when child is part of a has one" do
|
@@ -10,8 +10,20 @@ describe Mongoid::Extensions::Date::Conversions do
|
|
10
10
|
|
11
11
|
context "when string provided" do
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
context "when string is a utc time" do
|
14
|
+
|
15
|
+
it "returns a time from the string" do
|
16
|
+
Date.set(@time.utc.to_s).should == @time
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when string is a date" do
|
22
|
+
|
23
|
+
it "returns a time from the string" do
|
24
|
+
Date.set("01/15/2007").should == Date.new(2007, 1, 15).at_midnight
|
25
|
+
end
|
26
|
+
|
15
27
|
end
|
16
28
|
|
17
29
|
context "when string is empty" do
|
@@ -54,6 +66,30 @@ describe Mongoid::Extensions::Date::Conversions do
|
|
54
66
|
|
55
67
|
context "when value is not nil" do
|
56
68
|
|
69
|
+
context "when time is UTC" do
|
70
|
+
|
71
|
+
before do
|
72
|
+
@utc = Date.new(1974, 12, 1).to_time.utc
|
73
|
+
end
|
74
|
+
|
75
|
+
context "when time zone is not utc" do
|
76
|
+
|
77
|
+
before do
|
78
|
+
Time.zone = "Eastern Time (US & Canada)"
|
79
|
+
end
|
80
|
+
|
81
|
+
after do
|
82
|
+
Time.zone = "UTC"
|
83
|
+
end
|
84
|
+
|
85
|
+
it "converts to the proper date" do
|
86
|
+
Date.get(@utc).should == Date.new(1974, 12, 1)
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
57
93
|
it "converts the time back to a date" do
|
58
94
|
Date.get(@time).should == @time.to_date
|
59
95
|
end
|
@@ -11,6 +11,11 @@ describe Mongoid::Extensions::Hash::Accessors do
|
|
11
11
|
:name => {
|
12
12
|
:_id => 2, :first_name => "Test", :last_name => "User"
|
13
13
|
},
|
14
|
+
:pet => {
|
15
|
+
:_id => 6,
|
16
|
+
:name => "Fido",
|
17
|
+
:vet_visits => [ { :date => Date.new(2007, 1, 1) } ]
|
18
|
+
},
|
14
19
|
:addresses => [
|
15
20
|
{ :_id => 3, :street => "First Street" },
|
16
21
|
{ :_id => 4, :street => "Second Street" }
|
@@ -18,6 +23,32 @@ describe Mongoid::Extensions::Hash::Accessors do
|
|
18
23
|
}
|
19
24
|
end
|
20
25
|
|
26
|
+
context "when writing to a hash with a child array" do
|
27
|
+
|
28
|
+
context "when child attributes not present" do
|
29
|
+
|
30
|
+
it "does nothing to the children" do
|
31
|
+
@hash.insert(:pet, { :name => "Bingo" })
|
32
|
+
@hash[:pet].should == {
|
33
|
+
:_id => 6,
|
34
|
+
:name => "Bingo",
|
35
|
+
:vet_visits => [ { :date => Date.new(2007, 1, 1) } ]
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when child attributes present" do
|
42
|
+
|
43
|
+
it "overwrites the child attributes" do
|
44
|
+
@hash.insert(:pet, { :vet_visits => [ { :date => Date.new(2009, 11, 11) } ] })
|
45
|
+
@hash[:pet][:vet_visits].should == [ { :date => Date.new(2009, 11, 11) } ]
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
21
52
|
context "when writing a single attribute" do
|
22
53
|
|
23
54
|
context "when attribute exists" do
|