Skip to content

Commit 17ce0b1

Browse files
committed
Refactor the conditionals
1 parent b9ec47d commit 17ce0b1

File tree

1 file changed

+7
-7
lines changed
  • activerecord/lib/active_record/attribute_methods

1 file changed

+7
-7
lines changed

activerecord/lib/active_record/attribute_methods/dirty.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ def update(*)
7777

7878
def _field_changed?(attr, old, value)
7979
if column = column_for_attribute(attr)
80-
if numeric_changes_from_nil_to_empty_string?(column, old, value) ||
81-
numeric_changes_from_zero_to_string?(column, old, value)
80+
if column.number? && (changes_from_nil_to_empty_string?(column, old, value) ||
81+
changes_from_zero_to_string?(column, old, value))
8282
value = nil
8383
else
8484
value = column.type_cast(value)
@@ -88,17 +88,17 @@ def _field_changed?(attr, old, value)
8888
old != value
8989
end
9090

91-
def numeric_changes_from_nil_to_empty_string?(column, old, value)
91+
def changes_from_nil_to_empty_string?(column, old, value)
9292
# For nullable numeric columns, NULL gets stored in database for blank (i.e. '') values.
9393
# Hence we don't record it as a change if the value changes from nil to ''.
9494
# If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
9595
# be typecast back to 0 (''.to_i => 0)
96-
column.number? && column.null && (old.nil? || old == 0) && value.blank?
96+
column.null && (old.nil? || old == 0) && value.blank?
9797
end
9898

99-
def numeric_changes_from_zero_to_string?(column, old, value)
100-
# For numeric columns with old 0 and value non-empty string
101-
column.number? && old == 0 && value != '0' && !value.blank? && !old.nil?
99+
def changes_from_zero_to_string?(column, old, value)
100+
# For columns with old 0 and value non-empty string
101+
old == 0 && value.present? && value != '0'
102102
end
103103
end
104104
end

0 commit comments

Comments
 (0)