2010-03-27 14:03:36 -04:00
|
|
|
require 'active_support/hash_with_indifferent_access'
|
2010-03-28 06:43:20 -04:00
|
|
|
require 'active_support/core_ext/object/duplicable'
|
2010-03-27 14:03:36 -04:00
|
|
|
|
2009-08-10 14:51:48 -04:00
|
|
|
module ActiveModel
|
2012-10-21 02:26:01 -04:00
|
|
|
# == Active \Model \Dirty
|
2010-06-14 05:08:29 -04:00
|
|
|
#
|
2010-08-14 01:13:00 -04:00
|
|
|
# Provides a way to track changes in your object in the same way as
|
2010-06-14 05:08:29 -04:00
|
|
|
# Active Record does.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2011-04-11 15:54:46 -04:00
|
|
|
# The requirements for implementing ActiveModel::Dirty are:
|
2010-01-16 16:56:20 -05:00
|
|
|
#
|
2012-05-31 06:06:00 -04:00
|
|
|
# * <tt>include ActiveModel::Dirty</tt> in your object.
|
2010-08-14 01:13:00 -04:00
|
|
|
# * Call <tt>define_attribute_methods</tt> passing each method you want to
|
2012-05-31 06:06:00 -04:00
|
|
|
# track.
|
2010-08-14 01:13:00 -04:00
|
|
|
# * Call <tt>attr_name_will_change!</tt> before each change to the tracked
|
2012-05-31 06:06:00 -04:00
|
|
|
# attribute.
|
2013-09-23 09:59:05 -04:00
|
|
|
# * Call <tt>changes_applied</tt> after the changes are persisted.
|
2013-09-23 10:00:41 -04:00
|
|
|
# * Call <tt>reset_changes</tt> when you want to reset the changes
|
|
|
|
# information.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 16:56:20 -05:00
|
|
|
# A minimal implementation could be:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 16:56:20 -05:00
|
|
|
# class Person
|
|
|
|
# include ActiveModel::Dirty
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2012-05-14 12:38:23 -04:00
|
|
|
# define_attribute_methods :name
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 16:56:20 -05:00
|
|
|
# def name
|
|
|
|
# @name
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 16:56:20 -05:00
|
|
|
# def name=(val)
|
2010-08-02 15:15:44 -04:00
|
|
|
# name_will_change! unless val == @name
|
2010-01-16 16:56:20 -05:00
|
|
|
# @name = val
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 16:56:20 -05:00
|
|
|
# def save
|
2013-09-23 09:59:05 -04:00
|
|
|
# # do persistence work
|
|
|
|
# changes_applied
|
2010-01-16 16:56:20 -05:00
|
|
|
# end
|
2013-09-23 10:00:41 -04:00
|
|
|
#
|
|
|
|
# def reload!
|
|
|
|
# reset_changes
|
|
|
|
# end
|
2010-01-16 16:56:20 -05:00
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2009-08-10 14:51:48 -04:00
|
|
|
# A newly instantiated object is unchanged:
|
2012-05-31 06:06:00 -04:00
|
|
|
#
|
2013-04-02 15:09:15 -04:00
|
|
|
# person = Person.find_by(name: 'Uncle Bob')
|
2009-08-10 14:51:48 -04:00
|
|
|
# person.changed? # => false
|
|
|
|
#
|
|
|
|
# Change the name:
|
2012-05-31 06:06:00 -04:00
|
|
|
#
|
2009-08-10 14:51:48 -04:00
|
|
|
# person.name = 'Bob'
|
|
|
|
# person.changed? # => true
|
|
|
|
# person.name_changed? # => true
|
2013-12-01 23:50:43 -05:00
|
|
|
# person.name_changed?(from: "Uncle Bob", to: "Bob") # => true
|
2012-05-31 06:06:00 -04:00
|
|
|
# person.name_was # => "Uncle Bob"
|
|
|
|
# person.name_change # => ["Uncle Bob", "Bob"]
|
2009-08-10 14:51:48 -04:00
|
|
|
# person.name = 'Bill'
|
2012-05-31 06:06:00 -04:00
|
|
|
# person.name_change # => ["Uncle Bob", "Bill"]
|
2009-08-10 14:51:48 -04:00
|
|
|
#
|
|
|
|
# Save the changes:
|
2012-05-31 06:06:00 -04:00
|
|
|
#
|
2009-08-10 14:51:48 -04:00
|
|
|
# person.save
|
|
|
|
# person.changed? # => false
|
|
|
|
# person.name_changed? # => false
|
|
|
|
#
|
2013-09-23 10:00:41 -04:00
|
|
|
# Reset the changes:
|
|
|
|
#
|
|
|
|
# person.previous_changes # => {"name" => ["Uncle Bob", "Bill"]}
|
2013-09-23 10:25:43 -04:00
|
|
|
# person.reload!
|
2013-09-23 10:00:41 -04:00
|
|
|
# person.previous_changes # => {}
|
|
|
|
#
|
2009-08-10 14:51:48 -04:00
|
|
|
# Assigning the same value leaves the attribute unchanged:
|
2012-05-31 06:06:00 -04:00
|
|
|
#
|
2009-08-10 14:51:48 -04:00
|
|
|
# person.name = 'Bill'
|
|
|
|
# person.name_changed? # => false
|
|
|
|
# person.name_change # => nil
|
|
|
|
#
|
|
|
|
# Which attributes have changed?
|
2012-05-31 06:06:00 -04:00
|
|
|
#
|
2009-08-10 14:51:48 -04:00
|
|
|
# person.name = 'Bob'
|
2012-05-31 06:06:00 -04:00
|
|
|
# person.changed # => ["name"]
|
|
|
|
# person.changes # => {"name" => ["Bill", "Bob"]}
|
2009-08-10 14:51:48 -04:00
|
|
|
#
|
2010-07-29 09:18:29 -04:00
|
|
|
# If an attribute is modified in-place then make use of <tt>[attribute_name]_will_change!</tt>
|
2012-06-22 12:52:57 -04:00
|
|
|
# to mark that the attribute is changing. Otherwise ActiveModel can't track
|
|
|
|
# changes to in-place attributes.
|
2010-07-29 09:18:29 -04:00
|
|
|
#
|
2009-08-10 14:51:48 -04:00
|
|
|
# person.name_will_change!
|
2012-05-31 06:06:00 -04:00
|
|
|
# person.name_change # => ["Bill", "Bill"]
|
2009-08-10 14:51:48 -04:00
|
|
|
# person.name << 'y'
|
2012-05-31 06:06:00 -04:00
|
|
|
# person.name_change # => ["Bill", "Billy"]
|
2009-08-10 14:51:48 -04:00
|
|
|
module Dirty
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
include ActiveModel::AttributeMethods
|
|
|
|
|
|
|
|
included do
|
|
|
|
attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
|
2013-05-01 20:10:06 -04:00
|
|
|
attribute_method_affix prefix: 'reset_', suffix: '!'
|
2009-08-10 14:51:48 -04:00
|
|
|
end
|
|
|
|
|
2012-05-31 06:06:00 -04:00
|
|
|
# Returns +true+ if any attribute have unsaved changes, +false+ otherwise.
|
|
|
|
#
|
2009-08-10 14:51:48 -04:00
|
|
|
# person.changed? # => false
|
|
|
|
# person.name = 'bob'
|
|
|
|
# person.changed? # => true
|
|
|
|
def changed?
|
2012-02-06 05:43:38 -05:00
|
|
|
changed_attributes.present?
|
2009-08-10 14:51:48 -04:00
|
|
|
end
|
|
|
|
|
2012-05-31 06:06:00 -04:00
|
|
|
# Returns an array with the name of the attributes with unsaved changes.
|
|
|
|
#
|
2009-08-10 14:51:48 -04:00
|
|
|
# person.changed # => []
|
|
|
|
# person.name = 'bob'
|
2012-05-31 06:06:00 -04:00
|
|
|
# person.changed # => ["name"]
|
2009-08-10 14:51:48 -04:00
|
|
|
def changed
|
2010-04-01 06:46:03 -04:00
|
|
|
changed_attributes.keys
|
2009-08-10 14:51:48 -04:00
|
|
|
end
|
|
|
|
|
2012-05-31 06:06:00 -04:00
|
|
|
# Returns a hash of changed attributes indicating their original
|
2012-06-22 12:52:57 -04:00
|
|
|
# and new values like <tt>attr => [original value, new value]</tt>.
|
2012-05-31 06:06:00 -04:00
|
|
|
#
|
2009-08-10 14:51:48 -04:00
|
|
|
# person.changes # => {}
|
|
|
|
# person.name = 'bob'
|
2012-05-31 06:06:00 -04:00
|
|
|
# person.changes # => { "name" => ["bill", "bob"] }
|
2009-08-10 14:51:48 -04:00
|
|
|
def changes
|
2013-01-06 16:00:10 -05:00
|
|
|
ActiveSupport::HashWithIndifferentAccess[changed.map { |attr| [attr, attribute_change(attr)] }]
|
2009-08-10 14:51:48 -04:00
|
|
|
end
|
|
|
|
|
2012-05-31 06:06:00 -04:00
|
|
|
# Returns a hash of attributes that were changed before the model was saved.
|
|
|
|
#
|
|
|
|
# person.name # => "bob"
|
2009-08-31 13:50:27 -04:00
|
|
|
# person.name = 'robert'
|
|
|
|
# person.save
|
2012-05-31 06:06:00 -04:00
|
|
|
# person.previous_changes # => {"name" => ["bob", "robert"]}
|
2009-08-31 13:50:27 -04:00
|
|
|
def previous_changes
|
2014-01-20 20:00:17 -05:00
|
|
|
@previously_changed ||= ActiveSupport::HashWithIndifferentAccess.new
|
2009-08-31 13:50:27 -04:00
|
|
|
end
|
|
|
|
|
2012-05-31 06:06:00 -04:00
|
|
|
# Returns a hash of the attributes with unsaved changes indicating their original
|
|
|
|
# values like <tt>attr => original value</tt>.
|
|
|
|
#
|
|
|
|
# person.name # => "bob"
|
|
|
|
# person.name = 'robert'
|
|
|
|
# person.changed_attributes # => {"name" => "bob"}
|
2010-05-16 06:24:41 -04:00
|
|
|
def changed_attributes
|
2013-01-07 00:04:35 -05:00
|
|
|
@changed_attributes ||= ActiveSupport::HashWithIndifferentAccess.new
|
2010-05-16 06:24:41 -04:00
|
|
|
end
|
|
|
|
|
2013-06-11 16:39:46 -04:00
|
|
|
# Handle <tt>*_changed?</tt> for +method_missing+.
|
2013-12-01 23:50:43 -05:00
|
|
|
def attribute_changed?(attr, options = {}) #:nodoc:
|
|
|
|
result = changed_attributes.include?(attr)
|
|
|
|
result &&= options[:to] == __send__(attr) if options.key?(:to)
|
|
|
|
result &&= options[:from] == changed_attributes[attr] if options.key?(:from)
|
|
|
|
result
|
2013-06-11 16:39:46 -04:00
|
|
|
end
|
2010-04-01 06:46:03 -04:00
|
|
|
|
2013-06-11 16:39:46 -04:00
|
|
|
# Handle <tt>*_was</tt> for +method_missing+.
|
2013-12-12 15:01:52 -05:00
|
|
|
def attribute_was(attr) # :nodoc:
|
2013-06-11 16:39:46 -04:00
|
|
|
attribute_changed?(attr) ? changed_attributes[attr] : __send__(attr)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2009-08-10 14:51:48 -04:00
|
|
|
|
2013-09-23 09:59:05 -04:00
|
|
|
# Removes current changes and makes them accessible through +previous_changes+.
|
|
|
|
def changes_applied
|
|
|
|
@previously_changed = changes
|
2014-01-20 19:58:14 -05:00
|
|
|
@changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
|
2013-09-23 09:59:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Removes all dirty data: current changes and previous changes
|
|
|
|
def reset_changes
|
2014-01-20 19:58:14 -05:00
|
|
|
@previously_changed = ActiveSupport::HashWithIndifferentAccess.new
|
|
|
|
@changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
|
2013-09-23 09:59:05 -04:00
|
|
|
end
|
|
|
|
|
2009-08-10 14:51:48 -04:00
|
|
|
# Handle <tt>*_change</tt> for +method_missing+.
|
|
|
|
def attribute_change(attr)
|
2010-04-01 06:46:03 -04:00
|
|
|
[changed_attributes[attr], __send__(attr)] if attribute_changed?(attr)
|
2009-08-10 14:51:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Handle <tt>*_will_change!</tt> for +method_missing+.
|
|
|
|
def attribute_will_change!(attr)
|
2012-04-27 19:44:08 -04:00
|
|
|
return if attribute_changed?(attr)
|
|
|
|
|
2009-08-10 14:51:48 -04:00
|
|
|
begin
|
|
|
|
value = __send__(attr)
|
|
|
|
value = value.duplicable? ? value.clone : value
|
|
|
|
rescue TypeError, NoMethodError
|
|
|
|
end
|
|
|
|
|
2012-04-27 19:44:08 -04:00
|
|
|
changed_attributes[attr] = value
|
2009-08-10 14:51:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Handle <tt>reset_*!</tt> for +method_missing+.
|
|
|
|
def reset_attribute!(attr)
|
2012-12-01 12:29:49 -05:00
|
|
|
if attribute_changed?(attr)
|
|
|
|
__send__("#{attr}=", changed_attributes[attr])
|
|
|
|
changed_attributes.delete(attr)
|
|
|
|
end
|
2009-08-10 14:51:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|