diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2013-01-26 16:41:41 +0000 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2013-01-26 16:57:34 +0000 |
commit | 41ff6a10216f48f43605a1f9cd6094765cab750f (patch) | |
tree | 95b226b90d38bf906ee28ee118296ffd30a9264e | |
parent | bc982cbcb34129ea2cfe8aa1f8e0b40e444e68db (diff) | |
download | rails-41ff6a10216f48f43605a1f9cd6094765cab750f.tar.gz rails-41ff6a10216f48f43605a1f9cd6094765cab750f.tar.bz2 rails-41ff6a10216f48f43605a1f9cd6094765cab750f.zip |
Simplify type casting code for timezone aware attributes
With the addition of String#in_time_zone and Date#in_time_zone
we can simplify the type casting code by checking if the value
has an `in_time_zone` method.
-rw-r--r-- | activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb | 15 |
1 files changed, 5 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb index e3c5dc4823..41b5a6e926 100644 --- a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb +++ b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb @@ -33,17 +33,12 @@ module ActiveRecord def define_method_attribute=(attr_name) if create_time_zone_conversion_attribute?(attr_name, columns_hash[attr_name]) method_body, line = <<-EOV, __LINE__ + 1 - def #{attr_name}=(original_time) - original_time = nil if original_time.blank? - time = original_time - unless time.acts_like?(:time) - time = time.is_a?(String) ? Time.zone.parse(time) : time.to_time rescue time - end - time = time.in_time_zone rescue nil if time + def #{attr_name}=(time) + time_with_zone = time.respond_to?(:in_time_zone) ? time.in_time_zone : nil previous_time = attribute_changed?("#{attr_name}") ? changed_attributes["#{attr_name}"] : read_attribute(:#{attr_name}) - write_attribute(:#{attr_name}, original_time) - #{attr_name}_will_change! if previous_time != time - @attributes_cache["#{attr_name}"] = time + write_attribute(:#{attr_name}, time) + #{attr_name}_will_change! if previous_time != time_with_zone + @attributes_cache["#{attr_name}"] = time_with_zone end EOV generated_attribute_methods.module_eval(method_body, __FILE__, line) |