Make `action_on_strict_loading_violation` a module instance variable

Followup: https://github.com/rails/rails/pull/42442
This commit is contained in:
Jean Boussier 2021-06-10 16:21:20 +02:00
parent a50ff734cb
commit e65042707e
3 changed files with 13 additions and 12 deletions

View File

@ -197,6 +197,13 @@ module ActiveRecord
singleton_class.attr_accessor :application_record_class
self.application_record_class = nil
##
# :singleton-method:
# Set the application to log or raise when an association violates strict loading.
# Defaults to :raise.
singleton_class.attr_accessor :action_on_strict_loading_violation
self.action_on_strict_loading_violation = :raise
def self.eager_load!
super
ActiveRecord::Locking.eager_load!

View File

@ -127,12 +127,6 @@ module ActiveRecord
class_attribute :belongs_to_required_by_default, instance_accessor: false
##
# :singleton-method:
# Set the application to log or raise when an association violates strict loading.
# Defaults to :raise.
mattr_accessor :action_on_strict_loading_violation, instance_accessor: false, default: :raise
class_attribute :strict_loading_by_default, instance_accessor: false, default: false
class_attribute :strict_loading_mode, instance_accessor: true, default: :all
@ -345,7 +339,7 @@ module ActiveRecord
self.default_shard = :default
def self.strict_loading_violation!(owner:, reflection:) # :nodoc:
case action_on_strict_loading_violation
case ActiveRecord.action_on_strict_loading_violation
when :raise
message = "`#{owner}` is marked for strict_loading. The `#{reflection.klass}` association named `:#{reflection.name}` cannot be lazily loaded."
raise ActiveRecord::StrictLoadingViolationError.new(message)

View File

@ -548,7 +548,7 @@ class StrictLoadingTest < ActiveRecord::TestCase
end
def test_strict_loading_violation_raises_by_default
assert_equal :raise, ActiveRecord::Base.action_on_strict_loading_violation
assert_equal :raise, ActiveRecord.action_on_strict_loading_violation
developer = Developer.first
assert_not_predicate developer, :strict_loading?
@ -562,9 +562,9 @@ class StrictLoadingTest < ActiveRecord::TestCase
end
def test_strict_loading_violation_can_log_instead_of_raise
old_value = ActiveRecord::Base.action_on_strict_loading_violation
ActiveRecord::Base.action_on_strict_loading_violation = :log
assert_equal :log, ActiveRecord::Base.action_on_strict_loading_violation
old_value = ActiveRecord.action_on_strict_loading_violation
ActiveRecord.action_on_strict_loading_violation = :log
assert_equal :log, ActiveRecord.action_on_strict_loading_violation
developer = Developer.first
assert_not_predicate developer, :strict_loading?
@ -576,7 +576,7 @@ class StrictLoadingTest < ActiveRecord::TestCase
developer.audit_logs.to_a
end
ensure
ActiveRecord::Base.action_on_strict_loading_violation = old_value
ActiveRecord.action_on_strict_loading_violation = old_value
end
private