2 # Track unsaved attribute changes.
4 # A newly instantiated object is unchanged:
5 # person = Person.find_by_name('uncle bob')
6 # person.changed? # => false
10 # person.changed? # => true
11 # person.name_changed? # => true
12 # person.name_was # => 'uncle bob'
13 # person.name_change # => ['uncle bob', 'Bob']
14 # person.name = 'Bill'
15 # person.name_change # => ['uncle bob', 'Bill']
19 # person.changed? # => false
20 # person.name_changed? # => false
22 # Assigning the same value leaves the attribute unchanged:
23 # person.name = 'Bill'
24 # person.name_changed? # => false
25 # person.name_change # => nil
27 # Which attributes have changed?
29 # person.changed # => ['name']
30 # person.changes # => { 'name' => ['Bill', 'bob'] }
32 # Before modifying an attribute in-place:
33 # person.name_will_change!
35 # person.name_change # => ['uncle bob', 'uncle bobby']
37 DIRTY_SUFFIXES
= ['_changed?', '_change', '_will_change!', '_was']
39 def self.included(base
)
40 base
.attribute_method_suffix
*DIRTY_SUFFIXES
41 base
.alias_method_chain
:write_attribute, :dirty
42 base
.alias_method_chain
:save, :dirty
43 base
.alias_method_chain
:save!, :dirty
44 base
.alias_method_chain
:update, :dirty
45 base
.alias_method_chain
:reload, :dirty
47 base
.superclass_delegating_accessor
:partial_updates
48 base
.partial_updates
= true
50 base
.send(:extend, ClassMethods
)
53 # Do any attributes have unsaved changes?
54 # person.changed? # => false
56 # person.changed? # => true
58 !changed_attributes
.empty
?
61 # List of attributes with unsaved changes.
62 # person.changed # => []
64 # person.changed # => ['name']
66 changed_attributes
.keys
69 # Map of changed attrs => [original value, new value].
70 # person.changes # => {}
72 # person.changes # => { 'name' => ['bill', 'bob'] }
74 changed
.inject({}) { |h
, attr
| h
[attr
] = attribute_change(attr
); h
}
77 # Attempts to +save+ the record and clears changed attributes if successful.
78 def save_with_dirty(*args
) #:nodoc:
79 if status
= save_without_dirty(*args
)
80 changed_attributes
.clear
85 # Attempts to <tt>save!</tt> the record and clears changed attributes if successful.
86 def save_with_dirty
!(*args
) #:nodoc:
87 status
= save_without_dirty
!(*args
)
88 changed_attributes
.clear
92 # <tt>reload</tt> the record and clears changed attributes.
93 def reload_with_dirty(*args
) #:nodoc:
94 record
= reload_without_dirty(*args
)
95 changed_attributes
.clear
100 # Map of change <tt>attr => original value</tt>.
101 def changed_attributes
102 @changed_attributes ||= {}
105 # Handle <tt>*_changed?</tt> for +method_missing+.
106 def attribute_changed
?(attr
)
107 changed_attributes
.include?(attr
)
110 # Handle <tt>*_change</tt> for +method_missing+.
111 def attribute_change(attr
)
112 [changed_attributes
[attr
], __send__(attr
)] if attribute_changed
?(attr
)
115 # Handle <tt>*_was</tt> for +method_missing+.
116 def attribute_was(attr
)
117 attribute_changed
?(attr
) ? changed_attributes
[attr
] : __send__(attr
)
120 # Handle <tt>*_will_change!</tt> for +method_missing+.
121 def attribute_will_change
!(attr
)
122 changed_attributes
[attr
] = clone_attribute_value(:read_attribute, attr
)
125 # Wrap write_attribute to remember original attribute value.
126 def write_attribute_with_dirty(attr
, value
)
129 # The attribute already has an unsaved change.
130 if changed_attributes
.include?(attr
)
131 old
= changed_attributes
[attr
]
132 changed_attributes
.delete(attr
) unless field_changed
?(attr
, old
, value
)
134 old
= clone_attribute_value(:read_attribute, attr
)
135 changed_attributes
[attr
] = old
if field_changed
?(attr
, old
, value
)
139 write_attribute_without_dirty(attr
, value
)
142 def update_with_dirty
144 # Serialized attributes should always be written in case they've been
146 update_without_dirty(changed
| self.class.serialized_attributes
.keys
)
152 def field_changed
?(attr
, old
, value
)
153 if column
= column_for_attribute(attr
)
154 if column
.type
== :integer && column
.null
&& (old
.nil? || old
== 0)
155 # For nullable integer columns, NULL gets stored in database for blank (i.e. '') values.
156 # Hence we don't record it as a change if the value changes from nil to ''.
157 # If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
158 # be typecast back to 0 (''.to_i => 0)
159 value
= nil if value
.blank
?
161 value
= column
.type_cast(value
)
169 def self.extended(base
)
170 base
.metaclass
.alias_method_chain(:alias_attribute, :dirty)
173 def alias_attribute_with_dirty(new_name
, old_name
)
174 alias_attribute_without_dirty(new_name
, old_name
)
175 DIRTY_SUFFIXES
.each
do |suffix
|
176 module_eval
<<-STR, __FILE__, __LINE__+1
177 def #{new_name}#{suffix}; self.#{old_name}#{suffix}; end