mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
some refactoring and behave even more friendly with alias_method_chain
This commit is contained in:
parent
403a91addf
commit
cb962d1157
@ -35,33 +35,31 @@ module ValidatesTimeliness
|
|||||||
if !(value = @attributes[attr_name]).nil?
|
if !(value = @attributes[attr_name]).nil?
|
||||||
column = column_for_attribute(attr_name)
|
column = column_for_attribute(attr_name)
|
||||||
if column && [:date, :time, :datetime].include?(column.type) && @attributes_cache.has_key?(attr_name)
|
if column && [:date, :time, :datetime].include?(column.type) && @attributes_cache.has_key?(attr_name)
|
||||||
@attributes_cache[attr_name]
|
return @attributes_cache[attr_name]
|
||||||
else
|
end
|
||||||
|
end
|
||||||
read_attribute_without_timeliness(attr_name)
|
read_attribute_without_timeliness(attr_name)
|
||||||
end
|
end
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# If Rails dirty attributes is enabled then the value is added to
|
# If Rails dirty attributes is enabled then the value is added to
|
||||||
# changed attributes if changed. Can't use the default dirty checking
|
# changed attributes if changed. Can't use the default dirty checking
|
||||||
# implementation as it chains the write_attribute method which deletes
|
# implementation as it chains the write_attribute method which deletes
|
||||||
# the attribute from the cache.
|
# the attribute from the cache.
|
||||||
def write_date_time_attribute(attr_name, value, type, time_zone_aware)
|
def write_date_time_attribute(attr_name, value, type, time_zone_aware)
|
||||||
old = read_attribute(attr_name) if defined?(::ActiveRecord::Dirty)
|
|
||||||
new = self.class.parse_date_time(value, type)
|
new = self.class.parse_date_time(value, type)
|
||||||
|
|
||||||
unless type == :date || new.nil?
|
if new.acts_like?(:time)
|
||||||
new = new.to_time rescue new
|
new = new.to_time
|
||||||
|
new = new.in_time_zone if time_zone_aware
|
||||||
end
|
end
|
||||||
|
|
||||||
new = new.in_time_zone if new && time_zone_aware
|
if defined?(::ActiveRecord::Dirty) && !changed_attributes.include?(attr_name)
|
||||||
@attributes_cache[attr_name] = new
|
old = read_attribute(attr_name)
|
||||||
|
if old != new
|
||||||
if defined?(::ActiveRecord::Dirty) && !changed_attributes.include?(attr_name) && old != new
|
|
||||||
changed_attributes[attr_name] = (old.duplicable? ? old.clone : old)
|
changed_attributes[attr_name] = (old.duplicable? ? old.clone : old)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
@attributes_cache[attr_name] = new
|
||||||
@attributes[attr_name] = value
|
@attributes[attr_name] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user