Provides a way to track changes in your object in the same way as Active Record does.
The requirements for implementing
ActiveModel::Dirty are:
include ActiveModel::Dirty in your object.
Call define_attribute_methods passing each method you want to track.
Call attr_name_will_change! before each change to the tracked attribute.
Call changes_applied after the changes are persisted.
Call clear_changes_information when you want to reset the changes information.
Call restore_attributes when you want to restore previous data.
A newly instantiated Person
object is unchanged:
person = Person.new
person.changed?
Change the name:
person.name = 'Bob'
person.changed?
person.name_changed?
person.name_changed?(from: "Uncle Bob", to: "Bob")
person.name_was
person.name_change
person.name = 'Bill'
person.name_change
Save the changes:
person.save
person.changed?
person.name_changed?
Reset the changes:
person.previous_changes
person.reload!
person.previous_changes
Rollback the changes:
person.name = "Uncle Bob"
person.rollback!
person.name
person.name_changed?
Assigning the same value leaves the attribute unchanged:
person.name = 'Bill'
person.name_changed?
person.name_change
Which attributes have changed?