module ActiveRecord::AttributeMethods::Dirty
def _field_changed?(attr, old, value)
def _field_changed?(attr, old, value) if column = column_for_attribute(attr) 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) end end old != value end
def 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. # 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)
def changes_from_zero_to_string?(old, value) # For columns with old 0 and value non-empty string old == 0 && value.is_a?(String) && value.present? && non_zero?(value) end
def create_record(*)
def create_record(*) partial_writes? ? super(keys_for_partial_write) : super end
def init_changed_attributes
def init_changed_attributes @changed_attributes = nil # Intentionally avoid using #column_defaults since overridden defaults (as is done in # optimistic locking) won't get written unless they get marked as changed self.class.columns.each do |c| attr, orig_value = c.name, c.default changed_attributes[attr] = orig_value if _field_changed?(attr, orig_value, @attributes[attr]) end end
def initialize_dup(other) # :nodoc:
def initialize_dup(other) # :nodoc: super init_changed_attributes end
def initialize_internals_callback
def initialize_internals_callback super init_changed_attributes end
def keys_for_partial_write
Serialized attributes should always be written in case they've been
def keys_for_partial_write changed end
def non_zero?(value)
def non_zero?(value) value !~ /\A0+(\.0+)?\z/ end
def reload(*)
def reload(*) super.tap do reset_changes end end
def save(*)
def save(*) if status = super changes_applied end status end
def save!(*)
def save!(*) super.tap do changes_applied end end
def save_changed_attribute(attr, value)
def save_changed_attribute(attr, value) # The attribute already has an unsaved change. if attribute_changed?(attr) old = changed_attributes[attr] changed_attributes.delete(attr) unless _field_changed?(attr, old, value) else old = clone_attribute_value(:read_attribute, attr) changed_attributes[attr] = old if _field_changed?(attr, old, value) end end
def update_record(*)
def update_record(*) partial_writes? ? super(keys_for_partial_write) : super end
def write_attribute(attr, value)
def write_attribute(attr, value) attr = attr.to_s save_changed_attribute(attr, value) super(attr, value) end