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:
Adam Meehan
2008-05-03 08:08:28 +10:00
parent 7a34881472
commit fe7310fc10
3 changed files with 47 additions and 18 deletions

View File

@@ -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