Skip to content

Commit ba4fde3

Browse files
authored
Merge pull request rails#42451 from Shopify/active-record-cattr-3
Get rid of `cattr_accessor` in `ActiveRecord::Base`
2 parents 3cac9d8 + 2d79790 commit ba4fde3

File tree

6 files changed

+13
-13
lines changed

6 files changed

+13
-13
lines changed

activerecord/lib/active_record.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ def self.global_executor_concurrency # :nodoc:
222222
@global_executor_concurrency ||= nil
223223
end
224224

225+
singleton_class.attr_accessor :index_nested_attribute_errors
226+
self.index_nested_attribute_errors = false
227+
225228
##
226229
# :singleton-method:
227230
#

activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ def map_avoiding_infinite_recursion(value)
6363
extend ActiveSupport::Concern
6464

6565
included do
66-
mattr_accessor :time_zone_aware_attributes, instance_writer: false, default: false
67-
66+
class_attribute :time_zone_aware_attributes, instance_writer: false, default: false
6867
class_attribute :skip_time_zone_conversion_for_attributes, instance_writer: false, default: []
6968
class_attribute :time_zone_aware_types, instance_writer: false, default: [ :datetime, :time ]
7069
end

activerecord/lib/active_record/autosave_association.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ def self.valid_options
150150

151151
included do
152152
Associations::Builder::Association.extensions << AssociationBuilderExtension
153-
mattr_accessor :index_nested_attribute_errors, instance_writer: false, default: false
154153
end
155154

156155
module ClassMethods # :nodoc:
@@ -335,7 +334,7 @@ def association_valid?(reflection, record, index = nil)
335334

336335
unless valid = record.valid?(context)
337336
if reflection.options[:autosave]
338-
indexed_attribute = !index.nil? && (reflection.options[:index_errors] || ActiveRecord::Base.index_nested_attribute_errors)
337+
indexed_attribute = !index.nil? && (reflection.options[:index_errors] || ActiveRecord.index_nested_attribute_errors)
339338

340339
record.errors.group_by_attribute.each { |attribute, errors|
341340
attribute = normalize_reflection_attribute(indexed_attribute, reflection, index, attribute)

activerecord/lib/active_record/model_schema.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ module ModelSchema
127127
# <tt>attribute :foo, :string</tt>. Defaults to false.
128128

129129
included do
130-
mattr_accessor :primary_key_prefix_type, instance_writer: false
131-
130+
class_attribute :primary_key_prefix_type, instance_writer: false
132131
class_attribute :table_name_prefix, instance_writer: false, default: ""
133132
class_attribute :table_name_suffix, instance_writer: false, default: ""
134133
class_attribute :schema_migrations_table_name, instance_accessor: false, default: "schema_migrations"

activerecord/lib/active_record/signed_id.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module SignedId
1010
# :singleton-method:
1111
# Set the secret used for the signed id verifier instance when using Active Record outside of Rails.
1212
# Within Rails, this is automatically set using the Rails application key generator.
13-
mattr_accessor :signed_id_verifier_secret, instance_writer: false
13+
class_attribute :signed_id_verifier_secret, instance_writer: false
1414
end
1515

1616
module ClassMethods

activerecord/test/cases/autosave_association_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ def test_errors_should_be_indexed_when_passed_as_array
512512
end
513513

514514
def test_errors_should_be_indexed_when_global_flag_is_set
515-
old_attribute_config = ActiveRecord::Base.index_nested_attribute_errors
516-
ActiveRecord::Base.index_nested_attribute_errors = true
515+
old_attribute_config = ActiveRecord.index_nested_attribute_errors
516+
ActiveRecord.index_nested_attribute_errors = true
517517

518518
molecule = Molecule.new
519519
valid_electron = Electron.new(name: "electron")
@@ -527,7 +527,7 @@ def test_errors_should_be_indexed_when_global_flag_is_set
527527
assert_equal ["can't be blank"], molecule.errors["electrons[1].name"]
528528
assert_not_equal ["can't be blank"], molecule.errors["electrons.name"]
529529
ensure
530-
ActiveRecord::Base.index_nested_attribute_errors = old_attribute_config
530+
ActiveRecord.index_nested_attribute_errors = old_attribute_config
531531
end
532532

533533
def test_errors_details_should_be_set
@@ -559,8 +559,8 @@ def test_errors_details_should_be_indexed_when_passed_as_array
559559
end
560560

561561
def test_errors_details_should_be_indexed_when_global_flag_is_set
562-
old_attribute_config = ActiveRecord::Base.index_nested_attribute_errors
563-
ActiveRecord::Base.index_nested_attribute_errors = true
562+
old_attribute_config = ActiveRecord.index_nested_attribute_errors
563+
ActiveRecord.index_nested_attribute_errors = true
564564

565565
molecule = Molecule.new
566566
valid_electron = Electron.new(name: "electron")
@@ -574,7 +574,7 @@ def test_errors_details_should_be_indexed_when_global_flag_is_set
574574
assert_equal [{ error: :blank }], molecule.errors.details[:"electrons[1].name"]
575575
assert_equal [], molecule.errors.details[:"electrons.name"]
576576
ensure
577-
ActiveRecord::Base.index_nested_attribute_errors = old_attribute_config
577+
ActiveRecord.index_nested_attribute_errors = old_attribute_config
578578
end
579579

580580
def test_valid_adding_with_nested_attributes

0 commit comments

Comments
 (0)