Skip to content

Commit 32e549a

Browse files
authored
Merge pull request rails#52720 from rails/revert-52717-class-attribute-redefine-once
Revert "class_attribute: reduce reliance on define_method"
2 parents a3b0391 + 3676839 commit 32e549a

File tree

6 files changed

+23
-63
lines changed

6 files changed

+23
-63
lines changed

activerecord/test/cases/test_case.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ def with_has_many_inversing(model = ActiveRecord::Base)
141141
yield
142142
ensure
143143
model.has_many_inversing = old
144+
if model != ActiveRecord::Base && !old
145+
model.singleton_class.remove_method(:has_many_inversing) # reset the class_attribute
146+
end
144147
end
145148

146149
def with_automatic_scope_inversing(*reflections)

activesupport/lib/active_support.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ module ActiveSupport
6262
autoload :Cache
6363
autoload :Callbacks
6464
autoload :Configurable
65-
autoload :ClassAttribute
6665
autoload :Deprecation
6766
autoload :Delegation
6867
autoload :Digest

activesupport/lib/active_support/callbacks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module Callbacks
6666

6767
included do
6868
extend ActiveSupport::DescendantsTracker
69-
class_attribute :__callbacks, instance_writer: false, instance_predicate: false, default: {}
69+
class_attribute :__callbacks, instance_writer: false, default: {}
7070
end
7171

7272
CALLBACK_FILTER_TYPES = [:before, :after, :around].freeze

activesupport/lib/active_support/class_attribute.rb

Lines changed: 0 additions & 23 deletions
This file was deleted.

activesupport/lib/active_support/core_ext/class/attribute.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,24 @@ def class_attribute(*attrs, instance_accessor: true,
9191
raise TypeError, "#{name.inspect} is not a symbol nor a string"
9292
end
9393

94-
name = name.to_sym
95-
::ActiveSupport::ClassAttribute.redefine(self, name, default)
94+
class_methods << <<~RUBY # In case the method exists and is not public
95+
silence_redefinition_of_method def #{name}
96+
end
97+
RUBY
9698

97-
unless singleton_class?
98-
methods << <<~RUBY if instance_reader
99-
silence_redefinition_of_method def #{name}
100-
defined?(@#{name}) ? @#{name} : self.class.#{name}
101-
end
102-
RUBY
103-
end
99+
methods << <<~RUBY if instance_reader
100+
silence_redefinition_of_method def #{name}
101+
defined?(@#{name}) ? @#{name} : self.class.#{name}
102+
end
103+
RUBY
104+
105+
class_methods << <<~RUBY
106+
silence_redefinition_of_method def #{name}=(value)
107+
redefine_method(:#{name}) { value } if singleton_class?
108+
redefine_singleton_method(:#{name}) { value }
109+
value
110+
end
111+
RUBY
104112

105113
methods << <<~RUBY if instance_writer
106114
silence_redefinition_of_method(:#{name}=)
@@ -117,5 +125,7 @@ def class_attribute(*attrs, instance_accessor: true,
117125

118126
location = caller_locations(1, 1).first
119127
class_eval(["class << self", *class_methods, "end", *methods].join(";").tr("\n", ";"), location.path, location.lineno)
128+
129+
attrs.each { |name| public_send("#{name}=", default) }
120130
end
121131
end

activesupport/test/core_ext/class/attribute_test.rb

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,11 @@ def setup
3030
test "overridable" do
3131
@sub.setting = 1
3232
assert_nil @klass.setting
33-
assert_equal 1, @sub.setting
3433

3534
@klass.setting = 2
36-
assert_equal 2, @klass.setting
3735
assert_equal 1, @sub.setting
3836

3937
assert_equal 1, Class.new(@sub).setting
40-
assert_equal 2, Class.new(@klass).setting
4138
end
4239

4340
test "predicate method" do
@@ -100,34 +97,8 @@ def setup
10097

10198
test "works well with singleton classes" do
10299
object = @klass.new
103-
104100
object.singleton_class.setting = "foo"
105101
assert_equal "foo", object.setting
106-
assert_nil @klass.setting
107-
108-
object.singleton_class.setting = "bar"
109-
assert_equal "bar", object.setting
110-
assert_nil @klass.setting
111-
112-
@klass.setting = "plop"
113-
assert_equal "bar", object.setting
114-
assert_equal "plop", @klass.setting
115-
end
116-
117-
test "when defined in a class's singleton" do
118-
@klass = Class.new do
119-
class << self
120-
class_attribute :__callbacks, default: 1
121-
end
122-
end
123-
124-
assert_equal 1, @klass.__callbacks
125-
assert_equal 1, @klass.singleton_class.__callbacks
126-
127-
# I honestly think this is a bug, but that's how it used to behave
128-
@klass.__callbacks = 4
129-
assert_equal 1, @klass.__callbacks
130-
assert_equal 1, @klass.singleton_class.__callbacks
131102
end
132103

133104
test "works well with module singleton classes" do

0 commit comments

Comments
 (0)