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

Ticket #6107: reflection_should_respect_pluralization_setting-r6930.patch

File reflection_should_respect_pluralization_setting-r6930.patch, 4.0 kB (added by kamal, 3 years ago)

update patch file to patch cleanly against current edge (r6930)

  • activerecord/test/reflection_test.rb

    old new  
    9292  end 
    9393 
    9494  def test_has_many_reflection 
     95    classes = [Client, PluralClients, PluralThroughClients, Firm] 
     96     
    9597    reflection_for_clients = ActiveRecord::Reflection::AssociationReflection.new(:has_many, :clients, { :order => "id", :dependent => :destroy }, Firm) 
     98    reflection_for_plural_clients = ActiveRecord::Reflection::AssociationReflection.new(:has_many, :plural_clients, {}, Firm) 
     99    reflection_for_plural_through_clients = ActiveRecord::Reflection::AssociationReflection.new(:has_many, :plural_through_clients, { :through => :plural_clients }, Firm) 
    96100 
    97101    assert_equal reflection_for_clients, Firm.reflect_on_association(:clients) 
     102    assert_equal reflection_for_plural_clients , Firm.reflect_on_association(:plural_clients) 
     103    assert_equal reflection_for_plural_through_clients , Firm.reflect_on_association(:plural_through_clients) 
    98104 
    99105    assert_equal Client, Firm.reflect_on_association(:clients).klass 
    100106    assert_equal 'companies', Firm.reflect_on_association(:clients).table_name 
    101107 
    102108    assert_equal Client, Firm.reflect_on_association(:clients_of_firm).klass 
    103109    assert_equal 'companies', Firm.reflect_on_association(:clients_of_firm).table_name 
     110     
     111    ActiveRecord::Base.pluralize_table_names = false 
     112    classes.each(&:reset_table_name) 
     113     
     114    assert_equal PluralClients, Firm.reflect_on_association(:plural_clients).klass 
     115    assert_equal 'companies', Firm.reflect_on_association(:plural_clients).table_name 
     116     
     117    assert_equal PluralThroughClients, Firm.reflect_on_association(:plural_through_clients).klass 
     118    assert_equal 'companies', Firm.reflect_on_association(:plural_through_clients).table_name 
     119 
     120    ActiveRecord::Base.pluralize_table_names = true 
     121    classes.each(&:reset_table_name) 
    104122  end 
    105123 
    106124  def test_has_one_reflection 
     
    150168  end 
    151169   
    152170  def test_reflection_of_all_associations 
    153     assert_equal 17, Firm.reflect_on_all_associations.size 
    154     assert_equal 15, Firm.reflect_on_all_associations(:has_many).size 
     171    assert_equal 19, Firm.reflect_on_all_associations.size 
     172    assert_equal 17, Firm.reflect_on_all_associations(:has_many).size 
    155173    assert_equal 2, Firm.reflect_on_all_associations(:has_one).size 
    156174    assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size 
    157175  end 
  • activerecord/test/fixtures/company.rb

    old new  
    3939           :finder_sql  => 'SELECT * FROM companies WHERE client_of = 1000', 
    4040           :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000' 
    4141  has_many :plain_clients, :class_name => 'Client' 
     42  has_many :plural_clients 
     43  has_many :plural_through_clients, :through => :plural_clients 
    4244 
    4345  has_one :account, :foreign_key => "firm_id", :dependent => :destroy 
    4446end 
     
    111113      errors.add_on_empty "credit_limit" 
    112114    end 
    113115end 
     116 
     117class PluralClients < Client 
     118  has_many :plural_through_clients 
     119end 
     120 
     121class PluralThroughClients < Client 
     122end 
  • activerecord/lib/active_record/reflection.rb

    old new  
    200200            options[:source_type] || source_reflection.class_name 
    201201          else 
    202202            class_name = name.to_s.camelize 
    203             class_name = class_name.singularize if [ :has_many, :has_and_belongs_to_many ].include?(macro) 
     203            class_name = class_name.singularize if ActiveRecord::Base.pluralize_table_names && [ :has_many, :has_and_belongs_to_many ].include?(macro) 
    204204            class_name 
    205205          end 
    206206        end