Skip to content

Commit

Permalink
Refactor the conditionals
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Jun 19, 2012
1 parent b9ec47d commit 17ce0b1
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions activerecord/lib/active_record/attribute_methods/dirty.rb
Expand Up @@ -77,8 +77,8 @@ def update(*)


def _field_changed?(attr, old, value) def _field_changed?(attr, old, value)
if column = column_for_attribute(attr) if column = column_for_attribute(attr)
if numeric_changes_from_nil_to_empty_string?(column, old, value) || if column.number? && (changes_from_nil_to_empty_string?(column, old, value) ||
numeric_changes_from_zero_to_string?(column, old, value) changes_from_zero_to_string?(column, old, value))
value = nil value = nil
else else
value = column.type_cast(value) value = column.type_cast(value)
Expand All @@ -88,17 +88,17 @@ def _field_changed?(attr, old, value)
old != value old != value
end end


def numeric_changes_from_nil_to_empty_string?(column, old, value) def changes_from_nil_to_empty_string?(column, old, value)
# For nullable numeric columns, NULL gets stored in database for blank (i.e. '') values. # For nullable numeric 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 ''. # 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 # 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) # be typecast back to 0 (''.to_i => 0)
column.number? && column.null && (old.nil? || old == 0) && value.blank? column.null && (old.nil? || old == 0) && value.blank?
end end


def numeric_changes_from_zero_to_string?(column, old, value) def changes_from_zero_to_string?(column, old, value)
# For numeric columns with old 0 and value non-empty string # For columns with old 0 and value non-empty string
column.number? && old == 0 && value != '0' && !value.blank? && !old.nil? old == 0 && value.present? && value != '0'
end end
end end
end end
Expand Down

0 comments on commit 17ce0b1

Please sign in to comment.