Skip to content

Commit

Permalink
Merge pull request #6807 from koic/fix_incorrect_autocorrect_for_rail…
Browse files Browse the repository at this point in the history
…s_validation

[Fix #6800] Fix an incorrect auto-correct for `Rails/Validation`
  • Loading branch information
Drenmi committed Mar 7, 2019
2 parents 41896d0 + 171e557 commit 30919f1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [#6803](https://github.com/rubocop-hq/rubocop/pull/6803): Fix error for `Style/NumericLiterals` on a literal that contains spaces. ([@pocke][])
* [#6801](https://github.com/rubocop-hq/rubocop/pull/6801): Fix auto-correction for `Style/Lambda` with no-space argument. ([@pocke][])
* [#6804](https://github.com/rubocop-hq/rubocop/pull/6804): Fix auto-correction of `Style/NumericLiterals` on numeric literal with exponent. ([@pocke][])
* [#6800](https://github.com/rubocop-hq/rubocop/issues/6800): Fix an incorrect auto-correct for `Rails/Validation` when method arguments are enclosed in parentheses. ([@koic][])

### Changes

Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/rails/validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ def correct_validate_type(corrector, node)
"#{validate_type}: #{braced_options(last_argument)}"
)
else
corrector.insert_after(node.loc.expression,
", #{validate_type}: true")
range = last_argument.source_range

corrector.insert_after(range, ", #{validate_type}: true")
end
end

Expand Down
13 changes: 13 additions & 0 deletions spec/rubocop/cop/rails/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@

include_examples 'auto-corrects'
end

context "with validates_#{type}_of " \
'when method arguments are enclosed in parentheses' do
let(:auto_corrected_source) do
"validates(:full_name, :birth_date, #{type}: true)"
end

let(:source) do
"validates_#{type}_of(:full_name, :birth_date)"
end

include_examples 'auto-corrects'
end
end

context 'with single attribute name' do
Expand Down

0 comments on commit 30919f1

Please sign in to comment.