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

Changeset 4635

Show
Ignore:
Timestamp:
07/31/06 06:54:06 (2 years ago)
Author:
bitsweat
Message:

r4880@ks: jeremy | 2006-07-30 23:52:59 -0700
Only set method_name = md.pre_match if the pre_match is an attribute. Plays nicely with other ? suffixed attribute methods.
r4881@ks: jeremy | 2006-07-30 23:53:37 -0700
Heavier testing for attribute method suffixes.

Files:

Legend:

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

    r4632 r4635  
    17641764        if @attributes.include?(method_name) or 
    17651765            (md = /\?$/.match(method_name) and 
    1766             @attributes.include?(method_name = md.pre_match)) 
     1766            @attributes.include?(query_method_name = md.pre_match) and 
     1767            method_name = query_method_name) 
    17671768          define_read_methods if self.class.read_methods.empty? && self.class.generate_read_methods 
    17681769          md ? query_attribute(method_name) : read_attribute(method_name) 
  • trunk/activerecord/test/attribute_methods_test.rb

    r4632 r4635  
    33class AttributeMethodsTest < Test::Unit::TestCase 
    44  def setup 
     5    @old_suffixes = ActiveRecord::Base.send(:attribute_method_suffixes).dup 
    56    @target = Class.new(ActiveRecord::Base) 
    67    @target.table_name = 'topics' 
    78  end 
     9 
     10  def teardown 
     11    ActiveRecord::Base.send(:attribute_method_suffixes).clear 
     12    ActiveRecord::Base.attribute_method_suffix *@old_suffixes 
     13  end 
     14 
    815 
    916  def test_match_attribute_method_query_returns_match_data 
     
    1118    assert_equal 'title', md.pre_match 
    1219    assert_equal ['='], md.captures 
     20 
     21    %w(_hello_world ist! _maybe?).each do |suffix| 
     22      @target.class_eval "def attribute#{suffix}(*args) args end" 
     23      @target.attribute_method_suffix suffix 
     24 
     25      assert_not_nil md = @target.match_attribute_method?("title#{suffix}") 
     26      assert_equal 'title', md.pre_match 
     27      assert_equal [suffix], md.captures 
     28    end 
    1329  end 
    1430 
     
    2036    assert_raise(NoMethodError) { topic.title_hello_world } 
    2137 
    22     @target.class_eval "def attribute_hello_world(*args) args end" 
    23     @target.attribute_method_suffix '_hello_world' 
     38    %w(_hello_world _it! _candidate= able?).each do |suffix| 
     39      @target.class_eval "def attribute#{suffix}(*args) args end" 
     40      @target.attribute_method_suffix suffix 
    2441 
    25     assert topic.respond_to?('title_hello_world') 
    26     assert_equal ['title'], topic.title_hello_world 
    27     assert_equal ['title', 'a'], topic.title_hello_world('a') 
    28     assert_equal ['title', 1, 2, 3], topic.title_hello_world(1, 2, 3) 
     42      meth = "title#{suffix}" 
     43      assert topic.respond_to?(meth) 
     44      assert_equal ['title'], topic.send(meth) 
     45      assert_equal ['title', 'a'], topic.send(meth, 'a') 
     46      assert_equal ['title', 1, 2, 3], topic.send(meth, 1, 2, 3) 
     47    end 
    2948  end 
    3049end