Skip to content
This repository
Browse code

Remove or fix non-working examples and add a few tests to Dirty [#5185

…state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
commit 2c8a4a53a8c38a43a62342b9d46014242e781d18 1 parent fb2b8fe
Tore Darell authored August 02, 2010 josevalim committed August 03, 2010
9  activemodel/lib/active_model/dirty.rb
@@ -37,12 +37,13 @@ module ActiveModel
37 37
   #     end
38 38
   #   
39 39
   #     def name=(val)
40  
-  #       name_will_change!
  40
+  #       name_will_change! unless val == @name
41 41
   #       @name = val
42 42
   #     end
43 43
   #   
44 44
   #     def save
45 45
   #       @previously_changed = changes
  46
+  #       @changed_attributes.clear
46 47
   #     end
47 48
   #   
48 49
   #   end
@@ -77,12 +78,6 @@ module ActiveModel
77 78
   #   person.changed        # => ['name']
78 79
   #   person.changes        # => { 'name' => ['Bill', 'Bob'] }
79 80
   #
80  
-  # Resetting an attribute returns it to its original state:
81  
-  #   person.reset_name!    # => 'Bill'
82  
-  #   person.changed?       # => false
83  
-  #   person.name_changed?  # => false
84  
-  #   person.name           # => 'Bill'
85  
-  #
86 81
   # If an attribute is modified in-place then make use of <tt>[attribute_name]_will_change!</tt>
87 82
   # to mark that the attribute is changing. Otherwise ActiveModel can't track changes to 
88 83
   # in-place attributes.
90  activemodel/test/cases/dirty_test.rb
@@ -3,10 +3,11 @@
3 3
 class DirtyTest < ActiveModel::TestCase
4 4
   class DirtyModel
5 5
     include ActiveModel::Dirty
6  
-    define_attribute_methods [:name]
  6
+    define_attribute_methods [:name, :color]
7 7
 
8 8
     def initialize
9 9
       @name = nil
  10
+      @color = nil
10 11
     end
11 12
 
12 13
     def name
@@ -17,13 +18,92 @@ def name=(val)
17 18
       name_will_change!
18 19
       @name = val
19 20
     end
  21
+
  22
+    def color
  23
+      @color
  24
+    end
  25
+
  26
+    def color=(val)
  27
+      color_will_change! unless val == @color
  28
+      @color = val
  29
+    end
  30
+
  31
+    def save
  32
+      @previously_changed = changes
  33
+      @changed_attributes.clear
  34
+    end
  35
+  end
  36
+
  37
+  setup do
  38
+    @model = DirtyModel.new
  39
+  end
  40
+
  41
+  test "setting attribute will result in change" do
  42
+    assert !@model.changed?
  43
+    assert !@model.name_changed?
  44
+    @model.name = "Ringo"
  45
+    assert @model.changed?
  46
+    assert @model.name_changed?
  47
+  end
  48
+
  49
+  test "list of changed attributes" do
  50
+    assert_equal [], @model.changed
  51
+    @model.name = "Paul"
  52
+    assert_equal ['name'], @model.changed
  53
+  end
  54
+
  55
+  test "changes to attribute values" do
  56
+    assert !@model.changes['name']
  57
+    @model.name = "John"
  58
+    assert_equal [nil, "John"], @model.changes['name']
20 59
   end
21 60
 
22 61
   test "changes accessible through both strings and symbols" do
23  
-    model = DirtyModel.new
24  
-    model.name = "David"
25  
-    assert_not_nil model.changes[:name]
26  
-    assert_not_nil model.changes['name']
  62
+    @model.name = "David"
  63
+    assert_not_nil @model.changes[:name]
  64
+    assert_not_nil @model.changes['name']
  65
+  end
  66
+
  67
+  test "attribute mutation" do
  68
+    @model.instance_variable_set("@name", "Yam")
  69
+    assert !@model.name_changed?
  70
+    @model.name.replace("Hadad")
  71
+    assert !@model.name_changed?
  72
+    @model.name_will_change!
  73
+    @model.name.replace("Baal")
  74
+    assert @model.name_changed?
  75
+  end
  76
+
  77
+  test "resetting attribute" do
  78
+    @model.name = "Bob"
  79
+    @model.reset_name!
  80
+    assert_nil @model.name
  81
+    #assert !@model.name_changed #Doesn't work yet
  82
+  end
  83
+
  84
+  test "setting color to same value should not result in change being recorded" do
  85
+    @model.color = "red"
  86
+    assert @model.color_changed?
  87
+    @model.save
  88
+    assert !@model.color_changed?
  89
+    assert !@model.changed?
  90
+    @model.color = "red"
  91
+    assert !@model.color_changed?
  92
+    assert !@model.changed?
  93
+  end
  94
+
  95
+  test "saving should reset model's changed status" do
  96
+    @model.name = "Alf"
  97
+    assert @model.changed?
  98
+    @model.save
  99
+    assert !@model.changed?
  100
+    assert !@model.name_changed?
  101
+  end
  102
+
  103
+  test "saving should preserve previous changes" do
  104
+    @model.name = "Jericho Cane"
  105
+    @model.save
  106
+    assert_equal [nil, "Jericho Cane"], @model.previous_changes['name']
27 107
   end
28 108
 
29 109
 end

0 notes on commit 2c8a4a5

Please sign in to comment.
Something went wrong with that request. Please try again.