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

Changeset 8048

Show
Ignore:
Timestamp:
10/27/07 21:15:17 (11 months ago)
Author:
bitsweat
Message:

Merge [8046] from trunk: allow association redefinition in subclasses. References #9346.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1-2-stable/activerecord/CHANGELOG

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

    r8042 r8048  
    869869        # callbacks will be executed after the association is wiped out. 
    870870        old_method = "destroy_without_habtm_shim_for_#{reflection.name}" 
    871         class_eval <<-end_eval 
     871        class_eval <<-end_eval unless method_defined?(old_method) 
    872872          alias_method :#{old_method}, :destroy_without_callbacks 
    873873          def destroy_without_callbacks 
     
    12931293            if options.has_key?(callback_name.to_sym) 
    12941294              class_inheritable_reader full_callback_name.to_sym 
    1295               write_inheritable_array(full_callback_name.to_sym, [defined_callbacks].flatten) 
     1295              write_inheritable_attribute(full_callback_name.to_sym, [defined_callbacks].flatten) 
     1296            else 
     1297              write_inheritable_attribute(full_callback_name.to_sym, []) 
    12961298            end 
    12971299          end 
  • branches/1-2-stable/activerecord/test/aggregations_test.rb

    r4353 r8048  
    9494  end 
    9595end 
     96 
     97class OverridingAggregationsTest < Test::Unit::TestCase 
     98  class Name; end 
     99  class DifferentName; end 
     100 
     101  class Person   < ActiveRecord::Base 
     102    composed_of :composed_of, :mapping => %w(person_first_name first_name) 
     103  end 
     104 
     105  class DifferentPerson < Person 
     106    composed_of :composed_of, :class_name => 'DifferentName', :mapping => %w(different_person_first_name first_name) 
     107  end 
     108 
     109  def test_composed_of_aggregation_redefinition_reflections_should_differ_and_not_inherited 
     110    assert_not_equal Person.reflect_on_aggregation(:composed_of), 
     111                     DifferentPerson.reflect_on_aggregation(:composed_of) 
     112  end 
     113end 
  • branches/1-2-stable/activerecord/test/associations_test.rb

    r7940 r8048  
    18511851  end 
    18521852end 
     1853 
     1854 
     1855class OverridingAssociationsTest < Test::Unit::TestCase 
     1856  class Person < ActiveRecord::Base; end 
     1857  class DifferentPerson < ActiveRecord::Base; end 
     1858 
     1859  class PeopleList < ActiveRecord::Base 
     1860    has_and_belongs_to_many :has_and_belongs_to_many, :before_add => :enlist 
     1861    has_many :has_many, :before_add => :enlist 
     1862    belongs_to :belongs_to 
     1863    has_one :has_one 
     1864  end 
     1865 
     1866  class DifferentPeopleList < PeopleList 
     1867    # Different association with the same name, callbacks should be omitted here. 
     1868    has_and_belongs_to_many :has_and_belongs_to_many, :class_name => 'DifferentPerson' 
     1869    has_many :has_many, :class_name => 'DifferentPerson' 
     1870    belongs_to :belongs_to, :class_name => 'DifferentPerson', :foreign_key => 'belongs_to_id' 
     1871    has_one :has_one, :class_name => 'DifferentPerson' 
     1872  end 
     1873 
     1874  def test_habtm_association_redefinition_callbacks_should_differ_and_not_inherited 
     1875    # redeclared association on AR descendant should not inherit callbacks from superclass 
     1876    callbacks = PeopleList.read_inheritable_attribute(:before_add_for_has_and_belongs_to_many) 
     1877    assert_equal([:enlist], callbacks) 
     1878    callbacks = DifferentPeopleList.read_inheritable_attribute(:before_add_for_has_and_belongs_to_many) 
     1879    assert_equal([], callbacks) 
     1880  end 
     1881 
     1882  def test_has_many_association_redefinition_callbacks_should_differ_and_not_inherited 
     1883    # redeclared association on AR descendant should not inherit callbacks from superclass 
     1884    callbacks = PeopleList.read_inheritable_attribute(:before_add_for_has_many) 
     1885    assert_equal([:enlist], callbacks) 
     1886    callbacks = DifferentPeopleList.read_inheritable_attribute(:before_add_for_has_many) 
     1887    assert_equal([], callbacks) 
     1888  end 
     1889 
     1890  def test_habtm_association_redefinition_reflections_should_differ_and_not_inherited 
     1891    assert_not_equal( 
     1892      PeopleList.reflect_on_association(:has_and_belongs_to_many), 
     1893      DifferentPeopleList.reflect_on_association(:has_and_belongs_to_many) 
     1894    ) 
     1895  end 
     1896 
     1897  def test_has_many_association_redefinition_reflections_should_differ_and_not_inherited 
     1898    assert_not_equal( 
     1899      PeopleList.reflect_on_association(:has_many), 
     1900      DifferentPeopleList.reflect_on_association(:has_many) 
     1901    ) 
     1902  end 
     1903 
     1904  def test_belongs_to_association_redefinition_reflections_should_differ_and_not_inherited 
     1905    assert_not_equal( 
     1906      PeopleList.reflect_on_association(:belongs_to), 
     1907      DifferentPeopleList.reflect_on_association(:belongs_to) 
     1908    ) 
     1909  end 
     1910 
     1911  def test_has_one_association_redefinition_reflections_should_differ_and_not_inherited 
     1912    assert_not_equal( 
     1913      PeopleList.reflect_on_association(:has_one), 
     1914      DifferentPeopleList.reflect_on_association(:has_one) 
     1915    ) 
     1916  end 
     1917end