mirror of https://github.com/rails/rails
Merge pull request #38195 from Shopify/fix-class-attribute-regression
Do not define instance predicate if the instance reader isn't as well
This commit is contained in:
commit
3eab31c487
|
@ -120,7 +120,9 @@ class Class
|
|||
|
||||
if instance_predicate
|
||||
class_methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
|
||||
methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
|
||||
if instance_reader
|
||||
methods << "silence_redefinition_of_method def #{name}?; !!self.#{name}; end"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -68,24 +68,31 @@ class ClassAttributeTest < ActiveSupport::TestCase
|
|||
test "disabling instance writer" do
|
||||
object = Class.new { class_attribute :setting, instance_writer: false }.new
|
||||
assert_raise(NoMethodError) { object.setting = "boom" }
|
||||
assert_not_respond_to object, :setting=
|
||||
end
|
||||
|
||||
test "disabling instance reader" do
|
||||
object = Class.new { class_attribute :setting, instance_reader: false }.new
|
||||
assert_raise(NoMethodError) { object.setting }
|
||||
assert_not_respond_to object, :setting
|
||||
assert_raise(NoMethodError) { object.setting? }
|
||||
assert_not_respond_to object, :setting?
|
||||
end
|
||||
|
||||
test "disabling both instance writer and reader" do
|
||||
object = Class.new { class_attribute :setting, instance_accessor: false }.new
|
||||
assert_raise(NoMethodError) { object.setting }
|
||||
assert_not_respond_to object, :setting
|
||||
assert_raise(NoMethodError) { object.setting? }
|
||||
assert_not_respond_to object, :setting?
|
||||
assert_raise(NoMethodError) { object.setting = "boom" }
|
||||
assert_not_respond_to object, :setting=
|
||||
end
|
||||
|
||||
test "disabling instance predicate" do
|
||||
object = Class.new { class_attribute :setting, instance_predicate: false }.new
|
||||
assert_raise(NoMethodError) { object.setting? }
|
||||
assert_not_respond_to object, :setting?
|
||||
end
|
||||
|
||||
test "works well with singleton classes" do
|
||||
|
|
Loading…
Reference in New Issue