2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/author"
|
|
|
|
require "models/company"
|
2017-05-31 03:11:43 -04:00
|
|
|
require "models/membership"
|
2016-08-06 12:26:20 -04:00
|
|
|
require "models/person"
|
|
|
|
require "models/post"
|
|
|
|
require "models/project"
|
|
|
|
require "models/subscriber"
|
|
|
|
require "models/vegetables"
|
|
|
|
require "models/shop"
|
|
|
|
require "models/sponsor"
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2015-05-07 09:04:49 -04:00
|
|
|
module InheritanceTestHelper
|
|
|
|
def with_store_full_sti_class(&block)
|
|
|
|
assign_store_full_sti_class true, &block
|
|
|
|
end
|
|
|
|
|
|
|
|
def without_store_full_sti_class(&block)
|
|
|
|
assign_store_full_sti_class false, &block
|
|
|
|
end
|
|
|
|
|
|
|
|
def assign_store_full_sti_class(flag)
|
|
|
|
old_store_full_sti_class = ActiveRecord::Base.store_full_sti_class
|
|
|
|
ActiveRecord::Base.store_full_sti_class = flag
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = old_store_full_sti_class
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class InheritanceTest < ActiveRecord::TestCase
|
2015-05-07 09:04:49 -04:00
|
|
|
include InheritanceTestHelper
|
2017-05-31 03:11:43 -04:00
|
|
|
fixtures :companies, :projects, :subscribers, :accounts, :vegetables, :memberships
|
2008-05-31 20:13:11 -04:00
|
|
|
|
|
|
|
def test_class_with_store_full_sti_class_returns_full_name
|
2015-05-07 09:04:49 -04:00
|
|
|
with_store_full_sti_class do
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "Namespaced::Company", Namespaced::Company.sti_name
|
2015-05-07 09:04:49 -04:00
|
|
|
end
|
2008-05-31 20:13:11 -04:00
|
|
|
end
|
|
|
|
|
2010-09-30 19:02:49 -04:00
|
|
|
def test_class_with_blank_sti_name
|
2012-04-26 13:32:55 -04:00
|
|
|
company = Company.first
|
2010-11-23 14:57:33 -05:00
|
|
|
company = company.dup
|
2021-02-24 22:43:59 -05:00
|
|
|
company.update!(type: " ")
|
|
|
|
company = Company.find(company.id)
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal " ", company.type
|
2010-09-30 19:02:49 -04:00
|
|
|
end
|
|
|
|
|
2008-05-31 20:13:11 -04:00
|
|
|
def test_class_without_store_full_sti_class_returns_demodulized_name
|
2015-05-07 09:04:49 -04:00
|
|
|
without_store_full_sti_class do
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "Company", Namespaced::Company.sti_name
|
2015-05-07 09:04:49 -04:00
|
|
|
end
|
2008-05-31 20:13:11 -04:00
|
|
|
end
|
|
|
|
|
2015-10-30 19:30:18 -04:00
|
|
|
def test_compute_type_success
|
2016-12-31 13:42:53 -05:00
|
|
|
assert_equal Author, Company.send(:compute_type, "Author")
|
2015-10-30 19:30:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_compute_type_nonexistent_constant
|
|
|
|
e = assert_raises NameError do
|
2016-12-31 13:42:53 -05:00
|
|
|
Company.send :compute_type, "NonexistentModel"
|
2015-10-30 19:30:18 -04:00
|
|
|
end
|
2016-12-31 13:42:53 -05:00
|
|
|
assert_equal "uninitialized constant Company::NonexistentModel", e.message
|
|
|
|
assert_equal "Company::NonexistentModel", e.name
|
2015-10-30 19:30:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_compute_type_no_method_error
|
2016-08-16 03:30:11 -04:00
|
|
|
ActiveSupport::Dependencies.stub(:safe_constantize, proc { raise NoMethodError }) do
|
2015-10-30 19:30:18 -04:00
|
|
|
assert_raises NoMethodError do
|
2016-12-31 13:42:53 -05:00
|
|
|
Company.send :compute_type, "InvalidModel"
|
2015-10-30 19:30:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_compute_type_on_undefined_method
|
|
|
|
error = nil
|
|
|
|
begin
|
|
|
|
Class.new(Author) do
|
|
|
|
alias_method :foo, :bar
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
error = e
|
|
|
|
end
|
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
ActiveSupport::Dependencies.stub(:safe_constantize, proc { raise e }) do
|
2015-10-30 19:30:18 -04:00
|
|
|
exception = assert_raises NameError do
|
2016-12-31 13:42:53 -05:00
|
|
|
Company.send :compute_type, "InvalidModel"
|
2015-10-30 19:30:18 -04:00
|
|
|
end
|
|
|
|
assert_equal error.message, exception.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_compute_type_argument_error
|
2016-08-16 03:30:11 -04:00
|
|
|
ActiveSupport::Dependencies.stub(:safe_constantize, proc { raise ArgumentError }) do
|
2015-10-30 19:30:18 -04:00
|
|
|
assert_raises ArgumentError do
|
2016-12-31 13:42:53 -05:00
|
|
|
Company.send :compute_type, "InvalidModel"
|
2015-10-30 19:30:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-11 11:35:09 -04:00
|
|
|
def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled
|
2015-05-07 09:04:49 -04:00
|
|
|
without_store_full_sti_class do
|
|
|
|
item = Namespaced::Company.new
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "Company", item[:type]
|
2015-05-07 09:04:49 -04:00
|
|
|
end
|
2008-04-11 11:35:09 -04:00
|
|
|
end
|
2009-04-29 18:39:53 -04:00
|
|
|
|
2008-04-11 11:35:09 -04:00
|
|
|
def test_should_store_full_class_name_with_store_full_sti_class_option_enabled
|
2015-05-07 09:04:49 -04:00
|
|
|
with_store_full_sti_class do
|
|
|
|
item = Namespaced::Company.new
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "Namespaced::Company", item[:type]
|
2015-05-07 09:04:49 -04:00
|
|
|
end
|
2008-04-11 11:35:09 -04:00
|
|
|
end
|
2009-04-29 18:39:53 -04:00
|
|
|
|
2008-04-11 11:35:09 -04:00
|
|
|
def test_different_namespace_subclass_should_load_correctly_with_store_full_sti_class_option
|
2015-05-07 09:04:49 -04:00
|
|
|
with_store_full_sti_class do
|
|
|
|
item = Namespaced::Company.create name: "Wolverine 2"
|
|
|
|
assert_not_nil Company.find(item.id)
|
|
|
|
assert_not_nil Namespaced::Company.find(item.id)
|
|
|
|
end
|
2008-04-11 11:35:09 -04:00
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2015-10-30 19:30:18 -04:00
|
|
|
def test_descends_from_active_record
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate ActiveRecord::Base, :descends_from_active_record?
|
2015-10-30 19:30:18 -04:00
|
|
|
|
|
|
|
# Abstract subclass of AR::Base.
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate LoosePerson, :descends_from_active_record?
|
2015-10-30 19:30:18 -04:00
|
|
|
|
|
|
|
# Concrete subclass of an abstract class.
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate LooseDescendant, :descends_from_active_record?
|
2015-10-30 19:30:18 -04:00
|
|
|
|
|
|
|
# Concrete subclass of AR::Base.
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate TightPerson, :descends_from_active_record?
|
2015-10-30 19:30:18 -04:00
|
|
|
|
|
|
|
# Concrete subclass of a concrete class but has no type column.
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate TightDescendant, :descends_from_active_record?
|
2015-10-30 19:30:18 -04:00
|
|
|
|
|
|
|
# Concrete subclass of AR::Base.
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate Post, :descends_from_active_record?
|
2015-10-30 19:30:18 -04:00
|
|
|
|
2017-08-12 13:29:27 -04:00
|
|
|
# Concrete subclasses of a concrete class which has a type column.
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate StiPost, :descends_from_active_record?
|
|
|
|
assert_not_predicate SubStiPost, :descends_from_active_record?
|
2017-08-12 13:29:27 -04:00
|
|
|
|
2015-10-30 19:30:18 -04:00
|
|
|
# Abstract subclass of a concrete class which has a type column.
|
|
|
|
# This is pathological, as you'll never have Sub < Abstract < Concrete.
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate AbstractStiPost, :descends_from_active_record?
|
2015-10-30 19:30:18 -04:00
|
|
|
|
2017-08-12 13:29:27 -04:00
|
|
|
# Concrete subclass of an abstract class which has a type column.
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate SubAbstractStiPost, :descends_from_active_record?
|
2015-10-30 19:30:18 -04:00
|
|
|
end
|
|
|
|
|
2007-01-22 23:19:16 -05:00
|
|
|
def test_company_descends_from_active_record
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate ActiveRecord::Base, :descends_from_active_record?
|
2016-08-06 12:26:20 -04:00
|
|
|
assert AbstractCompany.descends_from_active_record?, "AbstractCompany should descend from ActiveRecord::Base"
|
|
|
|
assert Company.descends_from_active_record?, "Company should descend from ActiveRecord::Base"
|
2018-05-12 22:26:10 -04:00
|
|
|
assert_not Class.new(Company).descends_from_active_record?, "Company subclass should not descend from ActiveRecord::Base"
|
2007-01-22 23:19:16 -05:00
|
|
|
end
|
|
|
|
|
2015-10-30 19:30:18 -04:00
|
|
|
def test_abstract_class
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_not_predicate ActiveRecord::Base, :abstract_class?
|
|
|
|
assert_predicate LoosePerson, :abstract_class?
|
|
|
|
assert_not_predicate LooseDescendant, :abstract_class?
|
2015-10-30 19:30:18 -04:00
|
|
|
end
|
|
|
|
|
2012-07-26 13:46:49 -04:00
|
|
|
def test_inheritance_base_class
|
|
|
|
assert_equal Post, Post.base_class
|
2018-04-02 07:17:24 -04:00
|
|
|
assert_predicate Post, :base_class?
|
2012-07-26 13:46:49 -04:00
|
|
|
assert_equal Post, SpecialPost.base_class
|
2018-04-02 07:17:24 -04:00
|
|
|
assert_not_predicate SpecialPost, :base_class?
|
2012-07-26 13:46:49 -04:00
|
|
|
assert_equal Post, StiPost.base_class
|
2018-04-02 07:17:24 -04:00
|
|
|
assert_not_predicate StiPost, :base_class?
|
2017-08-12 13:29:27 -04:00
|
|
|
assert_equal Post, SubStiPost.base_class
|
2018-04-02 07:17:24 -04:00
|
|
|
assert_not_predicate SubStiPost, :base_class?
|
2017-08-12 13:29:27 -04:00
|
|
|
assert_equal SubAbstractStiPost, SubAbstractStiPost.base_class
|
2018-04-02 07:17:24 -04:00
|
|
|
assert_predicate SubAbstractStiPost, :base_class?
|
2012-07-26 13:46:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_abstract_inheritance_base_class
|
|
|
|
assert_equal LoosePerson, LoosePerson.base_class
|
2018-04-02 07:17:24 -04:00
|
|
|
assert_predicate LoosePerson, :base_class?
|
2012-07-26 13:46:49 -04:00
|
|
|
assert_equal LooseDescendant, LooseDescendant.base_class
|
2018-04-02 07:17:24 -04:00
|
|
|
assert_predicate LooseDescendant, :base_class?
|
2012-07-26 13:46:49 -04:00
|
|
|
assert_equal TightPerson, TightPerson.base_class
|
2018-04-02 07:17:24 -04:00
|
|
|
assert_predicate TightPerson, :base_class?
|
2012-07-26 13:46:49 -04:00
|
|
|
assert_equal TightPerson, TightDescendant.base_class
|
2018-04-02 07:17:24 -04:00
|
|
|
assert_not_predicate TightDescendant, :base_class?
|
2012-07-26 13:46:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_base_class_activerecord_error
|
2021-04-10 13:24:26 -04:00
|
|
|
assert_raise(ActiveRecord::ActiveRecordError) do
|
|
|
|
Class.new { include ActiveRecord::Inheritance }
|
|
|
|
end
|
2012-07-26 13:46:49 -04:00
|
|
|
end
|
|
|
|
|
2004-12-14 07:32:29 -05:00
|
|
|
def test_a_bad_type_column
|
2005-11-16 03:16:54 -05:00
|
|
|
Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')"
|
2005-10-27 04:18:41 -04:00
|
|
|
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.find(100) }
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_find
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_kind_of Firm, Company.find(1), "37signals should be a firm"
|
|
|
|
assert_kind_of Firm, Firm.find(1), "37signals should be a firm"
|
|
|
|
assert_kind_of Client, Company.find(2), "Summit should be a client"
|
|
|
|
assert_kind_of Client, Client.find(2), "Summit should be a client"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_inheritance_find
|
2012-09-03 14:30:43 -04:00
|
|
|
assert_kind_of Cucumber, Vegetable.find(1)
|
|
|
|
assert_kind_of Cucumber, Cucumber.find(1)
|
|
|
|
assert_kind_of Cabbage, Vegetable.find(2)
|
|
|
|
assert_kind_of Cabbage, Cabbage.find(2)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2012-09-02 11:19:50 -04:00
|
|
|
def test_alt_becomes_works_with_sti
|
|
|
|
vegetable = Vegetable.find(1)
|
|
|
|
assert_kind_of Vegetable, vegetable
|
|
|
|
cabbage = vegetable.becomes(Cabbage)
|
|
|
|
assert_kind_of Cabbage, cabbage
|
|
|
|
end
|
|
|
|
|
2020-09-20 12:24:19 -04:00
|
|
|
def test_becomes_sets_variables_before_initialization_callbacks
|
|
|
|
vegetable = Vegetable.create!(name: "yelling carrot")
|
|
|
|
assert_kind_of Vegetable, vegetable
|
|
|
|
assert_equal "yelling carrot", vegetable.name
|
|
|
|
|
|
|
|
yelling_veggie = vegetable.becomes(YellingVegetable)
|
|
|
|
assert_equal "YELLING CARROT", yelling_veggie.name, "YellingVegetable name should be YELLING CARROT"
|
|
|
|
assert_equal "YELLING CARROT", vegetable.name, "Vegetable name should be YELLING CARROT after becoming a YellingVegetable"
|
|
|
|
end
|
|
|
|
|
2014-10-02 04:02:30 -04:00
|
|
|
def test_becomes_and_change_tracking_for_inheritance_columns
|
|
|
|
cucumber = Vegetable.find(1)
|
|
|
|
cabbage = cucumber.becomes!(Cabbage)
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal ["Cucumber", "Cabbage"], cabbage.custom_type_change
|
2014-10-02 04:02:30 -04:00
|
|
|
end
|
|
|
|
|
2014-01-13 09:04:23 -05:00
|
|
|
def test_alt_becomes_bang_resets_inheritance_type_column
|
|
|
|
vegetable = Vegetable.create!(name: "Red Pepper")
|
|
|
|
assert_nil vegetable.custom_type
|
|
|
|
|
|
|
|
cabbage = vegetable.becomes!(Cabbage)
|
|
|
|
assert_equal "Cabbage", cabbage.custom_type
|
|
|
|
|
2019-01-09 04:09:01 -05:00
|
|
|
cabbage.becomes!(Vegetable)
|
2014-01-13 09:04:23 -05:00
|
|
|
assert_nil cabbage.custom_type
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_inheritance_find_all
|
2016-08-06 13:37:57 -04:00
|
|
|
companies = Company.all.merge!(order: "id").to_a
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_kind_of Firm, companies[0], "37signals should be a firm"
|
|
|
|
assert_kind_of Client, companies[1], "Summit should be a client"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_inheritance_find_all
|
2016-08-06 13:37:57 -04:00
|
|
|
companies = Vegetable.all.merge!(order: "id").to_a
|
2012-09-03 14:30:43 -04:00
|
|
|
assert_kind_of Cucumber, companies[0]
|
|
|
|
assert_kind_of Cabbage, companies[1]
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_save
|
|
|
|
firm = Firm.new
|
|
|
|
firm.name = "Next Angle"
|
|
|
|
firm.save
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
next_angle = Company.find(firm.id)
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_kind_of Firm, next_angle, "Next Angle should be a firm"
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_inheritance_save
|
2016-08-06 13:37:57 -04:00
|
|
|
cabbage = Cabbage.new(name: "Savoy")
|
2012-09-03 14:30:43 -04:00
|
|
|
cabbage.save!
|
|
|
|
|
|
|
|
savoy = Vegetable.find(cabbage.id)
|
|
|
|
assert_kind_of Cabbage, savoy
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2012-04-05 15:21:48 -04:00
|
|
|
def test_inheritance_new_with_default_class
|
|
|
|
company = Company.new
|
2012-11-29 06:24:05 -05:00
|
|
|
assert_equal Company, company.class
|
2012-04-05 15:21:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_new_with_base_class
|
2016-08-06 13:37:57 -04:00
|
|
|
company = Company.new(type: "Company")
|
2012-11-29 06:24:05 -05:00
|
|
|
assert_equal Company, company.class
|
2012-04-05 15:21:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_new_with_subclass
|
2016-08-06 13:37:57 -04:00
|
|
|
firm = Company.new(type: "Firm")
|
2012-11-29 06:24:05 -05:00
|
|
|
assert_equal Firm, firm.class
|
2012-04-05 15:21:48 -04:00
|
|
|
end
|
|
|
|
|
2017-12-11 00:53:35 -05:00
|
|
|
def test_where_new_with_subclass
|
|
|
|
firm = Company.where(type: "Firm").new
|
|
|
|
assert_equal Firm, firm.class
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_where_create_with_subclass
|
|
|
|
firm = Company.where(type: "Firm").create(name: "Basecamp")
|
|
|
|
assert_equal Firm, firm.class
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_where_create_bang_with_subclass
|
|
|
|
firm = Company.where(type: "Firm").create!(name: "Basecamp")
|
|
|
|
assert_equal Firm, firm.class
|
|
|
|
end
|
|
|
|
|
2013-03-18 02:45:39 -04:00
|
|
|
def test_new_with_abstract_class
|
|
|
|
e = assert_raises(NotImplementedError) do
|
|
|
|
AbstractCompany.new
|
|
|
|
end
|
2014-01-03 17:02:31 -05:00
|
|
|
assert_equal("AbstractCompany is an abstract class and cannot be instantiated.", e.message)
|
2013-03-18 02:45:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_new_with_ar_base
|
|
|
|
e = assert_raises(NotImplementedError) do
|
|
|
|
ActiveRecord::Base.new
|
|
|
|
end
|
2014-01-03 17:02:31 -05:00
|
|
|
assert_equal("ActiveRecord::Base is an abstract class and cannot be instantiated.", e.message)
|
2013-03-18 02:45:39 -04:00
|
|
|
end
|
|
|
|
|
2012-04-05 15:21:48 -04:00
|
|
|
def test_new_with_invalid_type
|
2016-08-06 13:37:57 -04:00
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.new(type: "InvalidType") }
|
2012-04-05 15:21:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_new_with_unrelated_type
|
2016-08-06 13:37:57 -04:00
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.new(type: "Account") }
|
2012-04-05 15:21:48 -04:00
|
|
|
end
|
|
|
|
|
2017-12-11 00:53:35 -05:00
|
|
|
def test_where_new_with_invalid_type
|
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.where(type: "InvalidType").new }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_where_new_with_unrelated_type
|
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.where(type: "Account").new }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_where_create_with_invalid_type
|
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.where(type: "InvalidType").create }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_where_create_with_unrelated_type
|
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.where(type: "Account").create }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_where_create_bang_with_invalid_type
|
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.where(type: "InvalidType").create! }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_where_create_bang_with_unrelated_type
|
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.where(type: "Account").create! }
|
|
|
|
end
|
|
|
|
|
2015-05-13 05:35:34 -04:00
|
|
|
def test_new_with_unrelated_namespaced_type
|
|
|
|
without_store_full_sti_class do
|
|
|
|
e = assert_raises ActiveRecord::SubclassNotFound do
|
2016-08-06 12:26:20 -04:00
|
|
|
Namespaced::Company.new(type: "Firm")
|
2015-05-13 05:35:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "Invalid single-table inheritance type: Namespaced::Firm is not a subclass of Namespaced::Company", e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-01 19:19:11 -04:00
|
|
|
def test_new_with_complex_inheritance
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_nothing_raised { Client.new(type: "VerySpecialClient") }
|
2013-04-01 19:19:11 -04:00
|
|
|
end
|
|
|
|
|
2015-03-26 13:26:19 -04:00
|
|
|
def test_new_without_storing_full_sti_class
|
|
|
|
without_store_full_sti_class do
|
2016-08-06 12:26:20 -04:00
|
|
|
item = Company.new(type: "SpecialCo")
|
2015-03-26 13:26:19 -04:00
|
|
|
assert_instance_of Company::SpecialCo, item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-28 17:32:38 -05:00
|
|
|
def test_new_with_autoload_paths
|
2017-05-15 10:17:28 -04:00
|
|
|
path = File.expand_path("../models/autoloadable", __dir__)
|
2013-02-28 17:32:38 -05:00
|
|
|
ActiveSupport::Dependencies.autoload_paths << path
|
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
firm = Company.new(type: "ExtraFirm")
|
2013-02-28 17:32:38 -05:00
|
|
|
assert_equal ExtraFirm, firm.class
|
|
|
|
ensure
|
|
|
|
ActiveSupport::Dependencies.autoload_paths.reject! { |p| p == path }
|
|
|
|
ActiveSupport::Dependencies.clear
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_inheritance_condition
|
2021-02-11 11:32:20 -05:00
|
|
|
assert_equal 12, Company.count
|
|
|
|
assert_equal 3, Firm.count
|
Ensure AR #second, #third, etc. finders work through associations
This commit fixes two regressions introduced in cafe31a078 where
newly created finder methods #second, #third, #forth, and #fifth
caused a NoMethodError error on reload associations and where we
were pulling the wrong element out of cached associations.
Examples:
some_book.authors.reload.second
# Before
# => NoMethodError: undefined method 'first' for nil:NilClass
# After
# => #<Author id: 2, name: "Sally Second", ...>
some_book.first.authors.first
some_book.first.authors.second
# Before
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 1, name: "Freddy First", ...>
# After
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 2, name: "Sally Second", ...>
Fixes #13783.
2014-01-21 17:34:39 -05:00
|
|
|
assert_equal 5, Client.count
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_inheritance_condition
|
2012-09-03 14:30:43 -04:00
|
|
|
assert_equal 4, Vegetable.count
|
|
|
|
assert_equal 1, Cucumber.count
|
|
|
|
assert_equal 3, Cabbage.count
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_finding_incorrect_type_data
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Firm.find(2) }
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_nothing_raised { Firm.find(1) }
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_finding_incorrect_type_data
|
2012-09-03 14:30:43 -04:00
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Cucumber.find(2) }
|
|
|
|
assert_nothing_raised { Cucumber.find(1) }
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_update_all_within_inheritance
|
|
|
|
Client.update_all "name = 'I am a client'"
|
2012-08-03 06:51:52 -04:00
|
|
|
assert_equal "I am a client", Client.first.name
|
2009-03-22 18:14:42 -04:00
|
|
|
# Order by added as otherwise Oracle tests were failing because of different order of results
|
2016-08-06 13:37:57 -04:00
|
|
|
assert_equal "37signals", Firm.all.merge!(order: "id").to_a.first.name
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_update_all_within_inheritance
|
2012-09-03 14:30:43 -04:00
|
|
|
Cabbage.update_all "name = 'the cabbage'"
|
|
|
|
assert_equal "the cabbage", Cabbage.first.name
|
|
|
|
assert_equal ["my cucumber"], Cucumber.all.map(&:name).uniq
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_destroy_all_within_inheritance
|
|
|
|
Client.destroy_all
|
2005-06-26 07:25:32 -04:00
|
|
|
assert_equal 0, Client.count
|
2021-02-11 11:32:20 -05:00
|
|
|
assert_equal 3, Firm.count
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_destroy_all_within_inheritance
|
2012-09-03 14:30:43 -04:00
|
|
|
Cabbage.destroy_all
|
|
|
|
assert_equal 0, Cabbage.count
|
|
|
|
assert_equal 1, Cucumber.count
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_find_first_within_inheritance
|
2016-08-06 13:37:57 -04:00
|
|
|
assert_kind_of Firm, Company.all.merge!(where: "name = '37signals'").first
|
|
|
|
assert_kind_of Firm, Firm.all.merge!(where: "name = '37signals'").first
|
|
|
|
assert_nil Client.all.merge!(where: "name = '37signals'").first
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_alt_find_first_within_inheritance
|
2016-08-06 13:37:57 -04:00
|
|
|
assert_kind_of Cabbage, Vegetable.all.merge!(where: "name = 'his cabbage'").first
|
|
|
|
assert_kind_of Cabbage, Cabbage.all.merge!(where: "name = 'his cabbage'").first
|
|
|
|
assert_nil Cucumber.all.merge!(where: "name = 'his cabbage'").first
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_complex_inheritance
|
|
|
|
very_special_client = VerySpecialClient.create("name" => "veryspecial")
|
2010-06-29 11:18:55 -04:00
|
|
|
assert_equal very_special_client, VerySpecialClient.where("name = 'veryspecial'").first
|
2016-08-06 13:37:57 -04:00
|
|
|
assert_equal very_special_client, SpecialClient.all.merge!(where: "name = 'veryspecial'").first
|
|
|
|
assert_equal very_special_client, Company.all.merge!(where: "name = 'veryspecial'").first
|
|
|
|
assert_equal very_special_client, Client.all.merge!(where: "name = 'veryspecial'").first
|
|
|
|
assert_equal 1, Client.all.merge!(where: "name = 'Summit'").to_a.size
|
2004-11-23 20:04:44 -05:00
|
|
|
assert_equal very_special_client, Client.find(very_special_client.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_alt_complex_inheritance
|
2012-09-03 14:30:43 -04:00
|
|
|
king_cole = KingCole.create("name" => "uniform heads")
|
|
|
|
assert_equal king_cole, KingCole.where("name = 'uniform heads'").first
|
2016-08-06 13:37:57 -04:00
|
|
|
assert_equal king_cole, GreenCabbage.all.merge!(where: "name = 'uniform heads'").first
|
|
|
|
assert_equal king_cole, Cabbage.all.merge!(where: "name = 'uniform heads'").first
|
|
|
|
assert_equal king_cole, Vegetable.all.merge!(where: "name = 'uniform heads'").first
|
|
|
|
assert_equal 1, Cabbage.all.merge!(where: "name = 'his cabbage'").to_a.size
|
2012-09-03 14:30:43 -04:00
|
|
|
assert_equal king_cole, Cabbage.find(king_cole.id)
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-12-18 22:10:39 -05:00
|
|
|
def test_eager_load_belongs_to_something_inherited
|
2016-08-06 13:37:57 -04:00
|
|
|
account = Account.all.merge!(includes: :firm).find(1)
|
2015-01-26 13:35:28 -05:00
|
|
|
assert account.association(:firm).loaded?, "association was not eager loaded"
|
2006-12-18 22:10:39 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2012-09-03 14:30:43 -04:00
|
|
|
def test_alt_eager_loading
|
2016-08-06 13:37:57 -04:00
|
|
|
cabbage = RedCabbage.all.merge!(includes: :seller).find(4)
|
2015-01-26 13:35:28 -05:00
|
|
|
assert cabbage.association(:seller).loaded?, "association was not eager loaded"
|
2012-09-03 14:30:43 -04:00
|
|
|
end
|
|
|
|
|
2008-07-10 23:18:41 -04:00
|
|
|
def test_eager_load_belongs_to_primary_key_quoting
|
2019-07-07 20:26:03 -04:00
|
|
|
c = Account.connection
|
Refactor Active Record to let Arel manage bind params
A common source of bugs and code bloat within Active Record has been the
need for us to maintain the list of bind values separately from the AST
they're associated with. This makes any sort of AST manipulation
incredibly difficult, as any time we want to potentially insert or
remove an AST node, we need to traverse the entire tree to find where
the associated bind parameters are.
With this change, the bind parameters now live on the AST directly.
Active Record does not need to know or care about them until the final
AST traversal for SQL construction. Rather than returning just the SQL,
the Arel collector will now return both the SQL and the bind parameters.
At this point the connection adapter will have all the values that it
had before.
A bit of this code is janky and something I'd like to refactor later. In
particular, I don't like how we're handling associations in the
predicate builder, the special casing of `StatementCache::Substitute` in
`QueryAttribute`, or generally how we're handling bind value replacement
in the statement cache when prepared statements are disabled.
This also mostly reverts #26378, as it moved all the code into a
location that I wanted to delete.
/cc @metaskills @yahonda, this change will affect the adapters
Fixes #29766.
Fixes #29804.
Fixes #26541.
Close #28539.
Close #24769.
Close #26468.
Close #26202.
There are probably other issues/PRs that can be closed because of this
commit, but that's all I could find on the first few pages.
2017-07-24 08:19:35 -04:00
|
|
|
bind_param = Arel::Nodes::BindParam.new(nil)
|
2019-07-07 20:26:03 -04:00
|
|
|
assert_sql(/#{Regexp.escape(c.quote_table_name("companies.id"))} = (?:#{Regexp.escape(bind_param.to_sql)}|1)/i) do
|
2016-08-06 13:37:57 -04:00
|
|
|
Account.all.merge!(includes: :firm).find(1)
|
2008-07-10 23:18:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-25 00:15:41 -05:00
|
|
|
def test_inherits_custom_primary_key
|
|
|
|
assert_equal Subscriber.primary_key, SpecialSubscriber.primary_key
|
|
|
|
end
|
|
|
|
|
2004-12-14 07:32:29 -05:00
|
|
|
def test_inheritance_without_mapping
|
2005-07-03 04:52:59 -04:00
|
|
|
assert_kind_of SpecialSubscriber, SpecialSubscriber.find("webster132")
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_nothing_raised { s = SpecialSubscriber.new("name" => "And breaaaaathe!"); s.id = "roger"; s.save }
|
2004-12-14 07:32:29 -05:00
|
|
|
end
|
2006-12-19 14:23:56 -05:00
|
|
|
|
2013-10-03 14:05:23 -04:00
|
|
|
def test_scope_inherited_properly
|
2020-05-28 14:07:26 -04:00
|
|
|
assert_nothing_raised { Company.of_first_firm.to_a }
|
|
|
|
assert_nothing_raised { Client.of_first_firm.to_a }
|
2013-10-03 14:05:23 -04:00
|
|
|
end
|
2017-05-31 03:11:43 -04:00
|
|
|
|
|
|
|
def test_inheritance_with_default_scope
|
|
|
|
assert_equal 1, SelectedMembership.count(:all)
|
|
|
|
end
|
2013-10-03 14:05:23 -04:00
|
|
|
end
|
2006-12-19 14:23:56 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class InheritanceComputeTypeTest < ActiveRecord::TestCase
|
2015-05-07 09:04:49 -04:00
|
|
|
include InheritanceTestHelper
|
2006-12-19 14:23:56 -05:00
|
|
|
fixtures :companies
|
|
|
|
|
|
|
|
def test_instantiation_doesnt_try_to_require_corresponding_file
|
2015-05-07 09:04:49 -04:00
|
|
|
without_store_full_sti_class do
|
|
|
|
foo = Firm.first.clone
|
2016-08-06 12:26:20 -04:00
|
|
|
foo.type = "FirmOnTheFly"
|
2015-05-07 09:04:49 -04:00
|
|
|
foo.save!
|
2006-12-19 14:23:56 -05:00
|
|
|
|
2015-05-07 09:04:49 -04:00
|
|
|
# Should fail without FirmOnTheFly in the type condition.
|
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Firm.find(foo.id) }
|
2019-02-26 12:31:39 -05:00
|
|
|
assert_raise(ActiveRecord::RecordNotFound) { Firm.find_by!(id: foo.id) }
|
2006-12-19 14:23:56 -05:00
|
|
|
|
2015-05-07 09:04:49 -04:00
|
|
|
# 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) }
|
2019-02-26 12:31:39 -05:00
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Firm.find_by!(id: foo.id) }
|
2006-12-19 14:23:56 -05:00
|
|
|
|
2015-05-07 09:04:49 -04:00
|
|
|
# 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)
|
2006-12-19 14:23:56 -05:00
|
|
|
|
2015-05-07 09:04:49 -04:00
|
|
|
# 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) }
|
2019-02-26 12:31:39 -05:00
|
|
|
assert_nothing_raised { assert_kind_of Firm::FirmOnTheFly, Firm.find_by!(id: foo.id) }
|
2015-05-07 09:04:49 -04:00
|
|
|
end
|
2020-06-09 05:59:30 -04:00
|
|
|
ensure
|
|
|
|
self.class.send(:remove_const, :FirmOnTheFly) rescue nil
|
|
|
|
Firm.send(:remove_const, :FirmOnTheFly) rescue nil
|
2006-12-19 14:23:56 -05:00
|
|
|
end
|
2014-01-14 08:23:45 -05:00
|
|
|
|
|
|
|
def test_sti_type_from_attributes_disabled_in_non_sti_class
|
2016-08-06 12:26:20 -04:00
|
|
|
phone = Shop::Product::Type.new(name: "Phone")
|
2016-08-06 13:37:57 -04:00
|
|
|
product = Shop::Product.new(type: phone)
|
2014-01-14 08:23:45 -05:00
|
|
|
assert product.save
|
|
|
|
end
|
2015-12-02 05:38:19 -05:00
|
|
|
|
|
|
|
def test_inheritance_new_with_subclass_as_default
|
|
|
|
original_type = Company.columns_hash["type"].default
|
2016-08-06 12:26:20 -04:00
|
|
|
ActiveRecord::Base.connection.change_column_default :companies, :type, "Firm"
|
2015-12-02 05:38:19 -05:00
|
|
|
Company.reset_column_information
|
2015-12-02 07:31:20 -05:00
|
|
|
|
|
|
|
firm = Company.new # without arguments
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "Firm", firm.type
|
2015-12-02 05:38:19 -05:00
|
|
|
assert_instance_of Firm, firm
|
2015-12-02 07:31:20 -05:00
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
firm = Company.new(firm_name: "Shri Hans Plastic") # with arguments
|
|
|
|
assert_equal "Firm", firm.type
|
2015-12-02 05:38:19 -05:00
|
|
|
assert_instance_of Firm, firm
|
2015-12-02 07:31:20 -05:00
|
|
|
|
2016-01-27 13:26:20 -05:00
|
|
|
client = Client.new
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "Client", client.type
|
2016-01-27 13:26:20 -05:00
|
|
|
assert_instance_of Client, client
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
firm = Company.new(type: "Client") # overwrite the default type
|
|
|
|
assert_equal "Client", firm.type
|
2015-12-02 05:38:19 -05:00
|
|
|
assert_instance_of Client, firm
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.connection.change_column_default :companies, :type, original_type
|
|
|
|
Company.reset_column_information
|
|
|
|
end
|
2006-12-19 14:23:56 -05:00
|
|
|
end
|
2015-12-02 11:02:11 -05:00
|
|
|
|
|
|
|
class InheritanceAttributeTest < ActiveRecord::TestCase
|
|
|
|
class Company < ActiveRecord::Base
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "companies"
|
2015-12-02 11:02:11 -05:00
|
|
|
attribute :type, :string, default: "InheritanceAttributeTest::Startup"
|
|
|
|
end
|
|
|
|
|
|
|
|
class Startup < Company
|
|
|
|
end
|
|
|
|
|
|
|
|
class Empire < Company
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_inheritance_new_with_subclass_as_default
|
|
|
|
startup = Company.new # without arguments
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal "InheritanceAttributeTest::Startup", startup.type
|
2015-12-02 11:02:11 -05:00
|
|
|
assert_instance_of Startup, startup
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
empire = Company.new(type: "InheritanceAttributeTest::Empire") # without arguments
|
|
|
|
assert_equal "InheritanceAttributeTest::Empire", empire.type
|
2015-12-02 11:02:11 -05:00
|
|
|
assert_instance_of Empire, empire
|
|
|
|
end
|
|
|
|
end
|
2016-01-19 11:27:21 -05:00
|
|
|
|
|
|
|
class InheritanceAttributeMappingTest < ActiveRecord::TestCase
|
|
|
|
setup do
|
|
|
|
Company.delete_all
|
|
|
|
Sponsor.delete_all
|
|
|
|
end
|
|
|
|
|
|
|
|
class OmgStiType < ActiveRecord::Type::String
|
|
|
|
def cast_value(value)
|
|
|
|
if value =~ /\Aomg_(.+)\z/
|
|
|
|
$1.classify
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def serialize(value)
|
|
|
|
if value
|
|
|
|
"omg_%s" % value.underscore
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-19 01:08:54 -05:00
|
|
|
ActiveRecord::Type.register :omg_sti, OmgStiType
|
|
|
|
|
2016-01-19 11:27:21 -05:00
|
|
|
class Company < ActiveRecord::Base
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "companies"
|
2016-01-19 11:27:21 -05:00
|
|
|
attribute :type, :omg_sti
|
|
|
|
end
|
|
|
|
|
|
|
|
class Startup < Company; end
|
|
|
|
class Empire < Company; end
|
|
|
|
|
|
|
|
class Sponsor < ActiveRecord::Base
|
2016-08-06 12:26:20 -04:00
|
|
|
self.table_name = "sponsors"
|
2016-01-19 11:27:21 -05:00
|
|
|
attribute :sponsorable_type, :omg_sti
|
|
|
|
|
|
|
|
belongs_to :sponsorable, polymorphic: true
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sti_with_custom_type
|
2016-08-06 12:26:20 -04:00
|
|
|
Startup.create! name: "a Startup"
|
|
|
|
Empire.create! name: "an Empire"
|
2016-01-19 11:27:21 -05:00
|
|
|
|
|
|
|
assert_equal [["a Startup", "omg_inheritance_attribute_mapping_test/startup"],
|
2016-08-06 12:26:20 -04:00
|
|
|
["an Empire", "omg_inheritance_attribute_mapping_test/empire"]], ActiveRecord::Base.connection.select_rows("SELECT name, type FROM companies").sort
|
2016-01-19 11:27:21 -05:00
|
|
|
assert_equal [["a Startup", "InheritanceAttributeMappingTest::Startup"],
|
2016-01-19 12:15:25 -05:00
|
|
|
["an Empire", "InheritanceAttributeMappingTest::Empire"]], Company.all.map { |a| [a.name, a.type] }.sort
|
2016-01-19 11:27:21 -05:00
|
|
|
|
|
|
|
startup = Startup.first
|
|
|
|
startup.becomes! Empire
|
|
|
|
startup.save!
|
|
|
|
|
|
|
|
assert_equal [["a Startup", "omg_inheritance_attribute_mapping_test/empire"],
|
2016-08-06 12:26:20 -04:00
|
|
|
["an Empire", "omg_inheritance_attribute_mapping_test/empire"]], ActiveRecord::Base.connection.select_rows("SELECT name, type FROM companies").sort
|
2016-01-19 12:15:25 -05:00
|
|
|
|
2016-01-19 11:27:21 -05:00
|
|
|
assert_equal [["a Startup", "InheritanceAttributeMappingTest::Empire"],
|
2016-01-19 12:15:25 -05:00
|
|
|
["an Empire", "InheritanceAttributeMappingTest::Empire"]], Company.all.map { |a| [a.name, a.type] }.sort
|
2016-01-19 11:27:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_polymorphic_associations_custom_type
|
2016-08-06 12:26:20 -04:00
|
|
|
startup = Startup.create! name: "a Startup"
|
2016-01-19 11:27:21 -05:00
|
|
|
sponsor = Sponsor.create! sponsorable: startup
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal ["omg_inheritance_attribute_mapping_test/company"], ActiveRecord::Base.connection.select_values("SELECT sponsorable_type FROM sponsors")
|
2016-01-19 11:27:21 -05:00
|
|
|
|
2019-01-09 04:09:01 -05:00
|
|
|
sponsor = Sponsor.find(sponsor.id)
|
2016-01-19 11:27:21 -05:00
|
|
|
assert_equal startup, sponsor.sponsorable
|
|
|
|
end
|
|
|
|
end
|