Skip to content

Commit

Permalink
Merge pull request #117 from koic/fix_an_incorrect_autocorrect_for_ra…
Browse files Browse the repository at this point in the history
…ils_presence

[Fix #116] Fix an incorrect autocorrect for `Rails/Prensence`
  • Loading branch information
koic authored Sep 1, 2019
2 parents 7766c50 + 3a0998c commit ba618ca
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#118](https://github.com/rubocop-hq/rubocop-rails/issues/118): Fix an incorrect autocorrect for `Rails/Validation` when attributes are specified with array literal. ([@koic][])
* [#116](https://github.com/rubocop-hq/rubocop-rails/issues/116): Fix an incorrect autocorrect for `Rails/Presence` when `else` branch of ternary operator is not nil. ([@koic][])

## 2.3.1 (2019-08-26)

Expand Down
4 changes: 3 additions & 1 deletion lib/rubocop/cop/rails/presence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ def message(node, receiver, other)
def replacement(receiver, other)
or_source = if other&.send_type?
build_source_for_or_method(other)
else
elsif other.nil? || other.nil_type?
''
else
" || #{other.source}"
end

"#{receiver.source}.presence" + or_source
Expand Down
2 changes: 2 additions & 0 deletions spec/rubocop/cop/rails/presence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
it_behaves_like 'offense', '!a.present? ? b : a', 'a.presence || b', 1, 1
it_behaves_like 'offense', 'a.blank? ? b : a', 'a.presence || b', 1, 1
it_behaves_like 'offense', '!a.blank? ? a : b', 'a.presence || b', 1, 1
it_behaves_like 'offense', 'a.present? ? a : 1', 'a.presence || 1', 1, 1
it_behaves_like 'offense', 'a.blank? ? 1 : a', 'a.presence || 1', 1, 1

it_behaves_like 'offense',
'a(:bar).map(&:baz).present? ? a(:bar).map(&:baz) : nil',
Expand Down

0 comments on commit ba618ca

Please sign in to comment.