diff options
author | Gonçalo Silva <goncalossilva@gmail.com> | 2011-03-24 17:21:17 +0000 |
---|---|---|
committer | Gonçalo Silva <goncalossilva@gmail.com> | 2011-03-24 17:21:17 +0000 |
commit | 9887f238871bb2dd73de6ce8855615bcc5d8d079 (patch) | |
tree | 74fa9ff9524a51701cfa23f708b3f777c65b7fe5 /activemodel/lib/active_model/dirty.rb | |
parent | aff821508a16245ebc03510ba29c70379718dfb7 (diff) | |
parent | 5214e73850916de3c9127d35a4ecee0424d364a3 (diff) | |
download | rails-9887f238871bb2dd73de6ce8855615bcc5d8d079.tar.gz rails-9887f238871bb2dd73de6ce8855615bcc5d8d079.tar.bz2 rails-9887f238871bb2dd73de6ce8855615bcc5d8d079.zip |
Merge branch 'master' of https://github.com/rails/rails
Diffstat (limited to 'activemodel/lib/active_model/dirty.rb')
-rw-r--r-- | activemodel/lib/active_model/dirty.rb | 39 |
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. |