class InversePolymorphicBelongsToTests

Public Instance Methods

test_child_instance_should_be_shared_with_parent_on_find() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 621
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"
end
test_child_instance_should_be_shared_with_replaced_via_accessor_parent() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 649
def test_child_instance_should_be_shared_with_replaced_via_accessor_parent
  face = faces(:confused)
  new_man = Man.new

  assert_not_nil face.polymorphic_man
  face.polymorphic_man = new_man

  assert_equal face.description, new_man.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"
end
test_eager_loaded_child_instance_should_be_shared_with_parent_on_find() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 631
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"
end
test_inversed_instance_should_not_be_reloaded_after_stale_state_changed() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 663
def test_inversed_instance_should_not_be_reloaded_after_stale_state_changed
  new_man = Man.new
  face = Face.new
  new_man.face = face

  old_inversed_man = face.man
  new_man.save!
  new_inversed_man = face.man

  assert_equal old_inversed_man.object_id, new_inversed_man.object_id
end
test_should_not_try_to_set_inverse_instances_when_the_inverse_is_a_has_many() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 675
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 }
  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"
  iz.topic = "Cow tipping"
  assert_not_equal i.topic, iz.topic, "Interest topics should not be the same after changes to parent-owned instance"
end
test_trying_to_access_inverses_that_dont_exist_shouldnt_raise_an_error() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 688
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 }
end
test_trying_to_set_polymorphic_inverses_that_dont_exist_at_all_should_raise_an_error() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 693
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 }
end
test_trying_to_set_polymorphic_inverses_that_dont_exist_on_the_instance_being_set_should_raise_an_error() click to toggle source
# File activerecord/test/cases/associations/inverse_associations_test.rb, line 698
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 }
  # fails because Interest does have the correct inverse_of
  assert_raise(ActiveRecord::InverseOfAssociationNotFoundError) { Face.first.polymorphic_man = Interest.first }
end