Ticket #10982: time_zone_ar_methods_draft-1.diff
| File time_zone_ar_methods_draft-1.diff, 5.6 kB (added by technoweenie, 5 months ago) |
|---|
-
trunk/activesupport/lib/active_support/core_ext/time/zones.rb
old new 9 9 end 10 10 11 11 module ClassMethods 12 attr_accessor :zone_default 13 12 14 def zone 13 Thread.current[:time_zone] 15 Thread.current[:time_zone] || zone_default 14 16 end 15 17 16 18 # Sets a global default time zone, separate from the system time zone in ENV['TZ']. -
trunk/activerecord/lib/active_record/attribute_methods.rb
old new 8 8 base.attribute_method_suffix(*DEFAULT_SUFFIXES) 9 9 base.cattr_accessor :attribute_types_cached_by_default, :instance_writer => false 10 10 base.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT 11 base.cattr_accessor :time_zone_aware_attributes, :instance_writer => false 12 base.convert_time_attributes_to_current_time_zone = false 13 base.cattr_accessor :skip_time_zone_conversion_for_attributes, :instance_writer => false 14 base.skip_time_zone_conversion_for_attributes = [] 11 15 end 12 16 13 17 # Declare and check for suffixed attribute methods. … … 64 68 unless instance_method_already_implemented?(name) 65 69 if self.serialized_attributes[name] 66 70 define_read_method_for_serialized_attribute(name) 71 elsif create_time_zone_conversion_attribute?(name, column) 72 define_read_method_for_time_zone_conversion(name) 67 73 else 68 74 define_read_method(name.to_sym, name, column) 69 75 end 70 76 end 71 77 72 78 unless instance_method_already_implemented?("#{name}=") 73 define_write_method(name.to_sym) 79 if create_time_zone_conversion_attribute?(name, column) 80 define_write_method_for_time_zone_conversion(name) 81 else 82 define_write_method(name.to_sym) 83 end 74 84 end 75 85 76 86 unless instance_method_already_implemented?("#{name}?") … … 121 131 @@attribute_method_suffixes ||= [] 122 132 end 123 133 134 def create_time_zone_conversion_attribute?(name, column) 135 convert_time_attributes_to_current_time_zone && !skip_time_zone_conversion_for_attributes.include?(name.to_sym) && [:datetime, :timestamp].include?(column.type) 136 end 137 124 138 # Define an attribute reader method. Cope with nil column. 125 139 def define_read_method(symbol, attr_name, column) 126 140 cast_code = column.type_cast_code('v') if column … … 140 154 def define_read_method_for_serialized_attribute(attr_name) 141 155 evaluate_attribute_method attr_name, "def #{attr_name}; unserialize_attribute('#{attr_name}'); end" 142 156 end 157 158 def define_read_method_for_time_zone_conversion(attr_name) 159 method_body = <<-EOV 160 def #{attr_name}(reload = false) 161 cached = @attributes_cache['#{attr_name}'] 162 return cached if cached && !reload 163 time = read_attribute('#{attr_name}') 164 @attributes_cache['#{attr_name}'] = time.acts_like?(:time) ? time.in_current_time_zone : time 165 end 166 EOV 167 evaluate_attribute_method attr_name, method_body 168 end 143 169 144 170 # Define an attribute ? method. 145 171 def define_question_method(attr_name) … … 149 175 def define_write_method(attr_name) 150 176 evaluate_attribute_method attr_name, "def #{attr_name}=(new_value);write_attribute('#{attr_name}', new_value);end", "#{attr_name}=" 151 177 end 178 179 def define_write_method_for_time_zone_conversion(attr_name) 180 method_body = <<-EOV 181 def #{attr_name}=(time) 182 if time 183 time = time.to_time rescue time unless time.acts_like?(:time) 184 time = time.change_time_zone_to_current if time.acts_like?(:time) 185 end 186 write_attribute(:#{attr_name}, time) 187 end 188 EOV 189 evaluate_attribute_method attr_name, method_body, "#{attr_name}=" 190 end 152 191 153 192 # Evaluate the definition for an attribute related method 154 193 def evaluate_attribute_method(attr_name, method_definition, method_name=attr_name) -
trunk/railties/lib/initializer.rb
old new 81 81 initialize_dependency_mechanism 82 82 initialize_whiny_nils 83 83 initialize_temporary_session_directory 84 initialize_time_zone 84 85 initialize_framework_settings 85 86 86 87 add_support_load_paths … … 316 317 end 317 318 end 318 319 320 def initialize_time_zone 321 if configuration.time_zone 322 Time.zone_default = TimeZone[configuration.time_zone] 323 if configuration.frameworks.include?(:active_record) 324 ActiveRecord::Base.time_zone_aware_attributes = true 325 ActiveRecord::Base.default_timezone = :utc 326 end 327 end 328 end 329 319 330 # Initializes framework-specific settings for each of the loaded frameworks 320 331 # (Configuration#frameworks). The available settings map to the accessors 321 332 # on each of the corresponding Base classes.