2014-01-14 08:23:45 -05:00
|
|
|
require 'cases/helper'
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/company'
|
2012-07-26 13:46:49 -04:00
|
|
|
require 'models/person'
|
|
|
|
require 'models/post'
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/project'
|
|
|
|
require 'models/subscriber'
|
2012-09-02 11:19:50 -04:00
|
|
|
require 'models/vegetables'
|
2014-01-14 08:23:45 -05:00
|
|
|
require 'models/shop'
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class InheritanceTest < ActiveRecord::TestCase
|
2012-09-02 11:19:50 -04:00
|
|
|
fixtures :companies, :projects, :subscribers, :accounts, :vegetables
|
2008-05-31 20:13:11 -04:00
|
|
|
|
|
|
|
def test_class_with_store_full_sti_class_returns_full_name
|
|
|
|
old = ActiveRecord::Base.store_full_sti_class
|
|
|
|
ActiveRecord::Base.store_full_sti_class = true
|
|
|
|
assert_equal 'Namespaced::Company', Namespaced::Company.sti_name
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = old
|
|
|
|
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
|
2010-09-30 19:02:49 -04:00
|
|
|
company.extend(Module.new {
|
|
|
|
def read_attribute(name)
|
|
|
|
return ' ' if name == 'type'
|
|
|
|
super
|
|
|
|
end
|
|
|
|
})
|
|
|
|
company.save!
|
2012-08-03 06:51:52 -04:00
|
|
|
company = Company.all.to_a.find { |x| x.id == company.id }
|
2010-09-30 19:02:49 -04:00
|
|
|
assert_equal ' ', company.type
|
|
|
|
end
|
|
|
|
|
2008-05-31 20:13:11 -04:00
|
|
|
def test_class_without_store_full_sti_class_returns_demodulized_name
|
|
|
|
old = ActiveRecord::Base.store_full_sti_class
|
|
|
|
ActiveRecord::Base.store_full_sti_class = false
|
|
|
|
assert_equal 'Company', Namespaced::Company.sti_name
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = old
|
|
|
|
end
|
|
|
|
|
2008-04-11 11:35:09 -04:00
|
|
|
def test_should_store_demodulized_class_name_with_store_full_sti_class_option_disabled
|
|
|
|
old = ActiveRecord::Base.store_full_sti_class
|
|
|
|
ActiveRecord::Base.store_full_sti_class = false
|
|
|
|
item = Namespaced::Company.new
|
|
|
|
assert_equal 'Company', item[:type]
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = old
|
|
|
|
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
|
|
|
|
old = ActiveRecord::Base.store_full_sti_class
|
|
|
|
ActiveRecord::Base.store_full_sti_class = true
|
|
|
|
item = Namespaced::Company.new
|
|
|
|
assert_equal 'Namespaced::Company', item[:type]
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = old
|
|
|
|
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
|
|
|
|
old = ActiveRecord::Base.store_full_sti_class
|
|
|
|
ActiveRecord::Base.store_full_sti_class = true
|
|
|
|
item = Namespaced::Company.create :name => "Wolverine 2"
|
|
|
|
assert_not_nil Company.find(item.id)
|
|
|
|
assert_not_nil Namespaced::Company.find(item.id)
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = old
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2007-01-22 23:19:16 -05:00
|
|
|
def test_company_descends_from_active_record
|
2013-03-11 07:27:13 -04:00
|
|
|
assert !ActiveRecord::Base.descends_from_active_record?
|
2007-01-22 23:19:16 -05: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'
|
|
|
|
assert !Class.new(Company).descends_from_active_record?, 'Company subclass should not descend from ActiveRecord::Base'
|
|
|
|
end
|
|
|
|
|
2012-07-26 13:46:49 -04:00
|
|
|
def test_inheritance_base_class
|
|
|
|
assert_equal Post, Post.base_class
|
|
|
|
assert_equal Post, SpecialPost.base_class
|
|
|
|
assert_equal Post, StiPost.base_class
|
|
|
|
assert_equal SubStiPost, SubStiPost.base_class
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_abstract_inheritance_base_class
|
|
|
|
assert_equal LoosePerson, LoosePerson.base_class
|
|
|
|
assert_equal LooseDescendant, LooseDescendant.base_class
|
|
|
|
assert_equal TightPerson, TightPerson.base_class
|
|
|
|
assert_equal TightPerson, TightDescendant.base_class
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_base_class_activerecord_error
|
2012-10-26 10:51:02 -04:00
|
|
|
klass = Class.new { include ActiveRecord::Inheritance }
|
2012-07-26 13:46:49 -04:00
|
|
|
assert_raise(ActiveRecord::ActiveRecordError) { klass.base_class }
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
vegetable = cabbage.becomes!(Vegetable)
|
|
|
|
assert_nil cabbage.custom_type
|
|
|
|
end
|
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def test_inheritance_find_all
|
2012-07-27 12:27:47 -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
|
2012-09-03 14:30:43 -04:00
|
|
|
companies = Vegetable.all.merge!(:order => 'id').to_a
|
|
|
|
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
|
2012-09-03 14:30:43 -04:00
|
|
|
cabbage = Cabbage.new(:name => 'Savoy')
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.new(:type => 'InvalidType') }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_new_with_unrelated_type
|
|
|
|
assert_raise(ActiveRecord::SubclassNotFound) { Company.new(:type => 'Account') }
|
|
|
|
end
|
|
|
|
|
2013-04-01 19:19:11 -04:00
|
|
|
def test_new_with_complex_inheritance
|
|
|
|
assert_nothing_raised { Client.new(type: 'VerySpecialClient') }
|
|
|
|
end
|
|
|
|
|
2013-02-28 17:32:38 -05:00
|
|
|
def test_new_with_autoload_paths
|
|
|
|
path = File.expand_path('../../models/autoloadable', __FILE__)
|
|
|
|
ActiveSupport::Dependencies.autoload_paths << path
|
|
|
|
|
|
|
|
firm = Company.new(:type => 'ExtraFirm')
|
|
|
|
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
|
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 11, Company.count
|
2005-06-26 07:25:32 -04:00
|
|
|
assert_equal 2, 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
|
2012-07-27 12:27:47 -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
|
|
|
|
assert_equal 2, 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
|
2012-07-27 12:27:47 -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
|
2012-09-03 14:30:43 -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
|
2012-07-27 12:27:47 -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
|
|
|
|
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
|
|
|
|
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
|
2012-07-27 12:27:47 -04:00
|
|
|
account = Account.all.merge!(:includes => :firm).find(1)
|
2011-01-07 14:28:11 -05:00
|
|
|
assert account.association_cache.key?(:firm), "nil proves eager load failed"
|
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
|
|
|
|
cabbage = RedCabbage.all.merge!(:includes => :seller).find(4)
|
|
|
|
assert cabbage.association_cache.key?(:seller), "nil proves eager load failed"
|
|
|
|
end
|
|
|
|
|
2008-07-10 23:18:41 -04:00
|
|
|
def test_eager_load_belongs_to_primary_key_quoting
|
|
|
|
con = Account.connection
|
2011-02-22 19:17:01 -05:00
|
|
|
assert_sql(/#{con.quote_table_name('companies')}.#{con.quote_column_name('id')} IN \(1\)/) do
|
2012-07-27 12:27:47 -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")
|
2005-10-12 15:55:46 -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
|
|
|
|
assert_nothing_raised { Company.of_first_firm }
|
|
|
|
assert_nothing_raised { Client.of_first_firm }
|
|
|
|
end
|
|
|
|
end
|
2006-12-19 14:23:56 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class InheritanceComputeTypeTest < ActiveRecord::TestCase
|
2006-12-19 14:23:56 -05:00
|
|
|
fixtures :companies
|
|
|
|
|
|
|
|
def setup
|
2008-06-03 14:32:53 -04:00
|
|
|
ActiveSupport::Dependencies.log_activity = true
|
2006-12-19 14:23:56 -05:00
|
|
|
end
|
|
|
|
|
2014-03-14 00:35:58 -04:00
|
|
|
teardown do
|
2008-06-03 14:32:53 -04:00
|
|
|
ActiveSupport::Dependencies.log_activity = false
|
2006-12-19 14:23:56 -05:00
|
|
|
self.class.const_remove :FirmOnTheFly rescue nil
|
|
|
|
Firm.const_remove :FirmOnTheFly rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_instantiation_doesnt_try_to_require_corresponding_file
|
2010-01-03 22:30:28 -05:00
|
|
|
ActiveRecord::Base.store_full_sti_class = false
|
2012-04-26 13:32:55 -04:00
|
|
|
foo = Firm.first.clone
|
2012-09-03 14:38:14 -04:00
|
|
|
foo.type = 'FirmOnTheFly'
|
2006-12-19 14:23:56 -05:00
|
|
|
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) }
|
2010-01-03 22:30:28 -05:00
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.store_full_sti_class = true
|
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
|
|
|
|
phone = Shop::Product::Type.new(name: 'Phone')
|
|
|
|
product = Shop::Product.new(:type => phone)
|
|
|
|
assert product.save
|
|
|
|
end
|
2006-12-19 14:23:56 -05:00
|
|
|
end
|