aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/lib/active_model/dirty.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel/lib/active_model/dirty.rb')
-rw-r--r--activemodel/lib/active_model/dirty.rb39
1 files changed, 19 insertions, 20 deletions
diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb
index 2516377afd..a479795d51 100644
--- a/activemodel/lib/active_model/dirty.rb
+++ b/activemodel/lib/active_model/dirty.rb
@@ -1,53 +1,52 @@
require 'active_model/attribute_methods'
-require 'active_support/concern'
require 'active_support/hash_with_indifferent_access'
require 'active_support/core_ext/object/duplicable'
module ActiveModel
# == Active Model Dirty
#
- # Provides a way to track changes in your object in the same way as
+ # Provides a way to track changes in your object in the same way as
# Active Record does.
- #
+ #
# The requirements to implement ActiveModel::Dirty are to:
#
# * <tt>include ActiveModel::Dirty</tt> in your object
- # * Call <tt>define_attribute_methods</tt> passing each method you want to
+ # * Call <tt>define_attribute_methods</tt> passing each method you want to
# track
- # * Call <tt>attr_name_will_change!</tt> before each change to the tracked
+ # * Call <tt>attr_name_will_change!</tt> before each change to the tracked
# attribute
- #
- # If you wish to also track previous changes on save or update, you need to
+ #
+ # If you wish to also track previous changes on save or update, you need to
# add
- #
+ #
# @previously_changed = changes
- #
+ #
# inside of your save or update method.
- #
+ #
# A minimal implementation could be:
- #
+ #
# class Person
- #
+ #
# include ActiveModel::Dirty
- #
+ #
# define_attribute_methods [:name]
- #
+ #
# def name
# @name
# end
- #
+ #
# def name=(val)
# name_will_change! unless val == @name
# @name = val
# end
- #
+ #
# def save
# @previously_changed = changes
# @changed_attributes.clear
# end
- #
+ #
# end
- #
+ #
# == Examples:
#
# A newly instantiated object is unchanged:
@@ -79,7 +78,7 @@ module ActiveModel
# person.changes # => { 'name' => ['Bill', 'Bob'] }
#
# If an attribute is modified in-place then make use of <tt>[attribute_name]_will_change!</tt>
- # to mark that the attribute is changing. Otherwise ActiveModel can't track changes to
+ # to mark that the attribute is changing. Otherwise ActiveModel can't track changes to
# in-place attributes.
#
# person.name_will_change!
@@ -115,7 +114,7 @@ module ActiveModel
# person.name = 'bob'
# person.changes # => { 'name' => ['bill', 'bob'] }
def changes
- changed.inject(HashWithIndifferentAccess.new){ |h, attr| h[attr] = attribute_change(attr); h }
+ HashWithIndifferentAccess[changed.map { |attr| [attr, attribute_change(attr)] }]
end
# Map of attributes that were changed when the model was saved.