| | 1853 | |
|---|
| | 1854 | |
|---|
| | 1855 | class 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 |
|---|
| | 1917 | end |
|---|