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

Changeset 5416

Show
Ignore:
Timestamp:
11/02/06 20:20:46 (2 years ago)
Author:
bitsweat
Message:

Dynamically generate reader methods for serialized attributes. Closes #6362.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r5405 r5416  
    11*SVN* 
     2 
     3* Dynamically generate reader methods for serialized attributes.  #6362 [Stefan Kaes] 
    24 
    35* Deprecation: object transactions warning.  [Jeremy Kemper] 
  • trunk/activerecord/lib/active_record/base.rb

    r5359 r5416  
    18751875      def define_read_methods 
    18761876        self.class.columns_hash.each do |name, column| 
    1877           unless self.class.serialized_attributes[name] 
    1878             define_read_method(name.to_sym, name, column) unless respond_to_without_attributes?(name) 
    1879             define_question_method(name)     unless respond_to_without_attributes?("#{name}?") 
     1877          unless respond_to_without_attributes?(name) 
     1878            if self.class.serialized_attributes[name] 
     1879              define_read_method_for_serialized_attribute(name) 
     1880            else 
     1881              define_read_method(name.to_sym, name, column) 
     1882            end 
     1883          end 
     1884 
     1885          unless respond_to_without_attributes?("#{name}?") 
     1886            define_question_method(name) 
    18801887          end 
    18811888        end 
     
    18951902      end 
    18961903       
     1904      # Define read method for serialized attribute. 
     1905      def define_read_method_for_serialized_attribute(attr_name) 
     1906        unless attr_name.to_s == self.class.primary_key.to_s 
     1907          self.class.read_methods << attr_name 
     1908        end 
     1909         
     1910        evaluate_read_method attr_name, "def #{attr_name}; unserialize_attribute('#{attr_name}'); end" 
     1911      end 
     1912            
    18971913      # Define an attribute ? method. 
    18981914      def define_question_method(attr_name) 
  • trunk/activerecord/test/base_test.rb

    r5256 r5416  
    14771477  private 
    14781478    def assert_readers(model, exceptions) 
    1479       expected_readers = Set.new(model.column_names - (model.serialized_attributes.keys + ['id'])
     1479      expected_readers = Set.new(model.column_names - ['id']
    14801480      expected_readers += expected_readers.map { |col| "#{col}?" } 
    14811481      expected_readers -= exceptions