Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

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)

Geoff's first patch

  • trunk/activesupport/lib/active_support/core_ext/time/zones.rb

    old new  
    99        end 
    1010         
    1111        module ClassMethods 
     12          attr_accessor :zone_default 
     13           
    1214          def zone 
    13             Thread.current[:time_zone] 
     15            Thread.current[:time_zone] || zone_default 
    1416          end 
    1517 
    1618          # 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  
    88      base.attribute_method_suffix(*DEFAULT_SUFFIXES) 
    99      base.cattr_accessor :attribute_types_cached_by_default, :instance_writer => false 
    1010      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 = [] 
    1115    end 
    1216 
    1317    # Declare and check for suffixed attribute methods. 
     
    6468          unless instance_method_already_implemented?(name) 
    6569            if self.serialized_attributes[name] 
    6670              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) 
    6773            else 
    6874              define_read_method(name.to_sym, name, column) 
    6975            end 
    7076          end 
    7177 
    7278          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 
    7484          end 
    7585 
    7686          unless instance_method_already_implemented?("#{name}?") 
     
    121131          @@attribute_method_suffixes ||= [] 
    122132        end 
    123133         
     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         
    124138        # Define an attribute reader method.  Cope with nil column. 
    125139        def define_read_method(symbol, attr_name, column) 
    126140          cast_code = column.type_cast_code('v') if column 
     
    140154        def define_read_method_for_serialized_attribute(attr_name) 
    141155          evaluate_attribute_method attr_name, "def #{attr_name}; unserialize_attribute('#{attr_name}'); end" 
    142156        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 
    143169 
    144170        # Define an attribute ? method. 
    145171        def define_question_method(attr_name) 
     
    149175        def define_write_method(attr_name) 
    150176          evaluate_attribute_method attr_name, "def #{attr_name}=(new_value);write_attribute('#{attr_name}', new_value);end", "#{attr_name}=" 
    151177        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 
    152191 
    153192        # Evaluate the definition for an attribute related method 
    154193        def evaluate_attribute_method(attr_name, method_definition, method_name=attr_name) 
  • trunk/railties/lib/initializer.rb

    old new  
    8181      initialize_dependency_mechanism 
    8282      initialize_whiny_nils 
    8383      initialize_temporary_session_directory 
     84      initialize_time_zone 
    8485      initialize_framework_settings 
    8586 
    8687      add_support_load_paths 
     
    316317      end 
    317318    end 
    318319 
     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 
    319330    # Initializes framework-specific settings for each of the loaded frameworks 
    320331    # (Configuration#frameworks). The available settings map to the accessors 
    321332    # on each of the corresponding Base classes.