aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-06-19 01:55:33 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-06-21 11:03:06 -0300
commit40617c7e539b8b70469671619e3c1716edcfbf59 (patch)
tree6e6535743f79aa859813facce2f51a864215ad5a /activerecord/lib/active_record/attribute_methods
parent94489a2570c2b922a820e5e150dc33711379ea52 (diff)
downloadrails-40617c7e539b8b70469671619e3c1716edcfbf59.tar.gz
rails-40617c7e539b8b70469671619e3c1716edcfbf59.tar.bz2
rails-40617c7e539b8b70469671619e3c1716edcfbf59.zip
Merge branch 'acapilleri-update_nested_attributes'
Closes #6675 Conflicts: activerecord/lib/active_record/attribute_methods/dirty.rb
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods')
-rw-r--r--activerecord/lib/active_record/attribute_methods/dirty.rb20
1 files changed, 15 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb
index f61e016f46..586a189011 100644
--- a/activerecord/lib/active_record/attribute_methods/dirty.rb
+++ b/activerecord/lib/active_record/attribute_methods/dirty.rb
@@ -79,11 +79,8 @@ module ActiveRecord
def field_changed?(attr, old, value)
if column = column_for_attribute(attr)
- if column.number? && column.null && (old.nil? || old == 0) && value.blank?
- # 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 ''.
- # 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)
+ if column.number? && (changes_from_nil_to_empty_string?(column, old, value) ||
+ changes_from_zero_to_string?(old, value))
value = nil
else
value = column.type_cast(value)
@@ -96,6 +93,19 @@ module ActiveRecord
def clone_with_time_zone_conversion_attribute?(attr, old)
old.class.name == "Time" && time_zone_aware_attributes && !self.skip_time_zone_conversion_for_attributes.include?(attr.to_sym)
end
+
+ def changes_from_nil_to_empty_string?(column, old, value)
+ # 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 ''.
+ # 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)
+ column.null && (old.nil? || old == 0) && value.blank?
+ end
+
+ def changes_from_zero_to_string?(old, value)
+ # For columns with old 0 and value non-empty string
+ old == 0 && value.present? && value != '0'
+ end
end
end
end