Skip to content

Commit 7269eb8

Browse files
committed
[Fix #1488] Fix an error for Rails/ReadWriteAttribute cop
Resolves #1488 Basically we should not assume AST literals are not frozen.
1 parent 1c1b567 commit 7269eb8

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1488](https://github.com/rubocop/rubocop-rails/issues/1488): Fix an error for `Rails/ReadWriteAttribute` with a frozen string attribute name. ([@viralpraxis][])

lib/rubocop/cop/rails/read_write_attribute.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def within_shadowing_method?(node)
6666
return false unless enclosing_method
6767

6868
shadowing_method_name = first_arg.value.to_s
69-
shadowing_method_name << '=' if node.method?(:write_attribute)
69+
shadowing_method_name += '=' if node.method?(:write_attribute)
7070
enclosing_method.method?(shadowing_method_name)
7171
end
7272

spec/rubocop/cop/rails/read_write_attribute_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,21 @@ def foo=(value)
235235
RUBY
236236
end
237237

238+
it 'corrects assignment with chained methods when using string attribute name' do
239+
expect_offense(<<~RUBY)
240+
def attr2=(k)
241+
write_attribute("attr", 'test_' + postfix)
242+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer `self["attr"] = 'test_' + postfix`.
243+
end
244+
RUBY
245+
246+
expect_correction(<<~RUBY)
247+
def attr2=(k)
248+
self["attr"] = 'test_' + postfix
249+
end
250+
RUBY
251+
end
252+
238253
it 'autocorrects multiline' do
239254
expect_offense(<<~RUBY)
240255
write_attribute(

0 commit comments

Comments
 (0)