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

Changeset 7500

Show
Ignore:
Timestamp:
09/17/07 09:29:02 (1 year ago)
Author:
nzkoz
Message:

Ensure that custom mutators aren't redefined by define_attribute_methods. [Koz]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/attribute_methods.rb

    r7406 r7500  
    5959        return if generated_methods? 
    6060        columns_hash.each do |name, column| 
    61           unless instance_methods.include?(name) 
     61          unless instance_method_already_defined?(name) 
    6262            if self.serialized_attributes[name] 
    6363              define_read_method_for_serialized_attribute(name) 
     
    6767          end 
    6868 
    69           unless instance_methods.include?("#{name}=") 
     69          unless instance_method_already_defined?("#{name}=") 
    7070            define_write_method(name.to_sym) 
    7171          end 
    7272 
    73           unless instance_methods.include?("#{name}?") 
     73          unless instance_method_already_defined?("#{name}?") 
    7474            define_question_method(name) 
    7575          end 
    7676        end 
    7777      end 
     78 
     79      def instance_method_already_defined?(method_name) 
     80        method_defined?(method_name) ||  
     81          private_method_defined?(method_name) || 
     82          protected_method_defined?(method_name) 
     83      end 
     84 
    7885      alias :define_read_methods :define_attribute_methods 
    7986 
  • trunk/activerecord/test/base_test.rb

    r7371 r7500  
    395395    end 
    396396  end 
     397   
     398  def test_custom_mutator 
     399    topic = Topic.find(1) 
     400    # This mutator is protected in the class definition 
     401    topic.send(:approved=, true) 
     402    assert topic.instance_variable_get("@custom_approved") 
     403  end 
    397404 
    398405  def test_destroy 
  • trunk/activerecord/test/fixtures/topic.rb

    r4909 r7500  
    1414    id 
    1515  end 
     16   
    1617 
    1718  protected 
     19    def approved=(val) 
     20      @custom_approved = val 
     21      write_attribute(:approved, val) 
     22    end 
     23 
    1824    def default_written_on 
    1925      self.written_on = Time.now unless attribute_present?("written_on")