| | 1935 | |
|---|
| | 1936 | |
|---|
| | 1937 | class 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 |
|---|
| | 1999 | end |
|---|