Skip to content

Commit

Permalink
Merge pull request #3544 from amatsuda/_field_changed
Browse files Browse the repository at this point in the history
Rename field_changed? to _field_changed? so that users can create a field named field
  • Loading branch information
tenderlove committed Feb 14, 2012
2 parents abdff44 + bdb6c4e commit 848c3d4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/attribute_methods/dirty.rb
Expand Up @@ -55,12 +55,12 @@ def write_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)
@changed_attributes.delete(attr) unless _field_changed?(attr, old, value)
else
old = clone_attribute_value(:read_attribute, attr)
# Save Time objects as TimeWithZone if time_zone_aware_attributes == true
old = old.in_time_zone if clone_with_time_zone_conversion_attribute?(attr, old)
@changed_attributes[attr] = old if field_changed?(attr, old, value)
@changed_attributes[attr] = old if _field_changed?(attr, old, value)
end

# Carry on.
Expand All @@ -77,7 +77,7 @@ def update(*)
end
end

def field_changed?(attr, old, value)
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.
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/core.rb
Expand Up @@ -223,7 +223,7 @@ def initialize_dup(other)

@changed_attributes = {}
self.class.column_defaults.each do |attr, orig_value|
@changed_attributes[attr] = orig_value if field_changed?(attr, orig_value, @attributes[attr])
@changed_attributes[attr] = orig_value if _field_changed?(attr, orig_value, @attributes[attr])
end

@aggregation_cache = {}
Expand Down
14 changes: 14 additions & 0 deletions activerecord/test/cases/dirty_test.rb
Expand Up @@ -497,6 +497,20 @@ def test_previous_changes
assert !pirate.previous_changes.key?('created_on')
end

if ActiveRecord::Base.connection.supports_migrations?
class Testings < ActiveRecord::Base; end
def test_field_named_field
ActiveRecord::Base.connection.create_table :testings do |t|
t.string :field
end
assert_nothing_raised do
Testings.new.attributes
end
ensure
ActiveRecord::Base.connection.drop_table :testings rescue nil
end
end

private
def with_partial_updates(klass, on = true)
old = klass.partial_updates?
Expand Down

0 comments on commit 848c3d4

Please sign in to comment.