andrewtimberlake-couch_potato 0.2.8.1 → 0.2.8.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -19,9 +19,20 @@ module CouchPotato
|
|
19
19
|
|
20
20
|
def reset_dirty_attributes
|
21
21
|
self.class.properties.each do |property|
|
22
|
-
instance_variable_set("@#{property.name}_was", send(property.name))
|
22
|
+
instance_variable_set("@#{property.name}_was", clone_attribute(send(property.name)))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def clone_attribute(value)
|
27
|
+
if [Fixnum, Symbol, TrueClass, FalseClass, NilClass, Float].include?(value.class)
|
28
|
+
value
|
29
|
+
elsif [Hash, Array].include?(value.class)
|
30
|
+
#Deep clone
|
31
|
+
Marshal::load(Marshal::dump(value))
|
32
|
+
else
|
33
|
+
value.clone
|
23
34
|
end
|
24
35
|
end
|
25
36
|
end
|
26
37
|
end
|
27
|
-
end
|
38
|
+
end
|
@@ -20,14 +20,6 @@ module CouchPotato
|
|
20
20
|
end if attributes
|
21
21
|
end
|
22
22
|
private :assign_attribute_copies_for_dirty_tracking
|
23
|
-
|
24
|
-
def clone_attribute(value)
|
25
|
-
if [Fixnum, Symbol, TrueClass, FalseClass, NilClass, Float].include?(value.class)
|
26
|
-
value
|
27
|
-
else
|
28
|
-
value.clone
|
29
|
-
end
|
30
|
-
end
|
31
23
|
|
32
24
|
define_method "#{name}" do
|
33
25
|
value = self.instance_variable_get("@#{name}")
|
@@ -33,6 +33,24 @@ describe 'dirty attribute tracking' do
|
|
33
33
|
@couchrest_db.should_receive(:save_doc)
|
34
34
|
@db.save_document(plate)
|
35
35
|
end
|
36
|
+
|
37
|
+
it "should correctly track dirty hashes (deep clone)" do
|
38
|
+
plate = Plate.new :food => {:veggies => ['carrots', 'peas']}
|
39
|
+
@db.save_document(plate)
|
40
|
+
plate.food[:veggies] << 'beans'
|
41
|
+
@couchrest_db.should_receive(:save_doc)
|
42
|
+
@db.save_document(plate)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should correctly track dirty hashes (deep clone) after a save" do
|
46
|
+
plate = Plate.new :food => {:veggies => ['carrots', 'peas']}
|
47
|
+
@db.save_document(plate)
|
48
|
+
plate.food[:veggies] << 'beans'
|
49
|
+
@db.save_document(plate)
|
50
|
+
plate.food[:veggies] << 'cauliflower'
|
51
|
+
@couchrest_db.should_receive(:save_doc)
|
52
|
+
@db.save_document(plate)
|
53
|
+
end
|
36
54
|
end
|
37
55
|
|
38
56
|
describe "newly created object" do
|
@@ -110,4 +128,4 @@ describe 'dirty attribute tracking' do
|
|
110
128
|
end
|
111
129
|
end
|
112
130
|
|
113
|
-
end
|
131
|
+
end
|