From 95064f6692422e57ca29b1ca4c3b171238687876 Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Sun, 12 Apr 2020 00:45:21 +0100 Subject: [PATCH] [Fix #12] Allow passing boolean literals to touch 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. --- CHANGELOG.md | 4 ++++ lib/rubocop/cop/rails/skips_model_validations.rb | 5 ++++- spec/rubocop/cop/rails/skips_model_validations_spec.rb | 8 ++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c6a74c26c..265cdd2770 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/rails/skips_model_validations.rb b/lib/rubocop/cop/rails/skips_model_validations.rb index 03ec93fe5b..d5c7b18a5a 100644 --- a/lib/rubocop/cop/rails/skips_model_validations.rb +++ b/lib/rubocop/cop/rails/skips_model_validations.rb @@ -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) diff --git a/spec/rubocop/cop/rails/skips_model_validations_spec.rb b/spec/rubocop/cop/rails/skips_model_validations_spec.rb index 171e88dcf0..9f25d5c736 100644 --- a/spec/rubocop/cop/rails/skips_model_validations_spec.rb +++ b/spec/rubocop/cop/rails/skips_model_validations_spec.rb @@ -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