public
Fork of rails/rails
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/josh/rails.git
Merge [8046] from trunk: allow association redefinition in subclasses. 
References #9346.

git-svn-id: 
http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@8048 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
jeremy (author)
Sat Oct 27 14:15:17 -0700 2007
commit  65539c9f4d67ad6da7992645533602540c43740a
tree    999845196798b69f18cacc324803a9fb8cc9e3d2
parent  cf3e9664a41b8137556b53efc1b1dbaeae68c079
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *SVN*
0
 
0
+* Allow association redefinition in subclasses. #9346 [wildchild]
0
+
0
 * Fix has_many :through delete with custom foreign keys. #6466 [naffis]
0
 
0
 * Fix regression where the association would not construct new finder SQL on save causing bogus queries for "WHERE owner_id = NULL" even after owner was saved. #8713 [Bryan Helmkamp]
...
868
869
870
871
 
872
873
874
...
1292
1293
1294
1295
 
 
 
1296
1297
1298
...
868
869
870
 
871
872
873
874
...
1292
1293
1294
 
1295
1296
1297
1298
1299
1300
0
@@ -868,7 +868,7 @@ module ActiveRecord
0
         # Don't use a before_destroy callback since users' before_destroy
0
         # callbacks will be executed after the association is wiped out.
0
         old_method = "destroy_without_habtm_shim_for_#{reflection.name}"
0
- class_eval <<-end_eval
0
+ class_eval <<-end_eval unless method_defined?(old_method)
0
           alias_method :#{old_method}, :destroy_without_callbacks
0
           def destroy_without_callbacks
0
             #{reflection.name}.clear
0
@@ -1292,7 +1292,9 @@ module ActiveRecord
0
             defined_callbacks = options[callback_name.to_sym]
0
             if options.has_key?(callback_name.to_sym)
0
               class_inheritable_reader full_callback_name.to_sym
0
- write_inheritable_array(full_callback_name.to_sym, [defined_callbacks].flatten)
0
+ write_inheritable_attribute(full_callback_name.to_sym, [defined_callbacks].flatten)
0
+ else
0
+ write_inheritable_attribute(full_callback_name.to_sym, [])
0
             end
0
           end
0
         end
...
93
94
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
0
@@ -93,3 +93,21 @@ class AggregationsTest < Test::Unit::TestCase
0
     assert_raises(NoMethodError) { customers(:david).balance = nil }
0
   end
0
 end
0
+
0
+class OverridingAggregationsTest < Test::Unit::TestCase
0
+ class Name; end
0
+ class DifferentName; end
0
+
0
+ class Person < ActiveRecord::Base
0
+ composed_of :composed_of, :mapping => %w(person_first_name first_name)
0
+ end
0
+
0
+ class DifferentPerson < Person
0
+ composed_of :composed_of, :class_name => 'DifferentName', :mapping => %w(different_person_first_name first_name)
0
+ end
0
+
0
+ def test_composed_of_aggregation_redefinition_reflections_should_differ_and_not_inherited
0
+ assert_not_equal Person.reflect_on_aggregation(:composed_of),
0
+ DifferentPerson.reflect_on_aggregation(:composed_of)
0
+ end
0
+end
...
1850
1851
1852
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
0
@@ -1850,3 +1850,68 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase
0
     assert_equal %w(1 2), projects.scan(/\d/).sort
0
   end
0
 end
0
+
0
+
0
+class OverridingAssociationsTest < Test::Unit::TestCase
0
+ class Person < ActiveRecord::Base; end
0
+ class DifferentPerson < ActiveRecord::Base; end
0
+
0
+ class PeopleList < ActiveRecord::Base
0
+ has_and_belongs_to_many :has_and_belongs_to_many, :before_add => :enlist
0
+ has_many :has_many, :before_add => :enlist
0
+ belongs_to :belongs_to
0
+ has_one :has_one
0
+ end
0
+
0
+ class DifferentPeopleList < PeopleList
0
+ # Different association with the same name, callbacks should be omitted here.
0
+ has_and_belongs_to_many :has_and_belongs_to_many, :class_name => 'DifferentPerson'
0
+ has_many :has_many, :class_name => 'DifferentPerson'
0
+ belongs_to :belongs_to, :class_name => 'DifferentPerson', :foreign_key => 'belongs_to_id'
0
+ has_one :has_one, :class_name => 'DifferentPerson'
0
+ end
0
+
0
+ def test_habtm_association_redefinition_callbacks_should_differ_and_not_inherited
0
+ # redeclared association on AR descendant should not inherit callbacks from superclass
0
+ callbacks = PeopleList.read_inheritable_attribute(:before_add_for_has_and_belongs_to_many)
0
+ assert_equal([:enlist], callbacks)
0
+ callbacks = DifferentPeopleList.read_inheritable_attribute(:before_add_for_has_and_belongs_to_many)
0
+ assert_equal([], callbacks)
0
+ end
0
+
0
+ def test_has_many_association_redefinition_callbacks_should_differ_and_not_inherited
0
+ # redeclared association on AR descendant should not inherit callbacks from superclass
0
+ callbacks = PeopleList.read_inheritable_attribute(:before_add_for_has_many)
0
+ assert_equal([:enlist], callbacks)
0
+ callbacks = DifferentPeopleList.read_inheritable_attribute(:before_add_for_has_many)
0
+ assert_equal([], callbacks)
0
+ end
0
+
0
+ def test_habtm_association_redefinition_reflections_should_differ_and_not_inherited
0
+ assert_not_equal(
0
+ PeopleList.reflect_on_association(:has_and_belongs_to_many),
0
+ DifferentPeopleList.reflect_on_association(:has_and_belongs_to_many)
0
+ )
0
+ end
0
+
0
+ def test_has_many_association_redefinition_reflections_should_differ_and_not_inherited
0
+ assert_not_equal(
0
+ PeopleList.reflect_on_association(:has_many),
0
+ DifferentPeopleList.reflect_on_association(:has_many)
0
+ )
0
+ end
0
+
0
+ def test_belongs_to_association_redefinition_reflections_should_differ_and_not_inherited
0
+ assert_not_equal(
0
+ PeopleList.reflect_on_association(:belongs_to),
0
+ DifferentPeopleList.reflect_on_association(:belongs_to)
0
+ )
0
+ end
0
+
0
+ def test_has_one_association_redefinition_reflections_should_differ_and_not_inherited
0
+ assert_not_equal(
0
+ PeopleList.reflect_on_association(:has_one),
0
+ DifferentPeopleList.reflect_on_association(:has_one)
0
+ )
0
+ end
0
+end

Comments

    No one has commented yet.