Skip to content

Commit

Permalink
[Fix #12] Allow passing boolean literals to touch
Browse files Browse the repository at this point in the history
The `Rails/SkipsModelValidations` cop currently incorrectly registers an
offense for the `touch` method from Shoulda Matchers' associations DSL:

    it { is_expected.to belong_to(:user).touch(true) }

Active Record's `touch` method accepts attribute names, so we can treat
`touch` calls that pass boolean literal arguments as false positives.
  • Loading branch information
eugeneius committed Apr 11, 2020
1 parent 7ac8eef commit 95064f6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### Bug fixes

* [#12](https://github.com/rubocop-hq/rubocop-rails/issues/12): Fix a false positive for `Rails/SkipsModelValidations` when passing a boolean literal to `touch`. ([@eugeneius][])

## 2.5.2 (2020-04-09)

### Bug fixes
Expand Down
5 changes: 4 additions & 1 deletion lib/rubocop/cop/rails/skips_model_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ class SkipsModelValidations < Cop
update_counters].freeze

def_node_matcher :good_touch?, <<~PATTERN
(send (const nil? :FileUtils) :touch ...)
{
(send (const nil? :FileUtils) :touch ...)
(send _ :touch {true false})
}
PATTERN

def on_send(node)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/rails/skips_model_validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
it 'accepts FileUtils.touch' do
expect_no_offenses("FileUtils.touch('file')")
end

it 'accepts touch with literal true' do
expect_no_offenses('belongs_to(:user).touch(true)')
end

it 'accepts touch with literal false' do
expect_no_offenses('belongs_to(:user).touch(false)')
end
end

context 'with methods that require at least an argument' do
Expand Down

0 comments on commit 95064f6

Please sign in to comment.