提交 218105f5 编写于 作者: S Sean Griffin

Refactor in-place dirty checking to use the attribute object

上级 d8569bd4
......@@ -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
......@@ -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
......@@ -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
......@@ -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)
......@@ -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
......
......@@ -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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册