Make `application_record_class` a module instance variable

Followup: https://github.com/rails/rails/pull/42442
This commit is contained in:
Jean Boussier 2021-06-10 16:12:57 +02:00
parent d0e2b00570
commit 40f6bd0eb9
5 changed files with 16 additions and 15 deletions

View File

@ -194,6 +194,9 @@ module ActiveRecord
singleton_class.attr_accessor :warn_on_records_fetched_greater_than
self.warn_on_records_fetched_greater_than = false
singleton_class.attr_accessor :application_record_class
self.application_record_class = nil
def self.eager_load!
super
ActiveRecord::Locking.eager_load!

View File

@ -146,8 +146,6 @@ module ActiveRecord
class_attribute :default_shard, instance_writer: false
mattr_accessor :application_record_class, instance_accessor: false, default: nil
# Sets the async_query_executor for an application. By default the thread pool executor
# set to +nil+ which will not run queries in the background. Applications must configure
# a thread pool executor to use this feature. Options are:
@ -185,8 +183,8 @@ module ActiveRecord
end
def self.application_record_class? # :nodoc:
if Base.application_record_class
self == Base.application_record_class
if ActiveRecord.application_record_class
self == ActiveRecord.application_record_class
else
if defined?(ApplicationRecord) && self == ApplicationRecord
true

View File

@ -163,12 +163,12 @@ module ActiveRecord
# will share a database connection with Active Record. It is the class
# that connects to your primary database.
def primary_abstract_class
if Base.application_record_class && Base.application_record_class.name != name
raise ArgumentError, "The `primary_abstract_class` is already set to #{Base.application_record_class.inspect}. There can only be one `primary_abstract_class` in an application."
if ActiveRecord.application_record_class && ActiveRecord.application_record_class.name != name
raise ArgumentError, "The `primary_abstract_class` is already set to #{ActiveRecord.application_record_class.inspect}. There can only be one `primary_abstract_class` in an application."
end
self.abstract_class = true
Base.application_record_class = self
ActiveRecord.application_record_class = self
end
# Returns the value to be stored in the inheritance column for STI.

View File

@ -448,7 +448,7 @@ module ActiveRecord
end
end
ensure
ActiveRecord::Base.application_record_class = nil
ActiveRecord.application_record_class = nil
Object.send(:remove_const, :ApplicationRecord)
ActiveRecord::Base.establish_connection :arunit
end

View File

@ -27,7 +27,7 @@ class PrimaryClassTest < ActiveRecord::TestCase
assert_predicate ApplicationRecord, :application_record_class?
assert_predicate ApplicationRecord, :abstract_class?
ensure
ActiveRecord::Base.application_record_class = nil
ActiveRecord.application_record_class = nil
Object.send(:remove_const, :ApplicationRecord)
end
@ -46,7 +46,7 @@ class PrimaryClassTest < ActiveRecord::TestCase
assert_not_predicate ActiveRecord::Base, :application_record_class?
assert_not_predicate ActiveRecord::Base, :abstract_class?
ensure
ActiveRecord::Base.application_record_class = nil
ActiveRecord.application_record_class = nil
end
def test_primary_abstract_class_cannot_be_reset
@ -56,7 +56,7 @@ class PrimaryClassTest < ActiveRecord::TestCase
PrimaryClassTest::AnotherAppRecord.primary_abstract_class
end
ensure
ActiveRecord::Base.application_record_class = nil
ActiveRecord.application_record_class = nil
end
def test_primary_abstract_class_is_used_over_application_record_if_set
@ -75,7 +75,7 @@ class PrimaryClassTest < ActiveRecord::TestCase
assert_not_predicate ActiveRecord::Base, :application_record_class?
assert_not_predicate ActiveRecord::Base, :abstract_class?
ensure
ActiveRecord::Base.application_record_class = nil
ActiveRecord.application_record_class = nil
Object.send(:remove_const, :ApplicationRecord)
end
@ -96,7 +96,7 @@ class PrimaryClassTest < ActiveRecord::TestCase
assert_not_predicate ApplicationRecord, :application_record_class?
assert_predicate ApplicationRecord, :abstract_class?
ensure
ActiveRecord::Base.application_record_class = nil
ActiveRecord.application_record_class = nil
Object.send(:remove_const, :ApplicationRecord)
end
@ -110,7 +110,7 @@ class PrimaryClassTest < ActiveRecord::TestCase
assert_predicate ApplicationRecord, :application_record_class?
assert_equal ActiveRecord::Base.connection, ApplicationRecord.connection
ensure
ActiveRecord::Base.application_record_class = nil
ActiveRecord.application_record_class = nil
Object.send(:remove_const, :ApplicationRecord)
ActiveRecord::Base.establish_connection :arunit
end
@ -125,7 +125,7 @@ class PrimaryClassTest < ActiveRecord::TestCase
assert_predicate PrimaryClassTest::PrimaryAppRecord, :abstract_class?
assert_equal ActiveRecord::Base.connection, PrimaryClassTest::PrimaryAppRecord.connection
ensure
ActiveRecord::Base.application_record_class = nil
ActiveRecord.application_record_class = nil
ActiveRecord::Base.establish_connection :arunit
end
end