lib/active_model/dirty.rb in activemodel-3.0.20 vs lib/active_model/dirty.rb in activemodel-3.1.0.beta1
- old
+ new
@@ -1,17 +1,16 @@
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
# Active Record does.
#
- # The requirements to implement ActiveModel::Dirty are to:
+ # The requirements for implementing ActiveModel::Dirty are:
#
# * <tt>include ActiveModel::Dirty</tt> in your object
# * 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
@@ -92,11 +91,11 @@
included do
attribute_method_suffix '_changed?', '_change', '_will_change!', '_was'
attribute_method_affix :prefix => 'reset_', :suffix => '!'
end
- # Do any attributes have unsaved changes?
+ # Returns true if any attribute have unsaved changes, false otherwise.
# person.changed? # => false
# person.name = 'bob'
# person.changed? # => true
def changed?
!changed_attributes.empty?
@@ -113,10 +112,10 @@
# Map of changed attrs => [original value, new value].
# person.changes # => {}
# 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.
# person.name # => 'bob'
# person.name = 'robert'