mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
fixed read method to use value before type cast
added AR version checks in specs include AR from vendor if in Rails app
This commit is contained in:
@@ -7,25 +7,34 @@ module ValidatesTimeliness
|
||||
|
||||
module ClassMethods
|
||||
def define_read_method_for_time_zone_conversion(attr_name)
|
||||
method_body = <<-EOV
|
||||
method_body = <<-EOV
|
||||
def #{attr_name}(reload = false)
|
||||
cached = @attributes_cache['#{attr_name}']
|
||||
return cached if cached && !reload
|
||||
time = read_attribute('#{attr_name}')
|
||||
unless time.acts_like?(:time)
|
||||
time = time.to_time(:local) rescue nil
|
||||
time = read_attribute_before_type_cast('#{attr_name}')
|
||||
if time && time.acts_like?(:time)
|
||||
# Rails 2.0.x compatibility check
|
||||
time = time.respond_to?(:in_time_zone) ? time.in_time_zone : time
|
||||
elsif time
|
||||
# checks date is valid
|
||||
time.to_date rescue time = nil
|
||||
time = time.to_time(:local) if time
|
||||
end
|
||||
@attributes_cache['#{attr_name}'] = time.acts_like?(:time) ? time.in_time_zone : time
|
||||
@attributes_cache['#{attr_name}'] = time
|
||||
end
|
||||
EOV
|
||||
evaluate_attribute_method attr_name, method_body
|
||||
end
|
||||
|
||||
# TODO rails 2.0 time casting better with timezone
|
||||
def define_write_method_for_time_zone_conversion(attr_name)
|
||||
method_body = <<-EOV
|
||||
def #{attr_name}=(time)
|
||||
if time
|
||||
time = time.in_time_zone if time.acts_like?(:time)
|
||||
def #{attr_name}=(time)
|
||||
if time && time.respond_to?(:in_time_zone)
|
||||
time = time.in_time_zone
|
||||
elsif time && time.acts_like?(:time)
|
||||
# Rails 2.0.x compatibility
|
||||
time = @@default_timezone == :utc ? time.to_time : time.to_time
|
||||
end
|
||||
write_attribute(:#{attr_name}, time)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user