Skip to content

Commit aad26f6

Browse files
bquorningkoic
authored andcommitted
[Fix #6429] Rails/Validation with braced options
Rails/Validation autocorrect was failing to recognize options hashes that already was wrapped in braces, always adding an extra set of braces around them.
1 parent 3e80811 commit aad26f6

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/rubocop/cop/rails/validation.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,20 @@ def correct_validate_type(corrector, node)
8080

8181
if options
8282
corrector.replace(options.loc.expression,
83-
"#{validate_type}: { #{options.source} }")
83+
"#{validate_type}: #{braced_options(options)}")
8484
else
8585
corrector.insert_after(node.loc.expression,
8686
", #{validate_type}: true")
8787
end
8888
end
89+
90+
def braced_options(options)
91+
if options.braces?
92+
options.source
93+
else
94+
"{ #{options.source} }"
95+
end
96+
end
8997
end
9098
end
9199
end

spec/rubocop/cop/rails/validation_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,13 @@
3939
'validates :age, numericality: { minimum: 0, maximum: 122 }'
4040
)
4141
end
42+
43+
it 'autocorrect validates_numericality_of with options in braces' do
44+
new_source = autocorrect_source(
45+
'validates_numericality_of :age, { minimum: 0, maximum: 122 }'
46+
)
47+
expect(new_source).to eq(
48+
'validates :age, numericality: { minimum: 0, maximum: 122 }'
49+
)
50+
end
4251
end

0 commit comments

Comments
 (0)