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.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
|
|
|
# If you wish to also track previous changes on save or update, you need to
|
2012-05-31 06:06:00 -04:00
|
|
|
# add:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 16:56:20 -05:00
|
|
|
# @previously_changed = changes
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-16 16:56:20 -05:00
|
|
|
# inside of your save or update method.
|
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
|
|
|
|
# @previously_changed = changes
|
2010-08-02 15:15:44 -04:00
|
|
|
# @changed_attributes.clear
|
2010-01-16 16:56:20 -05:00
|
|
|
# end
|
|
|
|
# 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
|
|
|
#
|
2009-08-10 14:51:48 -04:00
|
|
|
# person = Person.find_by_name('Uncle Bob')
|
|
|
|
# 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
|
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
|
|
|
|
#
|
|
|
|
# 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'
|
|
|
|
attribute_method_affix :prefix => 'reset_', :suffix => '!'
|
|
|
|
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
|
2010-09-22 11:07:48 -04:00
|
|
|
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
|
2010-03-16 14:26:19 -04:00
|
|
|
@previously_changed
|
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
|
|
|
|
@changed_attributes ||= {}
|
|
|
|
end
|
|
|
|
|
2009-08-10 14:51:48 -04:00
|
|
|
private
|
2010-04-01 06:46:03 -04:00
|
|
|
|
2009-08-10 14:51:48 -04:00
|
|
|
# Handle <tt>*_changed?</tt> for +method_missing+.
|
|
|
|
def attribute_changed?(attr)
|
2010-04-01 06:46:03 -04:00
|
|
|
changed_attributes.include?(attr)
|
2009-08-10 14:51:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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>*_was</tt> for +method_missing+.
|
|
|
|
def attribute_was(attr)
|
2010-04-01 06:46:03 -04:00
|
|
|
attribute_changed?(attr) ? changed_attributes[attr] : __send__(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
|