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

Changeset 8046

Show
Ignore:
Timestamp:
10/27/07 20:31:09 (1 year ago)
Author:
bitsweat
Message:

Allow association redefinition in subclasses. Closes #9346.

Files:

Legend:

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

    r8043 r8046  
    11*SVN* 
     2 
     3* Allow association redefinition in subclasses.  #9346 [wildchild] 
    24 
    35* Fix has_many :through delete with custom foreign keys.  #6466 [naffis] 
  • trunk/activerecord/lib/active_record/associations.rb

    r8043 r8046  
    956956        # callbacks will be executed after the association is wiped out. 
    957957        old_method = "destroy_without_habtm_shim_for_#{reflection.name}" 
    958         class_eval <<-end_eval 
     958        class_eval <<-end_eval unless method_defined?(old_method) 
    959959          alias_method :#{old_method}, :destroy_without_callbacks 
    960960          def destroy_without_callbacks 
     
    13521352            if options.has_key?(callback_name.to_sym) 
    13531353              class_inheritable_reader full_callback_name.to_sym 
    1354               write_inheritable_array(full_callback_name.to_sym, [defined_callbacks].flatten) 
     1354              write_inheritable_attribute(full_callback_name.to_sym, [defined_callbacks].flatten) 
     1355            else 
     1356              write_inheritable_attribute(full_callback_name.to_sym, []) 
    13551357            end 
    13561358          end 
  • trunk/activerecord/test/aggregations_test.rb

    r8002 r8046  
    109109  end 
    110110end 
     111 
     112class OverridingAggregationsTest < Test::Unit::TestCase 
     113  class Name; end 
     114  class DifferentName; end 
     115 
     116  class Person   < ActiveRecord::Base 
     117    composed_of :composed_of, :mapping => %w(person_first_name first_name) 
     118  end 
     119 
     120  class DifferentPerson < Person 
     121    composed_of :composed_of, :class_name => 'DifferentName', :mapping => %w(different_person_first_name first_name) 
     122  end 
     123 
     124  def test_composed_of_aggregation_redefinition_reflections_should_differ_and_not_inherited 
     125    assert_not_equal Person.reflect_on_aggregation(:composed_of), 
     126                     DifferentPerson.reflect_on_aggregation(:composed_of) 
     127  end 
     128end 
  • trunk/activerecord/test/associations_test.rb

    r8036 r8046  
    19331933  end 
    19341934end 
     1935 
     1936 
     1937class OverridingAssociationsTest < Test::Unit::TestCase 
     1938  class Person < ActiveRecord::Base; end 
     1939  class DifferentPerson < ActiveRecord::Base; end 
     1940 
     1941  class PeopleList < ActiveRecord::Base 
     1942    has_and_belongs_to_many :has_and_belongs_to_many, :before_add => :enlist 
     1943    has_many :has_many, :before_add => :enlist 
     1944    belongs_to :belongs_to 
     1945    has_one :has_one 
     1946  end 
     1947 
     1948  class DifferentPeopleList < PeopleList 
     1949    # Different association with the same name, callbacks should be omitted here. 
     1950    has_and_belongs_to_many :has_and_belongs_to_many, :class_name => 'DifferentPerson' 
     1951    has_many :has_many, :class_name => 'DifferentPerson' 
     1952    belongs_to :belongs_to, :class_name => 'DifferentPerson' 
     1953    has_one :has_one, :class_name => 'DifferentPerson' 
     1954  end 
     1955 
     1956  def test_habtm_association_redefinition_callbacks_should_differ_and_not_inherited 
     1957    # redeclared association on AR descendant should not inherit callbacks from superclass 
     1958    callbacks = PeopleList.read_inheritable_attribute(:before_add_for_has_and_belongs_to_many) 
     1959    assert_equal([:enlist], callbacks) 
     1960    callbacks = DifferentPeopleList.read_inheritable_attribute(:before_add_for_has_and_belongs_to_many) 
     1961    assert_equal([], callbacks) 
     1962  end 
     1963 
     1964  def test_has_many_association_redefinition_callbacks_should_differ_and_not_inherited 
     1965    # redeclared association on AR descendant should not inherit callbacks from superclass 
     1966    callbacks = PeopleList.read_inheritable_attribute(:before_add_for_has_many) 
     1967    assert_equal([:enlist], callbacks) 
     1968    callbacks = DifferentPeopleList.read_inheritable_attribute(:before_add_for_has_many) 
     1969    assert_equal([], callbacks) 
     1970  end 
     1971 
     1972  def test_habtm_association_redefinition_reflections_should_differ_and_not_inherited 
     1973    assert_not_equal( 
     1974      PeopleList.reflect_on_association(:has_and_belongs_to_many), 
     1975      DifferentPeopleList.reflect_on_association(:has_and_belongs_to_many) 
     1976    ) 
     1977  end 
     1978 
     1979  def test_has_many_association_redefinition_reflections_should_differ_and_not_inherited 
     1980    assert_not_equal( 
     1981      PeopleList.reflect_on_association(:has_many), 
     1982      DifferentPeopleList.reflect_on_association(:has_many) 
     1983    ) 
     1984  end 
     1985 
     1986  def test_belongs_to_association_redefinition_reflections_should_differ_and_not_inherited 
     1987    assert_not_equal( 
     1988      PeopleList.reflect_on_association(:belongs_to), 
     1989      DifferentPeopleList.reflect_on_association(:belongs_to) 
     1990    ) 
     1991  end 
     1992 
     1993  def test_has_one_association_redefinition_reflections_should_differ_and_not_inherited 
     1994    assert_not_equal( 
     1995      PeopleList.reflect_on_association(:has_one), 
     1996      DifferentPeopleList.reflect_on_association(:has_one) 
     1997    ) 
     1998  end 
     1999end