From 449162b0daf9439d6eecee24636eccd52b38b024 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 21 Sep 2022 01:39:33 +0900 Subject: [PATCH] [Fix #778] Fix a false positive for `Rails/DynamicFindBy` Fixes #778. This PR fixes a false positive for `Rails/DynamicFindBy` when using `page.find_by_id` as a Capybara testing API. This default setting emphasizes suppressing false positive over false negative. --- changelog/fix_false_positive_for_rails_dynamic_find_by.md | 1 + config/default.yml | 1 + lib/rubocop/cop/rails/dynamic_find_by.rb | 4 +++- 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 changelog/fix_false_positive_for_rails_dynamic_find_by.md diff --git a/changelog/fix_false_positive_for_rails_dynamic_find_by.md b/changelog/fix_false_positive_for_rails_dynamic_find_by.md new file mode 100644 index 0000000000..39a0ab23c8 --- /dev/null +++ b/changelog/fix_false_positive_for_rails_dynamic_find_by.md @@ -0,0 +1 @@ +* [#778](https://github.com/rubocop/rubocop-rails/issues/778): Fix a false positive for `Rails/DynamicFindBy` when using `page.find_by_id` as a Capybara testing API. ([@koic][]) diff --git a/config/default.yml b/config/default.yml index 12e3761a24..d5acda0555 100644 --- a/config/default.yml +++ b/config/default.yml @@ -352,6 +352,7 @@ Rails/DynamicFindBy: - find_by_sql AllowedReceivers: - Gem::Specification + - page # Prevents a warning for `page.find_by_id`. See: https://github.com/rubocop/rubocop-rails/issues/778 Rails/EagerEvaluationLogMessage: Description: 'Checks that blocks are used for interpolated strings passed to `Rails.logger.debug`.' diff --git a/lib/rubocop/cop/rails/dynamic_find_by.rb b/lib/rubocop/cop/rails/dynamic_find_by.rb index b187817753..0bc39a2103 100644 --- a/lib/rubocop/cop/rails/dynamic_find_by.rb +++ b/lib/rubocop/cop/rails/dynamic_find_by.rb @@ -29,12 +29,14 @@ module Rails # # good # User.find_by_sql(users_sql) # - # @example AllowedReceivers: ['Gem::Specification'] (default) + # @example AllowedReceivers: ['Gem::Specification', 'page'] (default) # # bad # Specification.find_by_name('backend').gem_dir + # page.find_by_id('a_dom_id').click # # # good # Gem::Specification.find_by_name('backend').gem_dir + # page.find_by_id('a_dom_id').click class DynamicFindBy < Base include ActiveRecordHelper extend AutoCorrector