class Hash
# Return a hash containing the entries that differ to the passed hash.
# Blank objects are considered equal.
#
# { 1 => 1, 2 => 3 }.substantial_differences_to({ 1 => 4, 2 => 3 }) => { 1 => 1 }
# { 1 => 1 }.substantial_differences_to({ 1 => 1 }) => {}
def substantial_differences_to(hash)
reject do |key, value|
hash[key] == value || (hash[key].blank? && value.blank?)
end
end
def substantial_difference_to?(hash)
substantial_differences_to(hash).any?
end
end
module ActiveRecord
module Acts
module Modified
def self.included(base)
base.extend(SingletonMethods)
base.attribute_method_suffix "_modified?", "_original"
base.class_inheritable_array :excluded_attributes, :included_attributes
base.excluded_attributes = %w(updated_at updated_on)
end
module SingletonMethods
# == Configuration options
#
# * :clear_after_save - should the original attributes be cleared after saving, defaults to false. Causes modified? to return false after save has been called.
# * :except - A list of attributes that should not be monitored for modifications.
# * :only - If specified, only the attributes listed will be monitored for modifications.
def acts_as_modified(options = {})
options.assert_valid_keys :clear_after_save, :except, :only
after_save :clear_original_attributes if options[:clear_after_save]
self.excluded_attributes = [*options[:except]].map!(&:to_s) if options[:except]
self.included_attributes = [*options[:only]].map!(&:to_s) if options[:only]
unless self.included_modules.include?(ActiveRecord::Acts::Modified::InstanceMethods)
class << self
include ClassMethods
alias_method_chain :belongs_to, :original
end
include InstanceMethods
alias_method_chain :write_attribute, :original_attributes
alias_method_chain :reload, :clear_original_attributes
end
end
end
module ClassMethods
# Use original_+association+ to get the original object of a belongs_to association.
#
# person.original_school instead of School.find(person.school_id_original)
#
# person = Person.find(:first)
# person.school_id # 1
# person.school_id = 3
# person.original_school # will do School.find_by_id(1)
def belongs_to_with_original(association_id, options = {})
returning belongs_to_without_original(association_id, options) do
reflection = reflections[association_id]
class_eval <<-END
def original_#{association_id}
reflection = self.class.reflections[:#{association_id}]
original_foreign_key = read_original_attribute(reflection.primary_key_name)
unless original_foreign_key.blank?
reflection.klass.find_by_id(original_foreign_key)
end
end
END
end
end
end
module InstanceMethods
# Updates the attribute identified by attr_name with the specified +value+. Empty strings for fixnum and float columns are turned into nil.
# The first call causes the original values of all attributes to be stored
def write_attribute_with_original_attributes(attr_name, value) #:nodoc:
ensure_original_attribute_stored(attr_name)
write_attribute_without_original_attributes(attr_name, value)
end
# Like ActiveRecord::Base#attributes_before_type_cast, but returns original attribute values
def original_attributes_before_type_cast
clone_attributes :read_original_attribute_before_type_cast
end
# Like ActiveRecord::Base#attributes, but returns original attribute values
def original_attributes
clone_attributes :read_original_attribute
end
# Like ActiveRecord::Base#read_attribute, but returns original attribute value
def read_original_attribute(attr_name)
attr_name = attr_name.to_s
unless original_attributes_hash.include?(attr_name)
__send__(attr_name)
else
if !(value = original_attributes_hash[attr_name]).nil?
if column = column_for_attribute(attr_name)
if unserializable_attribute?(attr_name, column)
unserialize_attribute(attr_name)
else
column.type_cast(value)
end
else
value
end
else
nil
end
end
end
# Like ActiveRecord::Base#read_attribute_before_type_cast, but returns original attribute value
def read_original_attribute_before_type_cast(attr_name)
attr_name = attr_name.to_s
if original_attributes_hash.include?(attr_name)
original_attributes_hash[attr_name]
else
read_attribute_before_type_cast(attr_name)
end
end
# Replaces the current set of original values with the current attribute values.
# This makes it as if the attributes were never modified.
#
# person = Person.find(:first)
# person.name = 'New name'
# person.modified? # true
#
# person.clear_original_attributes
#
# person.modified? # false
def clear_original_attributes
original_attributes_hash.clear
end
# Returns true if any of the attributes have changed. New records always return false.
#
# person = Person.find(:first)
# person.modified? # false
# person.name = "New name"
# person.modified? # true
def modified?
new_record? || original_attributes.substantial_difference_to?(attributes)
end
# Returns a hash containing changed attributes and their original values.
# Pass :changed to return the current attribute values instead.
#
# person = Person.find(:first)
# person.name # Jonathan
# person.name = 'New name'
# person.modified_attributes # { "name" => "Jonathan" }
# person.modified_attributes(:changed) # { "name" => "New name" }
def modified_attributes(changed = nil)
returning original_attributes.substantial_differences_to(attributes) do |values|
if changed
values.update(attributes.slice(*values.keys))
end
end
end
# Restore the attributes to their original values. Use :only or :except to restore specific attributes.
#
# person = Person.find(:first)
# person.name # Jonathan
# person.age # 100
# person.name = 'New name'
# person.age = 25
#
# person.restore_attributes # Restores name and age to original values
# person.restore_attributes :only => :name # Restores name to its original value
# person.restore_attributes :except => [:name, :age] # Restores all attributes except name and age to their original values
def restore_attributes
@attributes.update(original_attributes_before_type_cast)
@attributes_cache.clear
end
# Use +attribute+_modified? to find out if a specific attribute has been modified.
#
# person = Person.find(:first)
# person.name_modified? # false
# person.name = 'New name'
# person.name_modified? # true
def attribute_modified?(attr_name)
modified_attributes.include?(attr_name.to_s)
end
# When Base#reload is called, the original attributes should be cleared
def reload_with_clear_original_attributes(options = nil) #:nodoc:
clear_original_attributes
reload_without_clear_original_attributes(options)
end
private
def original_attributes_hash
@original_attributes ||= {}
end
def attribute_original(attr_name)
read_original_attribute(attr_name)
end
def included_attribute?(attr_name)
included_attributes && included_attributes.include?(attr_name.to_s)
end
def excluded_attribute?(attr_name)
excluded_attributes && excluded_attributes.include?(attr_name.to_s)
end
def store_original_attribute?(attr_name)
attr_name = attr_name.to_s
# If attributes are exclusively included, this attribute must be one of them and not excluded
if included_attributes
return included_attribute?(attr_name) && !excluded_attribute?(attr_name)
end
# Otherwise, this attribute just needs to not be excluded
!excluded_attribute?(attr_name)
end
def ensure_original_attribute_stored(attr_name)
attr_name = attr_name.to_s
if store_original_attribute?(attr_name) and !original_attributes_hash.include?(attr_name)
original_attributes_hash[attr_name] = @attributes[attr_name]
end
end
end
end
end
end
ActiveRecord::Base.class_eval do
include ActiveRecord::Acts::Modified
end