aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/dirty_test.rb
blob: 0edbbffa86abebbca29b277b0a6c81583c583479 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# frozen_string_literal: true

require "cases/helper"

class DirtyTest < ActiveModel::TestCase
  class DirtyModel
    include ActiveModel::Dirty
    define_attribute_methods :name, :color, :size, :status

    def initialize
      @name = nil
      @color = nil
      @size = nil
      @status = "initialized"
    end

    attr_reader :name, :color, :size, :status

    def name=(val)
      name_will_change!
      @name = val
    end

    def color=(val)
      color_will_change! unless val == @color
      @color = val
    end

    def size=(val)
      attribute_will_change!(:size) unless val == @size
      @size = val
    end

    def status=(val)
      status_will_change! unless val == @status
      @status = val
    end

    def save
      changes_applied
    end

    def reload
      clear_changes_information
    end
  end

  setup do
    @model = DirtyModel.new
  end

  test "setting attribute will result in change" do
    assert_not_predicate @model, :changed?
    assert_not_predicate @model, :name_changed?
    @model.name = "Ringo"
    assert_predicate @model, :changed?
    assert_predicate @model, :name_changed?
  end

  test "list of changed attribute keys" do
    assert_equal [], @model.changed
    @model.name = "Paul"
    assert_equal ["name"], @model.changed
  end

  test "changes to attribute values" do
    assert_not @model.changes["name"]
    @model.name = "John"
    assert_equal [nil, "John"], @model.changes["name"]
  end

  test "checking if an attribute has changed to a particular value" do
    @model.name = "Ringo"
    assert @model.name_changed?(from: nil, to: "Ringo")
    assert_not @model.name_changed?(from: "Pete", to: "Ringo")
    assert @model.name_changed?(to: "Ringo")
    assert_not @model.name_changed?(to: "Pete")
    assert @model.name_changed?(from: nil)
    assert_not @model.name_changed?(from: "Pete")
  end

  test "changes accessible through both strings and symbols" do
    @model.name = "David"
    assert_not_nil @model.changes[:name]
    assert_not_nil @model.changes["name"]
  end

  test "be consistent with symbols arguments after the changes are applied" do
    @model.name = "David"
    assert @model.attribute_changed?(:name)
    @model.save
    @model.name = "Rafael"
    assert @model.attribute_changed?(:name)
  end

  test "attribute mutation" do
    @model.instance_variable_set("@name", +"Yam")
    assert_not_predicate @model, :name_changed?
    @model.name.replace("Hadad")
    assert_not_predicate @model, :name_changed?
    @model.name_will_change!
    @model.name.replace("Baal")
    assert_predicate @model, :name_changed?
  end

  test "resetting attribute" do
    @model.name = "Bob"
    @model.restore_name!
    assert_nil @model.name
    assert_not_predicate @model, :name_changed?
  end

  test "setting color to same value should not result in change being recorded" do
    @model.color = "red"
    assert_predicate @model, :color_changed?
    @model.save
    assert_not_predicate @model, :color_changed?
    assert_not_predicate @model, :changed?
    @model.color = "red"
    assert_not_predicate @model, :color_changed?
    assert_not_predicate @model, :changed?
  end

  test "saving should reset model's changed status" do
    @model.name = "Alf"
    assert_predicate @model, :changed?
    @model.save
    assert_not_predicate @model, :changed?
    assert_not_predicate @model, :name_changed?
  end

  test "saving should preserve previous changes" do
    @model.name = "Jericho Cane"
    @model.status = "waiting"
    @model.save
    assert_equal [nil, "Jericho Cane"], @model.previous_changes["name"]
    assert_equal ["initialized", "waiting"], @model.previous_changes["status"]
  end

  test "setting new attributes should not affect previous changes" do
    @model.name = "Jericho Cane"
    @model.status = "waiting"
    @model.save
    @model.name = "DudeFella ManGuy"
    @model.status = "finished"
    assert_equal [nil, "Jericho Cane"], @model.name_previous_change
    assert_equal ["initialized", "waiting"], @model.previous_changes["status"]
  end

  test "saving should preserve model's previous changed status" do
    @model.name = "Jericho Cane"
    @model.save
    assert_predicate @model, :name_previously_changed?
  end

  test "previous value is preserved when changed after save" do
    assert_equal({}, @model.changed_attributes)
    @model.name = "Paul"
    @model.status = "waiting"
    assert_equal({ "name" => nil, "status" => "initialized" }, @model.changed_attributes)

    @model.save

    @model.name = "John"
    @model.status = "finished"
    assert_equal({ "name" => "Paul", "status" => "waiting" }, @model.changed_attributes)
  end

  test "changing the same attribute multiple times retains the correct original value" do
    @model.name = "Otto"
    @model.status = "waiting"
    @model.save
    @model.name = "DudeFella ManGuy"
    @model.name = "Mr. Manfredgensonton"
    @model.status = "processing"
    @model.status = "finished"
    assert_equal ["Otto", "Mr. Manfredgensonton"], @model.name_change
    assert_equal ["waiting", "finished"], @model.status_change
    assert_equal @model.name_was, "Otto"
  end

  test "using attribute_will_change! with a symbol" do
    @model.size = 1
    assert_predicate @model, :size_changed?
  end

  test "reload should reset all changes" do
    @model.name = "Dmitry"
    @model.name_changed?
    @model.save
    @model.name = "Bob"

    assert_equal [nil, "Dmitry"], @model.previous_changes["name"]
    assert_equal "Dmitry", @model.changed_attributes["name"]

    @model.reload

    assert_equal ActiveSupport::HashWithIndifferentAccess.new, @model.previous_changes
    assert_equal ActiveSupport::HashWithIndifferentAccess.new, @model.changed_attributes
  end

  test "restore_attributes should restore all previous data" do
    @model.name = "Dmitry"
    @model.color = "Red"
    @model.save
    @model.name = "Bob"
    @model.color = "White"

    @model.restore_attributes

    assert_not_predicate @model, :changed?
    assert_equal "Dmitry", @model.name
    assert_equal "Red", @model.color
  end

  test "restore_attributes can restore only some attributes" do
    @model.name = "Dmitry"
    @model.color = "Red"
    @model.save
    @model.name = "Bob"
    @model.color = "White"

    @model.restore_attributes(["name"])

    assert_predicate @model, :changed?
    assert_equal "Dmitry", @model.name
    assert_equal "White", @model.color
  end

  test "model can be dup-ed without Attributes" do
    assert @model.dup
  end
end