class InheritanceComputeTypeTest

Public Instance Methods

test_inheritance_new_with_subclass_as_default() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 491
def test_inheritance_new_with_subclass_as_default
  original_type = Company.columns_hash["type"].default
  ActiveRecord::Base.connection.change_column_default :companies, :type, "Firm"
  Company.reset_column_information

  firm = Company.new # without arguments
  assert_equal "Firm", firm.type
  assert_instance_of Firm, firm

  firm = Company.new(firm_name: "Shri Hans Plastic") # with arguments
  assert_equal "Firm", firm.type
  assert_instance_of Firm, firm

  client = Client.new
  assert_equal "Client", client.type
  assert_instance_of Client, client

  firm = Company.new(type: "Client") # overwrite the default type
  assert_equal "Client", firm.type
  assert_instance_of Client, firm
ensure
  ActiveRecord::Base.connection.change_column_default :companies, :type, original_type
  Company.reset_column_information
end
test_instantiation_doesnt_try_to_require_corresponding_file() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 462
def test_instantiation_doesnt_try_to_require_corresponding_file
  without_store_full_sti_class do
    foo = Firm.first.clone
    foo.type = "FirmOnTheFly"
    foo.save!

    # Should fail without FirmOnTheFly in the type condition.
    assert_raise(ActiveRecord::RecordNotFound) { Firm.find(foo.id) }

    # Nest FirmOnTheFly in the test case where Dependencies won't see it.
    self.class.const_set :FirmOnTheFly, Class.new(Firm)
    assert_raise(ActiveRecord::SubclassNotFound) { Firm.find(foo.id) }

    # Nest FirmOnTheFly in Firm where Dependencies will see it.
    # This is analogous to nesting models in a migration.
    Firm.const_set :FirmOnTheFly, Class.new(Firm)

    # And instantiate will find the existing constant rather than trying
    # to require firm_on_the_fly.
    assert_nothing_raised { assert_kind_of Firm::FirmOnTheFly, Firm.find(foo.id) }
  end
end
test_sti_type_from_attributes_disabled_in_non_sti_class() click to toggle source
# File activerecord/test/cases/inheritance_test.rb, line 485
def test_sti_type_from_attributes_disabled_in_non_sti_class
  phone = Shop::Product::Type.new(name: "Phone")
  product = Shop::Product.new(type: phone)
  assert product.save
end