Fix usage of self.inherited
This commit fixes issue #50003 where using `super` after adding relations to the subclass or defining filter attributes in the subclass in the method `self.inherited` lead to warnings and removed previously defined filter_attributes
This commit is contained in:
parent
16607e349a
commit
e56a09430b
@ -384,8 +384,8 @@ def inherited(subclass)
|
||||
@arel_table = nil
|
||||
@predicate_builder = nil
|
||||
@inspection_filter = nil
|
||||
@filter_attributes = nil
|
||||
@generated_association_methods = nil
|
||||
@filter_attributes ||= nil
|
||||
@generated_association_methods ||= nil
|
||||
end
|
||||
end
|
||||
|
||||
|
40
activerecord/test/cases/inherited_test.rb
Normal file
40
activerecord/test/cases/inherited_test.rb
Normal file
@ -0,0 +1,40 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "cases/helper"
|
||||
|
||||
module Inherited
|
||||
# When running the test with `RAILS_STRICT_WARNINGS` enabled, the `belongs_to`
|
||||
# call should not emit a warning that the constant `GeneratedAssociationMethods`
|
||||
# is already defined.
|
||||
class Person < ActiveRecord::Base; end
|
||||
|
||||
class Device < ActiveRecord::Base
|
||||
def self.inherited(subclass)
|
||||
subclass.belongs_to :person, inverse_of: subclass.name.demodulize.tableize.to_sym
|
||||
subclass.filter_attributes = [:secret_attribute, :"#{subclass.name.demodulize.downcase}_key"]
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
class Computer < Device; end
|
||||
|
||||
class Vehicle < ActiveRecord::Base
|
||||
def self.inherited(subclass)
|
||||
super
|
||||
subclass.belongs_to :person, inverse_of: subclass.name.demodulize.tableize.to_sym
|
||||
subclass.filter_attributes = [:secret_attribute, :"#{subclass.name.demodulize.downcase}_key"]
|
||||
end
|
||||
end
|
||||
|
||||
class Car < Vehicle; end
|
||||
end
|
||||
|
||||
class InheritedTest < ActiveRecord::TestCase
|
||||
def test_super_before_filter_attributes
|
||||
assert_equal %i[secret_attribute car_key], Inherited::Car.filter_attributes
|
||||
end
|
||||
|
||||
def test_super_after_filter_attributes
|
||||
assert_equal %i[secret_attribute computer_key], Inherited::Computer.filter_attributes
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user