2010-03-12 04:22:29 -05:00
|
|
|
require "cases/helper"
|
|
|
|
|
|
|
|
class DirtyTest < ActiveModel::TestCase
|
|
|
|
class DirtyModel
|
|
|
|
include ActiveModel::Dirty
|
2013-01-07 00:04:35 -05:00
|
|
|
define_attribute_methods :name, :color, :size
|
2010-03-12 04:22:29 -05:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
@name = nil
|
2010-08-02 15:15:44 -04:00
|
|
|
@color = nil
|
2013-01-07 00:04:35 -05:00
|
|
|
@size = nil
|
2010-03-12 04:22:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
@name
|
|
|
|
end
|
|
|
|
|
|
|
|
def name=(val)
|
|
|
|
name_will_change!
|
|
|
|
@name = val
|
|
|
|
end
|
2010-08-02 15:15:44 -04:00
|
|
|
|
|
|
|
def color
|
|
|
|
@color
|
|
|
|
end
|
|
|
|
|
|
|
|
def color=(val)
|
|
|
|
color_will_change! unless val == @color
|
|
|
|
@color = val
|
|
|
|
end
|
|
|
|
|
2013-01-07 00:04:35 -05:00
|
|
|
def size
|
|
|
|
@size
|
|
|
|
end
|
|
|
|
|
|
|
|
def size=(val)
|
|
|
|
attribute_will_change!(:size) unless val == @size
|
|
|
|
@size = val
|
|
|
|
end
|
|
|
|
|
2010-08-02 15:15:44 -04:00
|
|
|
def save
|
2013-09-23 09:59:05 -04:00
|
|
|
changes_applied
|
2010-08-02 15:15:44 -04:00
|
|
|
end
|
2014-02-08 20:34:14 -05:00
|
|
|
|
|
|
|
def reload
|
2014-07-15 15:00:50 -04:00
|
|
|
clear_changes_information
|
|
|
|
end
|
2010-08-02 15:15:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
|
|
|
@model = DirtyModel.new
|
|
|
|
end
|
|
|
|
|
|
|
|
test "setting attribute will result in change" do
|
|
|
|
assert !@model.changed?
|
|
|
|
assert !@model.name_changed?
|
|
|
|
@model.name = "Ringo"
|
|
|
|
assert @model.changed?
|
|
|
|
assert @model.name_changed?
|
|
|
|
end
|
|
|
|
|
2013-01-14 15:03:42 -05:00
|
|
|
test "list of changed attribute keys" do
|
2010-08-02 15:15:44 -04:00
|
|
|
assert_equal [], @model.changed
|
|
|
|
@model.name = "Paul"
|
|
|
|
assert_equal ['name'], @model.changed
|
|
|
|
end
|
|
|
|
|
|
|
|
test "changes to attribute values" do
|
|
|
|
assert !@model.changes['name']
|
|
|
|
@model.name = "John"
|
|
|
|
assert_equal [nil, "John"], @model.changes['name']
|
2010-03-12 04:22:29 -05:00
|
|
|
end
|
|
|
|
|
2013-12-01 23:50:43 -05:00
|
|
|
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
|
|
|
|
|
2010-03-12 04:22:29 -05:00
|
|
|
test "changes accessible through both strings and symbols" do
|
2010-08-02 15:15:44 -04:00
|
|
|
@model.name = "David"
|
|
|
|
assert_not_nil @model.changes[:name]
|
|
|
|
assert_not_nil @model.changes['name']
|
|
|
|
end
|
|
|
|
|
2014-01-21 19:38:07 -05:00
|
|
|
test "be consistent with symbols arguments after the changes are applied" do
|
2014-01-20 19:58:14 -05:00
|
|
|
@model.name = "David"
|
|
|
|
assert @model.attribute_changed?(:name)
|
|
|
|
@model.save
|
|
|
|
@model.name = 'Rafael'
|
|
|
|
assert @model.attribute_changed?(:name)
|
|
|
|
end
|
|
|
|
|
2010-08-02 15:15:44 -04:00
|
|
|
test "attribute mutation" do
|
|
|
|
@model.instance_variable_set("@name", "Yam")
|
|
|
|
assert !@model.name_changed?
|
|
|
|
@model.name.replace("Hadad")
|
|
|
|
assert !@model.name_changed?
|
|
|
|
@model.name_will_change!
|
|
|
|
@model.name.replace("Baal")
|
|
|
|
assert @model.name_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "resetting attribute" do
|
|
|
|
@model.name = "Bob"
|
2014-07-15 15:12:23 -04:00
|
|
|
@model.restore_name!
|
2010-08-02 15:15:44 -04:00
|
|
|
assert_nil @model.name
|
2012-12-01 12:29:49 -05:00
|
|
|
assert !@model.name_changed?
|
2010-08-02 15:15:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "setting color to same value should not result in change being recorded" do
|
|
|
|
@model.color = "red"
|
|
|
|
assert @model.color_changed?
|
|
|
|
@model.save
|
|
|
|
assert !@model.color_changed?
|
|
|
|
assert !@model.changed?
|
|
|
|
@model.color = "red"
|
|
|
|
assert !@model.color_changed?
|
|
|
|
assert !@model.changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "saving should reset model's changed status" do
|
|
|
|
@model.name = "Alf"
|
|
|
|
assert @model.changed?
|
|
|
|
@model.save
|
|
|
|
assert !@model.changed?
|
|
|
|
assert !@model.name_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
test "saving should preserve previous changes" do
|
|
|
|
@model.name = "Jericho Cane"
|
|
|
|
@model.save
|
|
|
|
assert_equal [nil, "Jericho Cane"], @model.previous_changes['name']
|
2010-03-12 04:22:29 -05:00
|
|
|
end
|
|
|
|
|
2013-01-14 15:03:42 -05:00
|
|
|
test "previous value is preserved when changed after save" do
|
|
|
|
assert_equal({}, @model.changed_attributes)
|
|
|
|
@model.name = "Paul"
|
|
|
|
assert_equal({ "name" => nil }, @model.changed_attributes)
|
|
|
|
|
|
|
|
@model.save
|
|
|
|
|
|
|
|
@model.name = "John"
|
|
|
|
assert_equal({ "name" => "Paul" }, @model.changed_attributes)
|
|
|
|
end
|
|
|
|
|
2011-06-29 01:18:24 -04:00
|
|
|
test "changing the same attribute multiple times retains the correct original value" do
|
|
|
|
@model.name = "Otto"
|
|
|
|
@model.save
|
|
|
|
@model.name = "DudeFella ManGuy"
|
|
|
|
@model.name = "Mr. Manfredgensonton"
|
|
|
|
assert_equal ["Otto", "Mr. Manfredgensonton"], @model.name_change
|
|
|
|
assert_equal @model.name_was, "Otto"
|
|
|
|
end
|
2013-01-07 00:04:35 -05:00
|
|
|
|
|
|
|
test "using attribute_will_change! with a symbol" do
|
|
|
|
@model.size = 1
|
|
|
|
assert @model.size_changed?
|
|
|
|
end
|
2014-02-08 20:34:14 -05:00
|
|
|
|
|
|
|
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
|
2014-04-24 09:06:26 -04:00
|
|
|
|
2014-07-15 15:12:23 -04:00
|
|
|
test "restore_attributes should restore all previous data" do
|
2014-04-24 09:06:26 -04:00
|
|
|
@model.name = 'Dmitry'
|
|
|
|
@model.color = 'Red'
|
|
|
|
@model.save
|
|
|
|
@model.name = 'Bob'
|
|
|
|
@model.color = 'White'
|
|
|
|
|
2014-07-17 13:53:41 -04:00
|
|
|
@model.restore_attributes
|
2014-04-24 09:06:26 -04:00
|
|
|
|
|
|
|
assert_not @model.changed?
|
|
|
|
assert_equal 'Dmitry', @model.name
|
|
|
|
assert_equal 'Red', @model.color
|
|
|
|
end
|
2014-07-17 13:53:41 -04:00
|
|
|
|
|
|
|
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 @model.changed?
|
|
|
|
assert_equal 'Dmitry', @model.name
|
|
|
|
assert_equal 'White', @model.color
|
|
|
|
end
|
2010-03-12 04:22:29 -05:00
|
|
|
end
|