Skip to content

Commit

Permalink
Merge pull request #15755 from sgrif/sg-dirty-refactor
Browse files Browse the repository at this point in the history
Refactor in-place dirty checking to use the attribute object
  • Loading branch information
rafaelfranca committed Jun 16, 2014
2 parents 1aa059c + 218105f commit 78ae3a3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
19 changes: 19 additions & 0 deletions activerecord/lib/active_record/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ def value_for_database
type.type_cast_for_database(value)
end

def changed_from?(old_value)
type.changed?(old_value, value, value_before_type_cast)
end

def changed_in_place_from?(old_value)
type.changed_in_place?(old_value, value)
end

def type_cast
raise NotImplementedError
end
Expand All @@ -52,5 +60,16 @@ def type_cast(value)
type.type_cast_from_user(value)
end
end

class Null
class << self
attr_reader :value, :value_before_type_cast, :value_for_database

def changed_from?(*)
false
end
alias changed_in_place_from? changed_from?
end
end
end
end
12 changes: 3 additions & 9 deletions activerecord/lib/active_record/attribute_methods/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ def keys_for_partial_write
end

def _field_changed?(attr, old_value)
new_value = read_attribute(attr)
raw_value = read_attribute_before_type_cast(attr)
column_for_attribute(attr).changed?(old_value, new_value, raw_value)
attribute_named(attr).changed_from?(old_value)
end

def changed_in_place
Expand All @@ -149,10 +147,8 @@ def changed_in_place
end

def changed_in_place?(attr_name)
type = type_for_attribute(attr_name)
old_value = original_raw_attribute(attr_name)
value = read_attribute(attr_name)
type.changed_in_place?(old_value, value)
attribute_named(attr_name).changed_in_place_from?(old_value)
end

def original_raw_attribute(attr_name)
Expand All @@ -166,9 +162,7 @@ def original_raw_attributes
end

def store_original_raw_attribute(attr_name)
type = type_for_attribute(attr_name)
value = type.type_cast_for_database(read_attribute(attr_name))
original_raw_attributes[attr_name] = value
original_raw_attributes[attr_name] = attribute_named(attr_name).value_for_database
end

def store_original_raw_attributes
Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/attribute_methods/read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ def read_attribute(attr_name)
def attribute(attribute_name)
read_attribute(attribute_name)
end

def attribute_named(attribute_name)
@attributes.fetch(attribute_name, Attribute::Null)
end
end
end
end

0 comments on commit 78ae3a3

Please sign in to comment.