diff --git a/activerecord/test/assets/schema_dump_5_1.yml b/activerecord/test/assets/schema_dump_5_1.yml index f37977daf24be5158b30e2ed49476af7a33cd649..9563b7e100526fd76e64bda3641a17371c545151 100644 --- a/activerecord/test/assets/schema_dump_5_1.yml +++ b/activerecord/test/assets/schema_dump_5_1.yml @@ -316,7 +316,7 @@ data_sources: triangles: true non_poly_ones: true non_poly_twos: true - men: true + humans: true faces: true interests: true zines: true diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index de9742b2501844e961f8215e37381b8ff5606da3..9a4822aae58cfd8cf45db725039075da868ce57a 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -120,7 +120,7 @@ class Sink < ActiveRecord::Base end class Source < ActiveRecord::Base - self.table_name = "men" + self.table_name = "humans" has_and_belongs_to_many :sinks, join_table: :edges end diff --git a/activerecord/test/cases/associations/inverse_associations_test.rb b/activerecord/test/cases/associations/inverse_associations_test.rb index 4d71e71c8b0c6620e05888912acc96c51dff5354..b9ee7c86396cc1dc3206a988569ce0d0f85c1e8f 100644 --- a/activerecord/test/cases/associations/inverse_associations_test.rb +++ b/activerecord/test/cases/associations/inverse_associations_test.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require "cases/helper" -require "models/man" +require "models/human" require "models/face" require "models/interest" require "models/zine" @@ -26,14 +26,14 @@ class AutomaticInverseFindingTests < ActiveRecord::TestCase fixtures :ratings, :comments, :cars def test_has_one_and_belongs_to_should_find_inverse_automatically_on_multiple_word_name - monkey_reflection = MixedCaseMonkey.reflect_on_association(:man) - man_reflection = Man.reflect_on_association(:mixed_case_monkey) + monkey_reflection = MixedCaseMonkey.reflect_on_association(:human) + human_reflection = Human.reflect_on_association(:mixed_case_monkey) assert monkey_reflection.has_inverse?, "The monkey reflection should have an inverse" - assert_equal man_reflection, monkey_reflection.inverse_of, "The monkey reflection's inverse should be the man reflection" + assert_equal human_reflection, monkey_reflection.inverse_of, "The monkey reflection's inverse should be the human reflection" - assert man_reflection.has_inverse?, "The man reflection should have an inverse" - assert_equal monkey_reflection, man_reflection.inverse_of, "The man reflection's inverse should be the monkey reflection" + assert human_reflection.has_inverse?, "The human reflection should have an inverse" + assert_equal monkey_reflection, human_reflection.inverse_of, "The human reflection's inverse should be the monkey reflection" end def test_has_many_and_belongs_to_should_find_inverse_automatically_for_model_in_module @@ -136,9 +136,9 @@ def test_polymorphic_and_has_many_through_relationships_should_not_have_inverses end def test_polymorphic_has_one_should_find_inverse_automatically - man_reflection = Man.reflect_on_association(:polymorphic_face_without_inverse) + human_reflection = Human.reflect_on_association(:polymorphic_face_without_inverse) - assert_predicate man_reflection, :has_inverse? + assert_predicate human_reflection, :has_inverse? end end @@ -158,13 +158,13 @@ def test_should_allow_for_inverse_of_options_in_associations end def test_should_be_able_to_ask_a_reflection_if_it_has_an_inverse - has_one_with_inverse_ref = Man.reflect_on_association(:face) + has_one_with_inverse_ref = Human.reflect_on_association(:face) assert_predicate has_one_with_inverse_ref, :has_inverse? - has_many_with_inverse_ref = Man.reflect_on_association(:interests) + has_many_with_inverse_ref = Human.reflect_on_association(:interests) assert_predicate has_many_with_inverse_ref, :has_inverse? - belongs_to_with_inverse_ref = Face.reflect_on_association(:man) + belongs_to_with_inverse_ref = Face.reflect_on_association(:human) assert_predicate belongs_to_with_inverse_ref, :has_inverse? has_one_without_inverse_ref = Club.reflect_on_association(:sponsor) @@ -178,14 +178,14 @@ def test_should_be_able_to_ask_a_reflection_if_it_has_an_inverse end def test_inverse_of_method_should_supply_the_actual_reflection_instance_it_is_the_inverse_of - has_one_ref = Man.reflect_on_association(:face) - assert_equal Face.reflect_on_association(:man), has_one_ref.inverse_of + has_one_ref = Human.reflect_on_association(:face) + assert_equal Face.reflect_on_association(:human), has_one_ref.inverse_of - has_many_ref = Man.reflect_on_association(:interests) - assert_equal Interest.reflect_on_association(:man), has_many_ref.inverse_of + has_many_ref = Human.reflect_on_association(:interests) + assert_equal Interest.reflect_on_association(:human), has_many_ref.inverse_of - belongs_to_ref = Face.reflect_on_association(:man) - assert_equal Man.reflect_on_association(:face), belongs_to_ref.inverse_of + belongs_to_ref = Face.reflect_on_association(:human) + assert_equal Human.reflect_on_association(:face), belongs_to_ref.inverse_of end def test_associations_with_no_inverse_of_should_return_nil @@ -204,7 +204,7 @@ def test_polymorphic_associations_dont_attempt_to_find_inverse_of assert_raise(ArgumentError) { belongs_to_ref.klass } assert_nil belongs_to_ref.inverse_of - belongs_to_ref = Face.reflect_on_association(:human) + belongs_to_ref = Face.reflect_on_association(:super_human) assert_raise(ArgumentError) { belongs_to_ref.klass } assert_nil belongs_to_ref.inverse_of end @@ -221,269 +221,269 @@ def test_this_inverse_stuff end class InverseHasOneTests < ActiveRecord::TestCase - fixtures :men, :faces + fixtures :humans, :faces def test_parent_instance_should_be_shared_with_child_on_find - m = men(:gordon) - f = m.face - assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance" - f.man.name = "Mungo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance" + human = humans(:gordon) + face = human.face + assert_equal human.name, face.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to parent instance" + face.human.name = "Mungo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to child-owned instance" end def test_parent_instance_should_be_shared_with_eager_loaded_child_on_find - m = Man.all.merge!(where: { name: "Gordon" }, includes: :face).first - f = m.face - assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance" - f.man.name = "Mungo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance" - - m = Man.all.merge!(where: { name: "Gordon" }, includes: :face, order: "faces.id").first - f = m.face - assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance" - f.man.name = "Mungo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to child-owned instance" + human = Human.all.merge!(where: { name: "Gordon" }, includes: :face).first + face = human.face + assert_equal human.name, face.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to parent instance" + face.human.name = "Mungo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to child-owned instance" + + human = Human.all.merge!(where: { name: "Gordon" }, includes: :face, order: "faces.id").first + face = human.face + assert_equal human.name, face.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to parent instance" + face.human.name = "Mungo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to child-owned instance" end def test_parent_instance_should_be_shared_with_newly_built_child - m = Man.first - f = m.build_face(description: "haunted") - assert_not_nil f.man - assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance" - f.man.name = "Mungo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to just-built-child-owned instance" + human = Human.first + face = human.build_face(description: "haunted") + assert_not_nil face.human + assert_equal human.name, face.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to parent instance" + face.human.name = "Mungo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to just-built-child-owned instance" end def test_parent_instance_should_be_shared_with_newly_created_child - m = Man.first - f = m.create_face(description: "haunted") - assert_not_nil f.man - assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance" - f.man.name = "Mungo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance" + human = Human.first + face = human.create_face(description: "haunted") + assert_not_nil face.human + assert_equal human.name, face.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to parent instance" + face.human.name = "Mungo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to newly-created-child-owned instance" end def test_parent_instance_should_be_shared_with_newly_created_child_via_bang_method - m = Man.first - f = m.create_face!(description: "haunted") - assert_not_nil f.man - assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance" - f.man.name = "Mungo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to newly-created-child-owned instance" + human = Human.first + face = human.create_face!(description: "haunted") + assert_not_nil face.human + assert_equal human.name, face.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to parent instance" + face.human.name = "Mungo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to newly-created-child-owned instance" end def test_parent_instance_should_be_shared_with_replaced_via_accessor_child - m = Man.first - f = Face.new(description: "haunted") - m.face = f - assert_not_nil f.man - assert_equal m.name, f.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to parent instance" - f.man.name = "Mungo" - assert_equal m.name, f.man.name, "Name of man should be the same after changes to replaced-child-owned instance" + human = Human.first + face = Face.new(description: "haunted") + human.face = face + assert_not_nil face.human + assert_equal human.name, face.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to parent instance" + face.human.name = "Mungo" + assert_equal human.name, face.human.name, "Name of human should be the same after changes to replaced-child-owned instance" end def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error - assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.first.dirty_face } + assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Human.first.dirty_face } end end class InverseHasManyTests < ActiveRecord::TestCase - fixtures :men, :interests, :posts, :authors, :author_addresses + fixtures :humans, :interests, :posts, :authors, :author_addresses def test_parent_instance_should_be_shared_with_every_child_on_find - m = men(:gordon) - is = m.interests - is.each do |i| - assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance" - i.man.name = "Mungo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance" + human = humans(:gordon) + interests = human.interests + interests.each do |interest| + assert_equal human.name, interest.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to parent instance" + interest.human.name = "Mungo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to child-owned instance" end end def test_parent_instance_should_be_shared_with_every_child_on_find_for_sti - a = authors(:david) - ps = a.posts - ps.each do |p| - assert_equal a.name, p.author.name, "Name of man should be the same before changes to parent instance" - a.name = "Bongo" - assert_equal a.name, p.author.name, "Name of man should be the same after changes to parent instance" - p.author.name = "Mungo" - assert_equal a.name, p.author.name, "Name of man should be the same after changes to child-owned instance" + author = authors(:david) + posts = author.posts + posts.each do |post| + assert_equal author.name, post.author.name, "Name of human should be the same before changes to parent instance" + author.name = "Bongo" + assert_equal author.name, post.author.name, "Name of human should be the same after changes to parent instance" + post.author.name = "Mungo" + assert_equal author.name, post.author.name, "Name of human should be the same after changes to child-owned instance" end - sps = a.special_posts - sps.each do |sp| - assert_equal a.name, sp.author.name, "Name of man should be the same before changes to parent instance" - a.name = "Bongo" - assert_equal a.name, sp.author.name, "Name of man should be the same after changes to parent instance" - sp.author.name = "Mungo" - assert_equal a.name, sp.author.name, "Name of man should be the same after changes to child-owned instance" + special_posts = author.special_posts + special_posts.each do |post| + assert_equal author.name, post.author.name, "Name of human should be the same before changes to parent instance" + author.name = "Bongo" + assert_equal author.name, post.author.name, "Name of human should be the same after changes to parent instance" + post.author.name = "Mungo" + assert_equal author.name, post.author.name, "Name of human should be the same after changes to child-owned instance" end end def test_parent_instance_should_be_shared_with_eager_loaded_children - m = Man.all.merge!(where: { name: "Gordon" }, includes: :interests).first - is = m.interests - is.each do |i| - assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance" - i.man.name = "Mungo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance" + human = Human.all.merge!(where: { name: "Gordon" }, includes: :interests).first + interests = human.interests + interests.each do |interest| + assert_equal human.name, interest.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to parent instance" + interest.human.name = "Mungo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to child-owned instance" end - m = Man.all.merge!(where: { name: "Gordon" }, includes: :interests, order: "interests.id").first - is = m.interests - is.each do |i| - assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance" - i.man.name = "Mungo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to child-owned instance" + human = Human.all.merge!(where: { name: "Gordon" }, includes: :interests, order: "interests.id").first + interests = human.interests + interests.each do |interest| + assert_equal human.name, interest.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to parent instance" + interest.human.name = "Mungo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to child-owned instance" end end def test_parent_instance_should_be_shared_with_newly_block_style_built_child - m = Man.first - i = m.interests.build { |ii| ii.topic = "Industrial Revolution Re-enactment" } - assert_not_nil i.topic, "Child attributes supplied to build via blocks should be populated" - assert_not_nil i.man - assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance" - i.man.name = "Mungo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to just-built-child-owned instance" + human = Human.first + interest = human.interests.build { |ii| ii.topic = "Industrial Revolution Re-enactment" } + assert_not_nil interest.topic, "Child attributes supplied to build via blocks should be populated" + assert_not_nil interest.human + assert_equal human.name, interest.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to parent instance" + interest.human.name = "Mungo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to just-built-child-owned instance" end def test_parent_instance_should_be_shared_with_newly_created_via_bang_method_child - m = Man.first - i = m.interests.create!(topic: "Industrial Revolution Re-enactment") - assert_not_nil i.man - assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance" - i.man.name = "Mungo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance" + human = Human.first + interest = human.interests.create!(topic: "Industrial Revolution Re-enactment") + assert_not_nil interest.human + assert_equal human.name, interest.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to parent instance" + interest.human.name = "Mungo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to newly-created-child-owned instance" end def test_parent_instance_should_be_shared_with_newly_block_style_created_child - m = Man.first - i = m.interests.create { |ii| ii.topic = "Industrial Revolution Re-enactment" } - assert_not_nil i.topic, "Child attributes supplied to create via blocks should be populated" - assert_not_nil i.man - assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance" - i.man.name = "Mungo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance" + human = Human.first + interest = human.interests.create { |ii| ii.topic = "Industrial Revolution Re-enactment" } + assert_not_nil interest.topic, "Child attributes supplied to create via blocks should be populated" + assert_not_nil interest.human + assert_equal human.name, interest.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to parent instance" + interest.human.name = "Mungo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to newly-created-child-owned instance" end def test_parent_instance_should_be_shared_within_create_block_of_new_child - man = Man.first - interest = man.interests.create do |i| - assert i.man.equal?(man), "Man of child should be the same instance as a parent" + human = Human.first + interest = human.interests.create do |i| + assert i.human.equal?(human), "Human of child should be the same instance as a parent" end - assert interest.man.equal?(man), "Man of the child should still be the same instance as a parent" + assert interest.human.equal?(human), "Human of the child should still be the same instance as a parent" end def test_parent_instance_should_be_shared_within_build_block_of_new_child - man = Man.first - interest = man.interests.build do |i| - assert i.man.equal?(man), "Man of child should be the same instance as a parent" + human = Human.first + interest = human.interests.build do |i| + assert i.human.equal?(human), "Human of child should be the same instance as a parent" end - assert interest.man.equal?(man), "Man of the child should still be the same instance as a parent" + assert interest.human.equal?(human), "Human of the child should still be the same instance as a parent" end def test_parent_instance_should_be_shared_with_poked_in_child - m = men(:gordon) - i = Interest.create(topic: "Industrial Revolution Re-enactment") - m.interests << i - assert_not_nil i.man - assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance" - i.man.name = "Mungo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to newly-created-child-owned instance" + human = humans(:gordon) + interest = Interest.create(topic: "Industrial Revolution Re-enactment") + human.interests << interest + assert_not_nil interest.human + assert_equal human.name, interest.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to parent instance" + interest.human.name = "Mungo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to newly-created-child-owned instance" end def test_parent_instance_should_be_shared_with_replaced_via_accessor_children - m = Man.first - i = Interest.new(topic: "Industrial Revolution Re-enactment") - m.interests = [i] - assert_not_nil i.man - assert_equal m.name, i.man.name, "Name of man should be the same before changes to parent instance" - m.name = "Bongo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to parent instance" - i.man.name = "Mungo" - assert_equal m.name, i.man.name, "Name of man should be the same after changes to replaced-child-owned instance" + human = Human.first + interest = Interest.new(topic: "Industrial Revolution Re-enactment") + human.interests = [interest] + assert_not_nil interest.human + assert_equal human.name, interest.human.name, "Name of human should be the same before changes to parent instance" + human.name = "Bongo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to parent instance" + interest.human.name = "Mungo" + assert_equal human.name, interest.human.name, "Name of human should be the same after changes to replaced-child-owned instance" end def test_parent_instance_should_be_shared_with_first_and_last_child - man = Man.first + human = Human.first - assert man.interests.first.man.equal? man - assert man.interests.last.man.equal? man + assert human.interests.first.human.equal? human + assert human.interests.last.human.equal? human end def test_parent_instance_should_be_shared_with_first_n_and_last_n_children - man = Man.first + human = Human.first - interests = man.interests.first(2) - assert interests[0].man.equal? man - assert interests[1].man.equal? man + interests = human.interests.first(2) + assert interests[0].human.equal? human + assert interests[1].human.equal? human - interests = man.interests.last(2) - assert interests[0].man.equal? man - assert interests[1].man.equal? man + interests = human.interests.last(2) + assert interests[0].human.equal? human + assert interests[1].human.equal? human end def test_parent_instance_should_find_child_instance_using_child_instance_id - man = Man.create! + human = Human.create! interest = Interest.create! - man.interests = [interest] + human.interests = [interest] - assert interest.equal?(man.interests.first), "The inverse association should use the interest already created and held in memory" - assert interest.equal?(man.interests.find(interest.id)), "The inverse association should use the interest already created and held in memory" - assert man.equal?(man.interests.first.man), "Two inversion should lead back to the same object that was originally held" - assert man.equal?(man.interests.find(interest.id).man), "Two inversions should lead back to the same object that was originally held" + assert interest.equal?(human.interests.first), "The inverse association should use the interest already created and held in memory" + assert interest.equal?(human.interests.find(interest.id)), "The inverse association should use the interest already created and held in memory" + assert human.equal?(human.interests.first.human), "Two inversion should lead back to the same object that was originally held" + assert human.equal?(human.interests.find(interest.id).human), "Two inversions should lead back to the same object that was originally held" end def test_parent_instance_should_find_child_instance_using_child_instance_id_when_created - man = Man.create! - interest = Interest.create!(man: man) + human = Human.create! + interest = Interest.create!(human: human) - assert man.equal?(man.interests.first.man), "Two inverses should lead back to the same object that was originally held" - assert man.equal?(man.interests.find(interest.id).man), "Two inversions should lead back to the same object that was originally held" + assert human.equal?(human.interests.first.human), "Two inverses should lead back to the same object that was originally held" + assert human.equal?(human.interests.find(interest.id).human), "Two inversions should lead back to the same object that was originally held" - assert_nil man.interests.find(interest.id).man.name, "The name of the man should match before the name is changed" - man.name = "Ben Bitdiddle" - assert_equal man.name, man.interests.find(interest.id).man.name, "The name of the man should match after the parent name is changed" - man.interests.find(interest.id).man.name = "Alyssa P. Hacker" - assert_equal man.name, man.interests.find(interest.id).man.name, "The name of the man should match after the child name is changed" + assert_nil human.interests.find(interest.id).human.name, "The name of the human should match before the name is changed" + human.name = "Ben Bitdiddle" + assert_equal human.name, human.interests.find(interest.id).human.name, "The name of the human should match after the parent name is changed" + human.interests.find(interest.id).human.name = "Alyssa P. Hacker" + assert_equal human.name, human.interests.find(interest.id).human.name, "The name of the human should match after the child name is changed" end def test_find_on_child_instance_with_id_should_not_load_all_child_records - man = Man.create! - interest = Interest.create!(man: man) + human = Human.create! + interest = Interest.create!(human: human) - man.interests.find(interest.id) - assert_not_predicate man.interests, :loaded? + human.interests.find(interest.id) + assert_not_predicate human.interests, :loaded? end def test_raise_record_not_found_error_when_invalid_ids_are_passed @@ -491,316 +491,316 @@ def test_raise_record_not_found_error_when_invalid_ids_are_passed # are indeed invalid. Interest.delete_all - man = Man.create! + human = Human.create! invalid_id = 245324523 - assert_raise(ActiveRecord::RecordNotFound) { man.interests.find(invalid_id) } + assert_raise(ActiveRecord::RecordNotFound) { human.interests.find(invalid_id) } invalid_ids = [8432342, 2390102913, 2453245234523452] - assert_raise(ActiveRecord::RecordNotFound) { man.interests.find(invalid_ids) } + assert_raise(ActiveRecord::RecordNotFound) { human.interests.find(invalid_ids) } end def test_raise_record_not_found_error_when_no_ids_are_passed - man = Man.create! + human = Human.create! - exception = assert_raise(ActiveRecord::RecordNotFound) { man.interests.load.find() } + exception = assert_raise(ActiveRecord::RecordNotFound) { human.interests.load.find() } assert_equal exception.model, "Interest" assert_equal exception.primary_key, "id" end def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error - assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Man.first.secret_interests } + assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Human.first.secret_interests } end def test_child_instance_should_point_to_parent_without_saving - man = Man.new - i = Interest.create(topic: "Industrial Revolution Re-enactment") + human = Human.new + interest = Interest.create(topic: "Industrial Revolution Re-enactment") - man.interests << i - assert_not_nil i.man + human.interests << interest + assert_not_nil interest.human - i.man.name = "Charles" - assert_equal i.man.name, man.name + interest.human.name = "Charles" + assert_equal interest.human.name, human.name - assert_not_predicate man, :persisted? + assert_not_predicate human, :persisted? end def test_inverse_instance_should_be_set_before_find_callbacks_are_run reset_callbacks(Interest, :find) do - Interest.after_find { raise unless association(:man).loaded? && man.present? } + Interest.after_find { raise unless association(:human).loaded? && human.present? } - assert_predicate Man.first.interests.reload, :any? - assert_predicate Man.includes(:interests).first.interests, :any? - assert_predicate Man.joins(:interests).includes(:interests).first.interests, :any? + assert_predicate Human.first.interests.reload, :any? + assert_predicate Human.includes(:interests).first.interests, :any? + assert_predicate Human.joins(:interests).includes(:interests).first.interests, :any? end end def test_inverse_instance_should_be_set_before_initialize_callbacks_are_run reset_callbacks(Interest, :initialize) do - Interest.after_initialize { raise unless association(:man).loaded? && man.present? } + Interest.after_initialize { raise unless association(:human).loaded? && human.present? } - assert_predicate Man.first.interests.reload, :any? - assert_predicate Man.includes(:interests).first.interests, :any? - assert_predicate Man.joins(:interests).includes(:interests).first.interests, :any? + assert_predicate Human.first.interests.reload, :any? + assert_predicate Human.includes(:interests).first.interests, :any? + assert_predicate Human.joins(:interests).includes(:interests).first.interests, :any? end end end class InverseBelongsToTests < ActiveRecord::TestCase - fixtures :men, :faces, :interests + fixtures :humans, :faces, :interests def test_child_instance_should_be_shared_with_parent_on_find - f = faces(:trusting) - m = f.man - assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance" - f.description = "gormless" - assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance" - m.face.description = "pleasing" - assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance" + face = faces(:trusting) + human = face.human + assert_equal face.description, human.face.description, "Description of face should be the same before changes to child instance" + face.description = "gormless" + assert_equal face.description, human.face.description, "Description of face should be the same after changes to child instance" + human.face.description = "pleasing" + assert_equal face.description, human.face.description, "Description of face should be the same after changes to parent-owned instance" end def test_eager_loaded_child_instance_should_be_shared_with_parent_on_find - f = Face.all.merge!(includes: :man, where: { description: "trusting" }).first - m = f.man - assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance" - f.description = "gormless" - assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance" - m.face.description = "pleasing" - assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance" - - f = Face.all.merge!(includes: :man, order: "men.id", where: { description: "trusting" }).first - m = f.man - assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance" - f.description = "gormless" - assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance" - m.face.description = "pleasing" - assert_equal f.description, m.face.description, "Description of face should be the same after changes to parent-owned instance" + face = Face.all.merge!(includes: :human, where: { description: "trusting" }).first + human = face.human + assert_equal face.description, human.face.description, "Description of face should be the same before changes to child instance" + face.description = "gormless" + assert_equal face.description, human.face.description, "Description of face should be the same after changes to child instance" + human.face.description = "pleasing" + assert_equal face.description, human.face.description, "Description of face should be the same after changes to parent-owned instance" + + face = Face.all.merge!(includes: :human, order: "humans.id", where: { description: "trusting" }).first + human = face.human + assert_equal face.description, human.face.description, "Description of face should be the same before changes to child instance" + face.description = "gormless" + assert_equal face.description, human.face.description, "Description of face should be the same after changes to child instance" + human.face.description = "pleasing" + assert_equal face.description, human.face.description, "Description of face should be the same after changes to parent-owned instance" end def test_child_instance_should_be_shared_with_newly_built_parent - f = faces(:trusting) - m = f.build_man(name: "Charles") - assert_not_nil m.face - assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance" - f.description = "gormless" - assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance" - m.face.description = "pleasing" - assert_equal f.description, m.face.description, "Description of face should be the same after changes to just-built-parent-owned instance" + face = faces(:trusting) + human = face.build_human(name: "Charles") + assert_not_nil human.face + assert_equal face.description, human.face.description, "Description of face should be the same before changes to child instance" + face.description = "gormless" + assert_equal face.description, human.face.description, "Description of face should be the same after changes to child instance" + human.face.description = "pleasing" + assert_equal face.description, human.face.description, "Description of face should be the same after changes to just-built-parent-owned instance" end def test_child_instance_should_be_shared_with_newly_created_parent - f = faces(:trusting) - m = f.create_man(name: "Charles") - assert_not_nil m.face - assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance" - f.description = "gormless" - assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance" - m.face.description = "pleasing" - assert_equal f.description, m.face.description, "Description of face should be the same after changes to newly-created-parent-owned instance" + face = faces(:trusting) + human = face.create_human(name: "Charles") + assert_not_nil human.face + assert_equal face.description, human.face.description, "Description of face should be the same before changes to child instance" + face.description = "gormless" + assert_equal face.description, human.face.description, "Description of face should be the same after changes to child instance" + human.face.description = "pleasing" + assert_equal face.description, human.face.description, "Description of face should be the same after changes to newly-created-parent-owned instance" end def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many - i = interests(:trainspotting) - m = i.man - assert_not_nil m.interests - iz = m.interests.detect { |_iz| _iz.id == i.id } + interest = interests(:trainspotting) + human = interest.human + assert_not_nil human.interests + iz = human.interests.detect { |_iz| _iz.id == interest.id } assert_not_nil iz - assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child" - i.topic = "Eating cheese with a spoon" - assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to child" + assert_equal interest.topic, iz.topic, "Interest topics should be the same before changes to child" + interest.topic = "Eating cheese with a spoon" + assert_not_equal interest.topic, iz.topic, "Interest topics should not be the same after changes to child" iz.topic = "Cow tipping" - assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance" + assert_not_equal interest.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance" end def test_with_has_many_inversing_should_try_to_set_inverse_instances_when_the_inverse_is_a_has_many with_has_many_inversing do - i = interests(:trainspotting) - m = i.man - assert_not_nil m.interests - iz = m.interests.detect { |_iz| _iz.id == i.id } + interest = interests(:trainspotting) + human = interest.human + assert_not_nil human.interests + iz = human.interests.detect { |_iz| _iz.id == interest.id } assert_not_nil iz - assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child" - i.topic = "Eating cheese with a spoon" - assert_equal i.topic, iz.topic, "Interest topics should be the same after changes to child" + assert_equal interest.topic, iz.topic, "Interest topics should be the same before changes to child" + interest.topic = "Eating cheese with a spoon" + assert_equal interest.topic, iz.topic, "Interest topics should be the same after changes to child" iz.topic = "Cow tipping" - assert_equal i.topic, iz.topic, "Interest topics should be the same after changes to parent-owned instance" + assert_equal interest.topic, iz.topic, "Interest topics should be the same after changes to parent-owned instance" end end def test_with_has_many_inversing_does_not_trigger_association_callbacks_on_set_when_the_inverse_is_a_has_many with_has_many_inversing do - man = interests(:trainspotting).man_with_callbacks - assert_not_predicate man, :add_callback_called? + human = interests(:trainspotting).human_with_callbacks + assert_not_predicate human, :add_callback_called? end end def test_child_instance_should_be_shared_with_replaced_via_accessor_parent - f = Face.first - m = Man.new(name: "Charles") - f.man = m - assert_not_nil m.face - assert_equal f.description, m.face.description, "Description of face should be the same before changes to child instance" - f.description = "gormless" - assert_equal f.description, m.face.description, "Description of face should be the same after changes to child instance" - m.face.description = "pleasing" - assert_equal f.description, m.face.description, "Description of face should be the same after changes to replaced-parent-owned instance" + face = Face.first + human = Human.new(name: "Charles") + face.human = human + assert_not_nil human.face + assert_equal face.description, human.face.description, "Description of face should be the same before changes to child instance" + face.description = "gormless" + assert_equal face.description, human.face.description, "Description of face should be the same after changes to child instance" + human.face.description = "pleasing" + assert_equal face.description, human.face.description, "Description of face should be the same after changes to replaced-parent-owned instance" end def test_trying_to_use_inverses_that_dont_exist_should_raise_an_error - assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.horrible_man } + assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.horrible_human } end def test_building_has_many_parent_association_inverses_one_record with_has_many_inversing do interest = Interest.new - interest.build_man - assert_equal 1, interest.man.interests.size + interest.build_human + assert_equal 1, interest.human.interests.size interest.save! - assert_equal 1, interest.man.interests.size + assert_equal 1, interest.human.interests.size end end end class InversePolymorphicBelongsToTests < ActiveRecord::TestCase - fixtures :men, :faces, :interests + fixtures :humans, :faces, :interests def test_child_instance_should_be_shared_with_parent_on_find - f = Face.all.merge!(where: { description: "confused" }).first - m = f.polymorphic_man - assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same before changes to child instance" - f.description = "gormless" - assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to child instance" - m.polymorphic_face.description = "pleasing" - assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance" + face = Face.all.merge!(where: { description: "confused" }).first + human = face.polymorphic_human + assert_equal face.description, human.polymorphic_face.description, "Description of face should be the same before changes to child instance" + face.description = "gormless" + assert_equal face.description, human.polymorphic_face.description, "Description of face should be the same after changes to child instance" + human.polymorphic_face.description = "pleasing" + assert_equal face.description, human.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance" end def test_eager_loaded_child_instance_should_be_shared_with_parent_on_find - f = Face.all.merge!(where: { description: "confused" }, includes: :man).first - m = f.polymorphic_man - assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same before changes to child instance" - f.description = "gormless" - assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to child instance" - m.polymorphic_face.description = "pleasing" - assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance" - - f = Face.all.merge!(where: { description: "confused" }, includes: :man, order: "men.id").first - m = f.polymorphic_man - assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same before changes to child instance" - f.description = "gormless" - assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to child instance" - m.polymorphic_face.description = "pleasing" - assert_equal f.description, m.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance" + face = Face.all.merge!(where: { description: "confused" }, includes: :human).first + human = face.polymorphic_human + assert_equal face.description, human.polymorphic_face.description, "Description of face should be the same before changes to child instance" + face.description = "gormless" + assert_equal face.description, human.polymorphic_face.description, "Description of face should be the same after changes to child instance" + human.polymorphic_face.description = "pleasing" + assert_equal face.description, human.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance" + + face = Face.all.merge!(where: { description: "confused" }, includes: :human, order: "humans.id").first + human = face.polymorphic_human + assert_equal face.description, human.polymorphic_face.description, "Description of face should be the same before changes to child instance" + face.description = "gormless" + assert_equal face.description, human.polymorphic_face.description, "Description of face should be the same after changes to child instance" + human.polymorphic_face.description = "pleasing" + assert_equal face.description, human.polymorphic_face.description, "Description of face should be the same after changes to parent-owned instance" end def test_child_instance_should_be_shared_with_replaced_via_accessor_parent face = faces(:confused) - new_man = Man.new + new_human = Human.new - assert_not_nil face.polymorphic_man - face.polymorphic_man = new_man + assert_not_nil face.polymorphic_human + face.polymorphic_human = new_human - assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same before changes to parent instance" + assert_equal face.description, new_human.polymorphic_face.description, "Description of face should be the same before changes to parent instance" face.description = "Bongo" - assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same after changes to parent instance" - new_man.polymorphic_face.description = "Mungo" - assert_equal face.description, new_man.polymorphic_face.description, "Description of face should be the same after changes to replaced-parent-owned instance" + assert_equal face.description, new_human.polymorphic_face.description, "Description of face should be the same after changes to parent instance" + new_human.polymorphic_face.description = "Mungo" + assert_equal face.description, new_human.polymorphic_face.description, "Description of face should be the same after changes to replaced-parent-owned instance" end def test_inversed_instance_should_not_be_reloaded_after_stale_state_changed - new_man = Man.new + new_human = Human.new face = Face.new - new_man.face = face + new_human.face = face - old_inversed_man = face.man - new_man.save! - new_inversed_man = face.man + old_inversed_human = face.human + new_human.save! + new_inversed_human = face.human - assert_same old_inversed_man, new_inversed_man + assert_same old_inversed_human, new_inversed_human end def test_inversed_instance_should_not_be_reloaded_after_stale_state_changed_with_validation - face = Face.new man: Man.new + face = Face.new human: Human.new - old_inversed_man = face.man + old_inversed_human = face.human face.save! - new_inversed_man = face.man + new_inversed_human = face.human - assert_same old_inversed_man, new_inversed_man + assert_same old_inversed_human, new_inversed_human end def test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many - i = interests(:llama_wrangling) - m = i.polymorphic_man - assert_not_nil m.polymorphic_interests - iz = m.polymorphic_interests.detect { |_iz| _iz.id == i.id } + interest = interests(:llama_wrangling) + human = interest.polymorphic_human + assert_not_nil human.polymorphic_interests + iz = human.polymorphic_interests.detect { |_iz| _iz.id == interest.id } assert_not_nil iz - assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child" - i.topic = "Eating cheese with a spoon" - assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to child" + assert_equal interest.topic, iz.topic, "Interest topics should be the same before changes to child" + interest.topic = "Eating cheese with a spoon" + assert_not_equal interest.topic, iz.topic, "Interest topics should not be the same after changes to child" iz.topic = "Cow tipping" - assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance" + assert_not_equal interest.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance" end def test_with_has_many_inversing_should_try_to_set_inverse_instances_when_the_inverse_is_a_has_many with_has_many_inversing do - i = interests(:llama_wrangling) - m = i.polymorphic_man - assert_not_nil m.polymorphic_interests - iz = m.polymorphic_interests.detect { |_iz| _iz.id == i.id } + interest = interests(:llama_wrangling) + human = interest.polymorphic_human + assert_not_nil human.polymorphic_interests + iz = human.polymorphic_interests.detect { |_iz| _iz.id == interest.id } assert_not_nil iz - assert_equal i.topic, iz.topic, "Interest topics should be the same before changes to child" - i.topic = "Eating cheese with a spoon" - assert_equal i.topic, iz.topic, "Interest topics should be the same after changes to child" + assert_equal interest.topic, iz.topic, "Interest topics should be the same before changes to child" + interest.topic = "Eating cheese with a spoon" + assert_equal interest.topic, iz.topic, "Interest topics should be the same after changes to child" iz.topic = "Cow tipping" - assert_equal i.topic, iz.topic, "Interest topics should be the same after changes to parent-owned instance" + assert_equal interest.topic, iz.topic, "Interest topics should be the same after changes to parent-owned instance" end end def test_with_has_many_inversing_does_not_trigger_association_callbacks_on_set_when_the_inverse_is_a_has_many with_has_many_inversing do - man = interests(:llama_wrangling).polymorphic_man_with_callbacks - assert_not_predicate man, :add_callback_called? + human = interests(:llama_wrangling).polymorphic_human_with_callbacks + assert_not_predicate human, :add_callback_called? end end def test_trying_to_access_inverses_that_dont_exist_shouldnt_raise_an_error # Ideally this would, if only for symmetry's sake with other association types - assert_nothing_raised { Face.first.horrible_polymorphic_man } + assert_nothing_raised { Face.first.horrible_polymorphic_human } end def test_trying_to_set_polymorphic_inverses_that_dont_exist_at_all_should_raise_an_error - # fails because no class has the correct inverse_of for horrible_polymorphic_man - assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.horrible_polymorphic_man = Man.first } + # fails because no class has the correct inverse_of for horrible_polymorphic_human + assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.horrible_polymorphic_human = Human.first } end def test_trying_to_set_polymorphic_inverses_that_dont_exist_on_the_instance_being_set_should_raise_an_error - # passes because Man does have the correct inverse_of - assert_nothing_raised { Face.first.polymorphic_man = Man.first } + # passes because Human does have the correct inverse_of + assert_nothing_raised { Face.first.polymorphic_human = Human.first } # fails because Interest does have the correct inverse_of - assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.polymorphic_man = Interest.first } + assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.polymorphic_human = Interest.first } end end # NOTE - these tests might not be meaningful, ripped as they were from the parental_control plugin # which would guess the inverse rather than look for an explicit configuration option. class InverseMultipleHasManyInversesForSameModel < ActiveRecord::TestCase - fixtures :men, :interests, :zines + fixtures :humans, :interests, :zines def test_that_we_can_load_associations_that_have_the_same_reciprocal_name_from_different_models assert_nothing_raised do - i = Interest.first - i.zine - i.man + interest = Interest.first + interest.zine + interest.human end end def test_that_we_can_create_associations_that_have_the_same_reciprocal_name_from_different_models assert_nothing_raised do - i = Interest.first - i.build_zine(title: "Get Some in Winter! 2008") - i.build_man(name: "Gordon") - i.save! + interest = Interest.first + interest.build_zine(title: "Get Some in Winter! 2008") + interest.build_human(name: "Gordon") + interest.save! end end end diff --git a/activerecord/test/cases/associations_test.rb b/activerecord/test/cases/associations_test.rb index 70191b3c43ed742805cabcd61d5ada437f584bfc..482a66bfa2aafa9c562a76e0044f2859c6d7ed68 100644 --- a/activerecord/test/cases/associations_test.rb +++ b/activerecord/test/cases/associations_test.rb @@ -19,7 +19,7 @@ require "models/liquid" require "models/molecule" require "models/electron" -require "models/man" +require "models/human" require "models/interest" require "models/pirate" require "models/parrot" @@ -240,13 +240,13 @@ def test_scoped_allows_conditions end test "inverses get set of subsets of the association" do - man = Man.create - man.interests.create + human = Human.create + human.interests.create - man = Man.find(man.id) + human = Human.find(human.id) assert_queries(1) do - assert_equal man, man.interests.where("1=1").first.man + assert_equal human, human.interests.where("1=1").first.human end end diff --git a/activerecord/test/cases/nested_attributes_test.rb b/activerecord/test/cases/nested_attributes_test.rb index 7c16c4d20ee2a1536717656a1c97a81a95fcf9e5..b0e30d6423ab7c73623ed02abc8a250b7090e000 100644 --- a/activerecord/test/cases/nested_attributes_test.rb +++ b/activerecord/test/cases/nested_attributes_test.rb @@ -7,7 +7,7 @@ require "models/bird" require "models/parrot" require "models/treasure" -require "models/man" +require "models/human" require "models/interest" require "models/owner" require "models/pet" @@ -140,19 +140,19 @@ def test_do_not_allow_assigning_foreign_key_when_reusing_existing_new_record end def test_reject_if_with_a_proc_which_returns_true_always_for_has_many - Man.accepts_nested_attributes_for :interests, reject_if: proc { |attributes| true } - man = Man.create(name: "John") - interest = man.interests.create(topic: "photography") - man.update(interests_attributes: { topic: "gardening", id: interest.id }) + Human.accepts_nested_attributes_for :interests, reject_if: proc { |attributes| true } + human = Human.create(name: "John") + interest = human.interests.create(topic: "photography") + human.update(interests_attributes: { topic: "gardening", id: interest.id }) assert_equal "photography", interest.reload.topic end def test_destroy_works_independent_of_reject_if - Man.accepts_nested_attributes_for :interests, reject_if: proc { |attributes| true }, allow_destroy: true - man = Man.create(name: "Jon") - interest = man.interests.create(topic: "the ladies") - man.update(interests_attributes: { _destroy: "1", id: interest.id }) - assert_empty man.reload.interests + Human.accepts_nested_attributes_for :interests, reject_if: proc { |attributes| true }, allow_destroy: true + human = Human.create(name: "Jon") + interest = human.interests.create(topic: "the ladies") + human.update(interests_attributes: { _destroy: "1", id: interest.id }) + assert_empty human.reload.interests end def test_reject_if_is_not_short_circuited_if_allow_destroy_is_false @@ -169,10 +169,10 @@ def test_reject_if_is_not_short_circuited_if_allow_destroy_is_false end def test_has_many_association_updating_a_single_record - Man.accepts_nested_attributes_for(:interests) - man = Man.create(name: "John") - interest = man.interests.create(topic: "photography") - man.update(interests_attributes: { topic: "gardening", id: interest.id }) + Human.accepts_nested_attributes_for(:interests) + human = Human.create(name: "John") + interest = human.interests.create(topic: "photography") + human.update(interests_attributes: { topic: "gardening", id: interest.id }) assert_equal "gardening", interest.reload.topic end @@ -186,12 +186,12 @@ def test_reject_if_with_blank_nested_attributes_id end def test_first_and_array_index_zero_methods_return_the_same_value_when_nested_attributes_are_set_to_update_existing_record - Man.accepts_nested_attributes_for(:interests) - man = Man.create(name: "John") - interest = man.interests.create topic: "gardening" - man = Man.find man.id - man.interests_attributes = [{ id: interest.id, topic: "gardening" }] - assert_equal man.interests.first.topic, man.interests[0].topic + Human.accepts_nested_attributes_for(:interests) + human = Human.create(name: "John") + interest = human.interests.create topic: "gardening" + human = Human.find human.id + human.interests_attributes = [{ id: interest.id, topic: "gardening" }] + assert_equal human.interests.first.topic, human.interests[0].topic end def test_allows_class_to_override_setter_and_call_super @@ -219,10 +219,10 @@ def test_accepts_nested_attributes_for_can_be_overridden_in_subclasses end def test_should_not_create_duplicates_with_create_with - Man.accepts_nested_attributes_for(:interests) + Human.accepts_nested_attributes_for(:interests) assert_difference("Interest.count", 1) do - Man.create_with( + Human.create_with( interests_attributes: [{ topic: "Pirate king" }] ).find_or_create_by!( name: "Monkey D. Luffy" @@ -817,17 +817,17 @@ def test_should_automatically_enable_autosave_on_the_association end def test_validate_presence_of_parent_works_with_inverse_of - Man.accepts_nested_attributes_for(:interests) - assert_equal :man, Man.reflect_on_association(:interests).options[:inverse_of] - assert_equal :interests, Interest.reflect_on_association(:man).options[:inverse_of] + Human.accepts_nested_attributes_for(:interests) + assert_equal :human, Human.reflect_on_association(:interests).options[:inverse_of] + assert_equal :interests, Interest.reflect_on_association(:human).options[:inverse_of] repair_validations(Interest) do - Interest.validates_presence_of(:man) - assert_difference "Man.count" do + Interest.validates_presence_of(:human) + assert_difference "Human.count" do assert_difference "Interest.count", 2 do - man = Man.create!(name: "John", + human = Human.create!(name: "John", interests_attributes: [{ topic: "Cars" }, { topic: "Sports" }]) - assert_equal 2, man.interests.count + assert_equal 2, human.interests.count end end end @@ -839,14 +839,14 @@ def test_can_use_symbols_as_object_identifier end def test_numeric_column_changes_from_zero_to_no_empty_string - Man.accepts_nested_attributes_for(:interests) + Human.accepts_nested_attributes_for(:interests) repair_validations(Interest) do Interest.validates_numericality_of(:zine_id) - man = Man.create(name: "John") - interest = man.interests.create(topic: "bar", zine_id: 0) + human = Human.create(name: "John") + interest = human.interests.create(topic: "bar", zine_id: 0) assert interest.save - assert_not man.update(interests_attributes: { id: interest.id, zine_id: "foo" }) + assert_not human.update(interests_attributes: { id: interest.id, zine_id: "foo" }) end end diff --git a/activerecord/test/cases/relation/where_chain_test.rb b/activerecord/test/cases/relation/where_chain_test.rb index 2528a89d4f5d871b96856fa273a1947a13e28ab4..94bc06362d265acfd0598837bc2d956104447ac7 100644 --- a/activerecord/test/cases/relation/where_chain_test.rb +++ b/activerecord/test/cases/relation/where_chain_test.rb @@ -3,7 +3,7 @@ require "cases/helper" require "models/post" require "models/author" -require "models/man" +require "models/human" require "models/essay" require "models/comment" require "models/categorization" @@ -11,7 +11,7 @@ module ActiveRecord class WhereChainTest < ActiveRecord::TestCase - fixtures :posts, :comments, :authors, :men, :essays + fixtures :posts, :comments, :authors, :humans, :essays def test_missing_with_association assert posts(:authorless).author.blank? @@ -105,8 +105,8 @@ def test_rewhere_with_nested_condition end def test_rewhere_with_polymorphic_association - relation = Essay.where(writer: authors(:david)).rewhere(writer: men(:steve)) - expected = Essay.where(writer: men(:steve)) + relation = Essay.where(writer: authors(:david)).rewhere(writer: humans(:steve)) + expected = Essay.where(writer: humans(:steve)) assert_equal expected.to_a, relation.to_a end diff --git a/activerecord/test/cases/validations/absence_validation_test.rb b/activerecord/test/cases/validations/absence_validation_test.rb index 1982734f02d90de499f2801a9656dbd2c8555196..a4eb42c4d3d508739d423a69fc875871a4c2e36f 100644 --- a/activerecord/test/cases/validations/absence_validation_test.rb +++ b/activerecord/test/cases/validations/absence_validation_test.rb @@ -3,12 +3,12 @@ require "cases/helper" require "models/face" require "models/interest" -require "models/man" +require "models/human" require "models/topic" class AbsenceValidationTest < ActiveRecord::TestCase def test_non_association - boy_klass = Class.new(Man) do + boy_klass = Class.new(Human) do def self.name; "Boy" end validates_absence_of :name end @@ -18,7 +18,7 @@ def self.name; "Boy" end end def test_has_one_marked_for_destruction - boy_klass = Class.new(Man) do + boy_klass = Class.new(Human) do def self.name; "Boy" end validates_absence_of :face end @@ -32,7 +32,7 @@ def self.name; "Boy" end end def test_has_many_marked_for_destruction - boy_klass = Class.new(Man) do + boy_klass = Class.new(Human) do def self.name; "Boy" end validates_absence_of :interests end @@ -48,7 +48,7 @@ def self.name; "Boy" end end def test_does_not_call_to_a_on_associations - boy_klass = Class.new(Man) do + boy_klass = Class.new(Human) do def self.name; "Boy" end validates_absence_of :face end diff --git a/activerecord/test/cases/validations/association_validation_test.rb b/activerecord/test/cases/validations/association_validation_test.rb index ce6d42b34b1ed1ba0f17f0fc544e827488419d81..5245ca744a0935dd394d5ba23485d40084a56440 100644 --- a/activerecord/test/cases/validations/association_validation_test.rb +++ b/activerecord/test/cases/validations/association_validation_test.rb @@ -3,7 +3,7 @@ require "cases/helper" require "models/topic" require "models/reply" -require "models/man" +require "models/human" require "models/interest" class AssociationValidationTest < ActiveRecord::TestCase @@ -80,20 +80,20 @@ def test_validates_associated_missing def test_validates_presence_of_belongs_to_association__parent_is_new_record repair_validations(Interest) do - # Note that Interest and Man have the :inverse_of option set - Interest.validates_presence_of(:man) - man = Man.new(name: "John") - interest = man.interests.build(topic: "Airplanes") - assert interest.valid?, "Expected interest to be valid, but was not. Interest should have a man object associated" + # Note that Interest and Human have the :inverse_of option set + Interest.validates_presence_of(:human) + human = Human.new(name: "John") + interest = human.interests.build(topic: "Airplanes") + assert interest.valid?, "Expected interest to be valid, but was not. Interest should have a human object associated" end end def test_validates_presence_of_belongs_to_association__existing_parent repair_validations(Interest) do - Interest.validates_presence_of(:man) - man = Man.create!(name: "John") - interest = man.interests.build(topic: "Airplanes") - assert interest.valid?, "Expected interest to be valid, but was not. Interest should have a man object associated" + Interest.validates_presence_of(:human) + human = Human.create!(name: "John") + interest = human.interests.build(topic: "Airplanes") + assert interest.valid?, "Expected interest to be valid, but was not. Interest should have a human object associated" end end end diff --git a/activerecord/test/cases/validations/presence_validation_test.rb b/activerecord/test/cases/validations/presence_validation_test.rb index 4b9cbe9098a6088e62f8f4d16993d6cc81a137f5..4230832f9858684c1f8c00287c00ec06f9334f87 100644 --- a/activerecord/test/cases/validations/presence_validation_test.rb +++ b/activerecord/test/cases/validations/presence_validation_test.rb @@ -1,14 +1,14 @@ # frozen_string_literal: true require "cases/helper" -require "models/man" +require "models/human" require "models/face" require "models/interest" require "models/speedometer" require "models/dashboard" class PresenceValidationTest < ActiveRecord::TestCase - class Boy < Man; end + class Boy < Human; end repair_validations(Boy) diff --git a/activerecord/test/fixtures/essays.yml b/activerecord/test/fixtures/essays.yml index 9fd07aded373a12112d76d770bb3bf550d70f4f8..be96dcacb3e5d0a8eac852c271b241ca454f2570 100644 --- a/activerecord/test/fixtures/essays.yml +++ b/activerecord/test/fixtures/essays.yml @@ -12,5 +12,5 @@ mary_stay_home: steve_connecting_the_dots: name: Connecting The Dots - writer_type: Man + writer_type: Human writer_id: Steve diff --git a/activerecord/test/fixtures/faces.yml b/activerecord/test/fixtures/faces.yml index c8e4a34484e82cf4fbf8975f91ea6a594b1b4eaf..b034f59c6a962892f310b762fc354085a9dc1237 100644 --- a/activerecord/test/fixtures/faces.yml +++ b/activerecord/test/fixtures/faces.yml @@ -1,11 +1,11 @@ trusting: description: trusting - man: gordon + human: gordon weather_beaten: description: weather beaten - man: steve + human: steve confused: description: confused - polymorphic_man: gordon (Man) + polymorphic_human: gordon (Human) diff --git a/activerecord/test/fixtures/men.yml b/activerecord/test/fixtures/humans.yml similarity index 100% rename from activerecord/test/fixtures/men.yml rename to activerecord/test/fixtures/humans.yml diff --git a/activerecord/test/fixtures/interests.yml b/activerecord/test/fixtures/interests.yml index 9200a19d5a3d76ec51756c6ec94bdd80868374ec..335e855dbc9c7b52c3c8ccc51e72c93a3b67084a 100644 --- a/activerecord/test/fixtures/interests.yml +++ b/activerecord/test/fixtures/interests.yml @@ -1,33 +1,33 @@ trainspotting: topic: Trainspotting zine: staying_in - man: gordon + human: gordon birdwatching: topic: Birdwatching zine: staying_in - man: gordon + human: gordon stamp_collecting: topic: Stamp Collecting zine: staying_in - man: gordon + human: gordon hunting: topic: Hunting zine: going_out - man: steve + human: steve woodsmanship: topic: Woodsmanship zine: going_out - man: steve + human: steve survival: topic: Survival zine: going_out - man: steve + human: steve llama_wrangling: topic: Llama Wrangling - polymorphic_man: gordon (Man) + polymorphic_human: gordon (Human) diff --git a/activerecord/test/models/face.rb b/activerecord/test/models/face.rb index 45ccc442bada76d304ed550caa5a64a6ae1471f4..178834155ffc7f03fd405089244fb11a50d3ab29 100644 --- a/activerecord/test/models/face.rb +++ b/activerecord/test/models/face.rb @@ -1,16 +1,16 @@ # frozen_string_literal: true class Face < ActiveRecord::Base - belongs_to :man, inverse_of: :face - belongs_to :human, polymorphic: true - belongs_to :polymorphic_man, polymorphic: true, inverse_of: :polymorphic_face + belongs_to :human, inverse_of: :face + belongs_to :super_human, polymorphic: true + belongs_to :polymorphic_human, polymorphic: true, inverse_of: :polymorphic_face # Oracle identifier length is limited to 30 bytes or less, `polymorphic` renamed `poly` - belongs_to :poly_man_without_inverse, polymorphic: true + belongs_to :poly_human_without_inverse, polymorphic: true # These are "broken" inverse_of associations for the purposes of testing - belongs_to :horrible_man, class_name: "Man", inverse_of: :horrible_face - belongs_to :horrible_polymorphic_man, polymorphic: true, inverse_of: :horrible_polymorphic_face + belongs_to :horrible_human, class_name: "Human", inverse_of: :horrible_face + belongs_to :horrible_polymorphic_human, polymorphic: true, inverse_of: :horrible_polymorphic_face validate do - man + human end end diff --git a/activerecord/test/models/man.rb b/activerecord/test/models/human.rb similarity index 61% rename from activerecord/test/models/man.rb rename to activerecord/test/models/human.rb index 118a4f27cdbefc61918fd69da5532e0a1e0bcc63..d19b08e25a65cc3b93a281d60eb23d100f06a821 100644 --- a/activerecord/test/models/man.rb +++ b/activerecord/test/models/human.rb @@ -1,28 +1,30 @@ # frozen_string_literal: true -class Man < ActiveRecord::Base - has_one :face, inverse_of: :man - has_one :polymorphic_face, class_name: "Face", as: :polymorphic_man, inverse_of: :polymorphic_man - has_one :polymorphic_face_without_inverse, class_name: "Face", as: :poly_man_without_inverse - has_many :interests, inverse_of: :man +class Human < ActiveRecord::Base + self.table_name = "humans" + + has_one :face, inverse_of: :human + has_one :polymorphic_face, class_name: "Face", as: :polymorphic_human, inverse_of: :polymorphic_human + has_one :polymorphic_face_without_inverse, class_name: "Face", as: :poly_human_without_inverse + has_many :interests, inverse_of: :human has_many :interests_with_callbacks, class_name: "Interest", before_add: :add_called, after_add: :add_called, - inverse_of: :man_with_callbacks + inverse_of: :human_with_callbacks has_many :polymorphic_interests, class_name: "Interest", - as: :polymorphic_man, - inverse_of: :polymorphic_man + as: :polymorphic_human, + inverse_of: :polymorphic_human has_many :polymorphic_interests_with_callbacks, class_name: "Interest", - as: :polymorphic_man, + as: :polymorphic_human, before_add: :add_called, after_add: :add_called, - inverse_of: :polymorphic_man + inverse_of: :polymorphic_human # These are "broken" inverse_of associations for the purposes of testing - has_one :dirty_face, class_name: "Face", inverse_of: :dirty_man - has_many :secret_interests, class_name: "Interest", inverse_of: :secret_man + has_one :dirty_face, class_name: "Face", inverse_of: :dirty_human + has_many :secret_interests, class_name: "Interest", inverse_of: :secret_human has_one :mixed_case_monkey attribute :add_callback_called, :boolean, default: false @@ -32,5 +34,5 @@ def add_called(_interest) end end -class Human < Man +class SuperHuman < Human end diff --git a/activerecord/test/models/interest.rb b/activerecord/test/models/interest.rb index d2da6a46efec97169b3fd855a7debc384c3ebfbe..0c0cef190f256a19d42587d14afb2aad3fb8c487 100644 --- a/activerecord/test/models/interest.rb +++ b/activerecord/test/models/interest.rb @@ -1,15 +1,15 @@ # frozen_string_literal: true class Interest < ActiveRecord::Base - belongs_to :man, inverse_of: :interests - belongs_to :man_with_callbacks, - class_name: "Man", - foreign_key: :man_id, + belongs_to :human, inverse_of: :interests + belongs_to :human_with_callbacks, + class_name: "Human", + foreign_key: :human_id, inverse_of: :interests_with_callbacks - belongs_to :polymorphic_man, polymorphic: true, inverse_of: :polymorphic_interests - belongs_to :polymorphic_man_with_callbacks, - foreign_key: :polymorphic_man_id, - foreign_type: :polymorphic_man_type, + belongs_to :polymorphic_human, polymorphic: true, inverse_of: :polymorphic_interests + belongs_to :polymorphic_human_with_callbacks, + foreign_key: :polymorphic_human_id, + foreign_type: :polymorphic_human_type, polymorphic: true, inverse_of: :polymorphic_interests_with_callbacks belongs_to :zine, inverse_of: :interests diff --git a/activerecord/test/models/mixed_case_monkey.rb b/activerecord/test/models/mixed_case_monkey.rb index 8e92f68817ac193acf5ec3639c7384bf63cffeca..f4985ebadb861aa23527617b3150211ef77312b9 100644 --- a/activerecord/test/models/mixed_case_monkey.rb +++ b/activerecord/test/models/mixed_case_monkey.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true class MixedCaseMonkey < ActiveRecord::Base - belongs_to :man + belongs_to :human end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 7bb316fdb42bcea1395f231cc70b5fd2719a920c..a0b4442465ad957e5187b67216ef04f3bf7354c2 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -1009,27 +1009,27 @@ create_table(t, force: true) { } end - create_table :men, force: true do |t| + create_table :humans, force: true do |t| t.string :name end create_table :faces, force: true do |t| t.string :description - t.integer :man_id - t.integer :polymorphic_man_id - t.string :polymorphic_man_type - t.integer :poly_man_without_inverse_id - t.string :poly_man_without_inverse_type - t.integer :horrible_polymorphic_man_id - t.string :horrible_polymorphic_man_type - t.references :human, polymorphic: true, index: false + t.integer :human_id + t.integer :polymorphic_human_id + t.string :polymorphic_human_type + t.integer :poly_human_without_inverse_id + t.string :poly_human_without_inverse_type + t.integer :horrible_polymorphic_human_id + t.string :horrible_polymorphic_human_type + t.references :super_human, polymorphic: true, index: false end create_table :interests, force: true do |t| t.string :topic - t.integer :man_id - t.integer :polymorphic_man_id - t.string :polymorphic_man_type + t.integer :human_id + t.integer :polymorphic_human_id + t.string :polymorphic_human_type t.integer :zine_id end