mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
fixed error on database write with saving cached values for time attributes which have been converted
This commit is contained in:
parent
ed23c4a9a4
commit
39e16d2fde
@ -10,10 +10,6 @@ module ValidatesTimeliness
|
|||||||
# be converted to local timezone and then stored and cached to avoid the need
|
# be converted to local timezone and then stored and cached to avoid the need
|
||||||
# for any subsequent differentiation.
|
# for any subsequent differentiation.
|
||||||
#
|
#
|
||||||
# One last check is made before values are persisted to the database so that
|
|
||||||
# the original raw user entered value is not improperly type cast by the default
|
|
||||||
# Rails time type casting.
|
|
||||||
#
|
|
||||||
# A wholesale replacement of the Rails time type casting is not done to preserve
|
# A wholesale replacement of the Rails time type casting is not done to preserve
|
||||||
# the quick conversion for timestamp columns and also any value which is never
|
# the quick conversion for timestamp columns and also any value which is never
|
||||||
# touched during the life of the record object.
|
# touched during the life of the record object.
|
||||||
@ -35,22 +31,15 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
time.respond_to?(:in_time_zone) ? time.in_time_zone : time rescue time
|
time.respond_to?(:in_time_zone) ? time.in_time_zone : time rescue time
|
||||||
end
|
end
|
||||||
|
|
||||||
# checks if an attribute value has been cached as nil but has a nono-nil
|
|
||||||
# stored value which indicates the time value failed the type casting.
|
|
||||||
def failed_strict_time_type_cast?(attr_name)
|
|
||||||
attr_name = attr_name.to_s
|
|
||||||
@attributes_cache.has_key?(attr_name) && @attributes_cache[attr_name].nil? && !@attributes[attr_name].nil?
|
|
||||||
end
|
|
||||||
|
|
||||||
def read_attribute(attr_name)
|
def read_attribute(attr_name)
|
||||||
attr_name = attr_name.to_s
|
attr_name = attr_name.to_s
|
||||||
if !(value = @attributes[attr_name]).nil?
|
if !(value = @attributes[attr_name]).nil?
|
||||||
if column = column_for_attribute(attr_name)
|
if column = column_for_attribute(attr_name)
|
||||||
if unserializable_attribute?(attr_name, column)
|
if unserializable_attribute?(attr_name, column)
|
||||||
unserialize_attribute(attr_name)
|
unserialize_attribute(attr_name)
|
||||||
elsif column.klass == Time && failed_strict_time_type_cast?(attr_name)
|
elsif column.klass == Time && @attributes_cache.has_key?(attr_name)
|
||||||
nil
|
@attributes_cache[attr_name]
|
||||||
else
|
else
|
||||||
column.type_cast(value)
|
column.type_cast(value)
|
||||||
end
|
end
|
||||||
@ -70,9 +59,11 @@ module ValidatesTimeliness
|
|||||||
method_body = <<-EOV
|
method_body = <<-EOV
|
||||||
def #{attr_name}=(time)
|
def #{attr_name}=(time)
|
||||||
@attributes['#{attr_name}'] = time
|
@attributes['#{attr_name}'] = time
|
||||||
time = strict_time_type_cast(time)
|
unless time.acts_like?(:time)
|
||||||
|
time = strict_time_type_cast(time)
|
||||||
@attributes_cache['#{attr_name}'] = time.respond_to?(:in_time_zone) ? time.in_time_zone : time
|
end
|
||||||
|
time = time.respond_to?(:in_time_zone) ? time.in_time_zone : time
|
||||||
|
@attributes_cache['#{attr_name}'] = time
|
||||||
end
|
end
|
||||||
EOV
|
EOV
|
||||||
evaluate_attribute_method attr_name, method_body, "#{attr_name}="
|
evaluate_attribute_method attr_name, method_body, "#{attr_name}="
|
||||||
@ -114,7 +105,6 @@ module ValidatesTimeliness
|
|||||||
define_read_method_for_serialized_attribute(name)
|
define_read_method_for_serialized_attribute(name)
|
||||||
elsif column.klass == Time
|
elsif column.klass == Time
|
||||||
define_read_method_for_time_zone_conversion(name.to_sym)
|
define_read_method_for_time_zone_conversion(name.to_sym)
|
||||||
define_write_method_for_time_zone_conversion(name.to_sym)
|
|
||||||
else
|
else
|
||||||
define_read_method(name.to_sym, name, column)
|
define_read_method(name.to_sym, name, column)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -69,8 +69,7 @@ describe ValidatesTimeliness::AttributeMethods do
|
|||||||
Time.zone = 'Melbourne'
|
Time.zone = 'Melbourne'
|
||||||
time_string = "2000-06-01 01:02:03"
|
time_string = "2000-06-01 01:02:03"
|
||||||
@person.birth_date_and_time = time_string
|
@person.birth_date_and_time = time_string
|
||||||
@person.birth_date_and_time.utc_offset.should == 10.hours
|
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000'
|
||||||
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S').should == time_string
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return time object from database in correct timezone" do
|
it "should return time object from database in correct timezone" do
|
||||||
@ -80,8 +79,9 @@ describe ValidatesTimeliness::AttributeMethods do
|
|||||||
@person.birth_date_and_time = time_string
|
@person.birth_date_and_time = time_string
|
||||||
@person.save
|
@person.save
|
||||||
@person.reload
|
@person.reload
|
||||||
@person.birth_date_and_time.to_s(:db).should == time_string
|
@person.birth_date_and_time.strftime('%Y-%m-%d %H:%M:%S %Z %z').should == time_string + ' EST +1000'
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should return same time object on repeat reads" do
|
it "should return same time object on repeat reads" do
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user