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:
Kasper Timm Hansen 2020-01-09 11:14:19 +01:00 committed by GitHub
commit 3eab31c487
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -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

View File

@ -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