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

Ticket #9758: active_record_reflection_fix.patch

File active_record_reflection_fix.patch, 1.7 kB (added by mindforge, 9 months ago)
  • lib/active_record/reflection.rb

    old new  
    110110 
    111111      private 
    112112        def derive_class_name 
    113           name.to_s.camelize 
     113          parent_module = self.active_record.parent 
     114          [parent_module, name.to_s.camelize].reject{ |i| Object == i }.join('::') 
    114115        end 
    115116    end 
    116117 
     
    199200          if through_reflection 
    200201            options[:source_type] || source_reflection.class_name 
    201202          else 
    202             class_name = name.to_s.camelize 
     203            class_name = super 
    203204            class_name = class_name.singularize if [ :has_many, :has_and_belongs_to_many ].include?(macro) 
    204205            class_name 
    205206          end 
  • test/reflection_test.rb

    old new  
    165165    assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size 
    166166  end 
    167167 
     168  def test_reflection_class_name 
     169    reflection = ActiveRecord::Base.create_reflection(:has_many, :clients, {}, Firm) 
     170    assert_equal 'Client', reflection.class_name 
     171  end 
     172 
     173  def test_reflection_class_name_for_classes_nested_in_modules 
     174    reflection = ActiveRecord::Base.create_reflection(:has_many, :clients, {}, MyApplication::Business::Firm) 
     175    assert_equal 'MyApplication::Business::Client', reflection.class_name 
     176  end 
     177 
    168178  private 
    169179    def assert_reflection(klass, association, options) 
    170180      assert reflection = klass.reflect_on_association(association)