Skip to content

Commit

Permalink
- Updating dirty.rb to match the lastest rails implementation of meth…
Browse files Browse the repository at this point in the history
…od write_attribute_with_dirty

- Also fixing a rails bug that handles incorrectly the changes for null decimal fields:
http://www.pastie.org/348281
  • Loading branch information
ckozus authored and bkeepers committed Dec 30, 2008
1 parent 3b7cfaf commit c1acd59
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions lib/acts_as_audited/dirty.rb
Expand Up @@ -117,22 +117,34 @@ def write_attribute_with_dirty(attr, value)
attr = attr.to_s

# The attribute already has an unsaved change.
unless changed_attributes.include?(attr)
if changed_attributes.include?(attr)
old = changed_attributes[attr]
changed_attributes.delete(attr) unless field_changed?(attr, old, value)
else
old = clone_attribute_value(:read_attribute, attr)

# Remember the original value if it's different.
typecasted = if column = column_for_attribute(attr)
column.type_cast(value)
else
value
end
changed_attributes[attr] = old unless old == typecasted
changed_attributes[attr] = old if field_changed?(attr, old, value)
end

# Carry on.
write_attribute_without_dirty(attr, value)
end

def field_changed?(attr, old, value)
if column = column_for_attribute(attr)
if (column.type == :integer || column.type == :decimal) && column.null && (old.nil? || old == 0)
# For nullable integer columns, NULL gets stored in database for blank (i.e. '') values.
# Hence we don't record it as a change if the value changes from nil to ''.
# If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
# be typecast back to 0 (''.to_i => 0)
value = nil if value.blank?
else
value = column.type_cast(value)
end
end

old != value
end

# def update_with_dirty
# update_without_dirty(changed)
# end
Expand Down

0 comments on commit c1acd59

Please sign in to comment.