From e4821b055522470cf80c44a23e73868c6776d819 Mon Sep 17 00:00:00 2001
From: Shubham Kumar <shubhamkrai123@gmail.com>
Date: Thu, 1 Apr 2021 20:15:42 +0000
Subject: [PATCH] Resolves rubocop offense Rails/WhereNot

Fixes auto correct rubocop offenses
---
 .rubocop_todo.yml                                            | 5 -----
 app/finders/ci/pipelines_finder.rb                           | 2 +-
 app/models/ci/build.rb                                       | 3 +--
 app/models/ci/stage.rb                                       | 2 +-
 app/models/clusters/clusters_hierarchy.rb                    | 2 +-
 app/models/environment.rb                                    | 2 +-
 app/models/project.rb                                        | 2 +-
 app/models/user.rb                                           | 2 +-
 app/services/todos/destroy/base_service.rb                   | 2 +-
 app/services/todos/destroy/private_features_service.rb       | 2 +-
 changelogs/unreleased/pl-rubocop-todo-where-not.yml          | 5 +++++
 ...06102120_backfill_deployment_clusters_from_deployments.rb | 2 +-
 ...00811130433_create_missing_vulnerabilities_issue_links.rb | 4 ++--
 ee/app/models/elastic/reindexing_task.rb                     | 2 +-
 ee/lib/ee/gitlab/usage_data.rb                               | 2 +-
 .../migrations/nullify_feature_flag_plaintext_tokens_spec.rb | 2 +-
 ee/spec/models/ee/description_version_spec.rb                | 2 +-
 .../populate_merge_request_assignees_table.rb                | 2 +-
 .../wrongfully_confirmed_email_unconfirmer.rb                | 2 +-
 lib/gitlab/import_export/uploads_manager.rb                  | 2 +-
 .../cycle_analytics/stage_events/code_stage_start_spec.rb    | 2 +-
 .../clean_up_noteable_id_for_notes_on_commits_spec.rb        | 2 +-
 spec/migrations/migrate_bot_type_to_user_type_spec.rb        | 2 +-
 23 files changed, 27 insertions(+), 28 deletions(-)
 create mode 100644 changelogs/unreleased/pl-rubocop-todo-where-not.yml

diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml
index f01dedb40dd3f..f06496d4519e5 100644
--- a/.rubocop_todo.yml
+++ b/.rubocop_todo.yml
@@ -640,11 +640,6 @@ Rails/WhereEquals:
 Rails/WhereExists:
   Enabled: false
 
-# Offense count: 21
-# Cop supports --auto-correct.
-Rails/WhereNot:
-  Enabled: false
-
 # Offense count: 8
 # Cop supports --auto-correct.
 Security/YAMLLoad:
diff --git a/app/finders/ci/pipelines_finder.rb b/app/finders/ci/pipelines_finder.rb
index a77faebb160e4..e509cf940b8bf 100644
--- a/app/finders/ci/pipelines_finder.rb
+++ b/app/finders/ci/pipelines_finder.rb
@@ -131,7 +131,7 @@ def by_username(items)
     def by_yaml_errors(items)
       case Gitlab::Utils.to_boolean(params[:yaml_errors])
       when true
-        items.where("yaml_errors IS NOT NULL")
+        items.where.not(yaml_errors: nil)
       when false
         items.where("yaml_errors IS NULL")
       else
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb
index dce9168a9a415..1f7f5f6babe10 100644
--- a/app/models/ci/build.rb
+++ b/app/models/ci/build.rb
@@ -93,8 +93,7 @@ def persisted_environment
     validates :ref, presence: true
 
     scope :not_interruptible, -> do
-      joins(:metadata).where('ci_builds_metadata.id NOT IN (?)',
-        Ci::BuildMetadata.scoped_build.with_interruptible.select(:id))
+      joins(:metadata).where.not('ci_builds_metadata.id' => Ci::BuildMetadata.scoped_build.with_interruptible.select(:id))
     end
 
     scope :unstarted, -> { where(runner_id: nil) }
diff --git a/app/models/ci/stage.rb b/app/models/ci/stage.rb
index fe6889a680827..9dd75150ac76d 100644
--- a/app/models/ci/stage.rb
+++ b/app/models/ci/stage.rb
@@ -44,7 +44,7 @@ class Stage < ApplicationRecord
       next if position.present?
 
       self.position = statuses.select(:stage_idx)
-        .where('stage_idx IS NOT NULL')
+        .where.not(stage_idx: nil)
         .group(:stage_idx)
         .order('COUNT(*) DESC')
         .first&.stage_idx.to_i
diff --git a/app/models/clusters/clusters_hierarchy.rb b/app/models/clusters/clusters_hierarchy.rb
index c9c18d8c96a62..125783e6ee15d 100644
--- a/app/models/clusters/clusters_hierarchy.rb
+++ b/app/models/clusters/clusters_hierarchy.rb
@@ -16,7 +16,7 @@ def base_and_ancestors
 
       model
         .unscoped
-        .where('clusters.id IS NOT NULL')
+        .where.not('clusters.id' => nil)
         .with
         .recursive(cte.to_arel)
         .from(cte_alias)
diff --git a/app/models/environment.rb b/app/models/environment.rb
index 96b44c9ac0a3a..4cc65f4e29596 100644
--- a/app/models/environment.rb
+++ b/app/models/environment.rb
@@ -86,7 +86,7 @@ class Environment < ApplicationRecord
   end
 
   scope :for_project, -> (project) { where(project_id: project) }
-  scope :for_tier, -> (tier) { where(tier: tier).where('tier IS NOT NULL') }
+  scope :for_tier, -> (tier) { where(tier: tier).where.not(tier: nil) }
   scope :with_deployment, -> (sha) { where('EXISTS (?)', Deployment.select(1).where('deployments.environment_id = environments.id').where(sha: sha)) }
   scope :unfoldered, -> { where(environment_type: nil) }
   scope :with_rank, -> do
diff --git a/app/models/project.rb b/app/models/project.rb
index eb0382414de50..2431b40a8be3b 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -519,7 +519,7 @@ class Project < ApplicationRecord
   scope :with_packages, -> { joins(:packages) }
   scope :in_namespace, ->(namespace_ids) { where(namespace_id: namespace_ids) }
   scope :personal, ->(user) { where(namespace_id: user.namespace_id) }
-  scope :joined, ->(user) { where('namespace_id != ?', user.namespace_id) }
+  scope :joined, ->(user) { where.not(namespace_id: user.namespace_id) }
   scope :starred_by, ->(user) { joins(:users_star_projects).where('users_star_projects.user_id': user.id) }
   scope :visible_to_user, ->(user) { where(id: user.authorized_projects.select(:id).reorder(nil)) }
   scope :visible_to_user_and_access_level, ->(user, access_level) { where(id: user.authorized_projects.where('project_authorizations.access_level >= ?', access_level).select(:id).reorder(nil)) }
diff --git a/app/models/user.rb b/app/models/user.rb
index decd1117c6c02..2cffbf3788286 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1041,7 +1041,7 @@ def owned_projects
       [
         Project.where(namespace: namespace),
         Project.joins(:project_authorizations)
-          .where("projects.namespace_id <> ?", namespace.id)
+          .where.not('projects.namespace_id' => namespace.id)
           .where(project_authorizations: { user_id: id, access_level: Gitlab::Access::OWNER })
       ],
       remove_duplicates: false
diff --git a/app/services/todos/destroy/base_service.rb b/app/services/todos/destroy/base_service.rb
index 7378f10e7c425..4e971246185a2 100644
--- a/app/services/todos/destroy/base_service.rb
+++ b/app/services/todos/destroy/base_service.rb
@@ -13,7 +13,7 @@ def execute
 
       # rubocop: disable CodeReuse/ActiveRecord
       def without_authorized(items)
-        items.where('todos.user_id NOT IN (?)', authorized_users)
+        items.where.not('todos.user_id' => authorized_users)
       end
       # rubocop: enable CodeReuse/ActiveRecord
 
diff --git a/app/services/todos/destroy/private_features_service.rb b/app/services/todos/destroy/private_features_service.rb
index bd49519d694db..44c3ff231f86f 100644
--- a/app/services/todos/destroy/private_features_service.rb
+++ b/app/services/todos/destroy/private_features_service.rb
@@ -36,7 +36,7 @@ def remove_todos(project_id, target_types)
         items = Todo.where(project_id: project_id)
         items = items.where(user_id: user_id) if user_id
 
-        items.where('user_id NOT IN (?)', authorized_users)
+        items.where.not(user_id: authorized_users)
           .where(target_type: target_types)
           .delete_all
       end
diff --git a/changelogs/unreleased/pl-rubocop-todo-where-not.yml b/changelogs/unreleased/pl-rubocop-todo-where-not.yml
new file mode 100644
index 0000000000000..da8c041ec101d
--- /dev/null
+++ b/changelogs/unreleased/pl-rubocop-todo-where-not.yml
@@ -0,0 +1,5 @@
+---
+title: Resolves rubocop offenses Rails/WhereNot
+merge_request: 58062
+author: Shubham Kumar (@imskr)
+type: fixed
diff --git a/db/post_migrate/20200406102120_backfill_deployment_clusters_from_deployments.rb b/db/post_migrate/20200406102120_backfill_deployment_clusters_from_deployments.rb
index 76b00796d1ae7..ab217ba92abd2 100644
--- a/db/post_migrate/20200406102120_backfill_deployment_clusters_from_deployments.rb
+++ b/db/post_migrate/20200406102120_backfill_deployment_clusters_from_deployments.rb
@@ -17,7 +17,7 @@ class BackfillDeploymentClustersFromDeployments < ActiveRecord::Migration[6.0]
   class Deployment < ActiveRecord::Base
     include EachBatch
 
-    default_scope { where('cluster_id IS NOT NULL') } # rubocop:disable Cop/DefaultScope
+    default_scope { where.not(cluster_id: nil) } # rubocop:disable Cop/DefaultScope
 
     self.table_name = 'deployments'
   end
diff --git a/db/post_migrate/20200811130433_create_missing_vulnerabilities_issue_links.rb b/db/post_migrate/20200811130433_create_missing_vulnerabilities_issue_links.rb
index 891201eaa52af..031d9ea49e2ea 100644
--- a/db/post_migrate/20200811130433_create_missing_vulnerabilities_issue_links.rb
+++ b/db/post_migrate/20200811130433_create_missing_vulnerabilities_issue_links.rb
@@ -18,11 +18,11 @@ class VulnerabilitiesIssueLink < ActiveRecord::Base
   disable_ddl_transaction!
 
   def up
-    VulnerabilitiesFeedback.where('issue_id IS NOT NULL').each_batch do |relation|
+    VulnerabilitiesFeedback.where.not(issue_id: nil).each_batch do |relation|
       timestamp = Time.now
       issue_links = relation
         .joins("JOIN vulnerability_occurrences vo ON vo.project_id = vulnerability_feedback.project_id AND vo.report_type = vulnerability_feedback.category AND encode(vo.project_fingerprint, 'hex') = vulnerability_feedback.project_fingerprint")
-        .where('vo.vulnerability_id IS NOT NULL')
+        .where.not('vo.vulnerability_id' => nil)
         .pluck(:vulnerability_id, :issue_id)
         .map do |v_id, i_id|
           {
diff --git a/ee/app/models/elastic/reindexing_task.rb b/ee/app/models/elastic/reindexing_task.rb
index c5d715b118632..17efca14f9b21 100644
--- a/ee/app/models/elastic/reindexing_task.rb
+++ b/ee/app/models/elastic/reindexing_task.rb
@@ -18,7 +18,7 @@ class Elastic::ReindexingTask < ApplicationRecord
     original_index_deleted: 12
   }
 
-  scope :old_indices_scheduled_for_deletion, -> { where(state: :success).where('delete_original_index_at IS NOT NULL') }
+  scope :old_indices_scheduled_for_deletion, -> { where(state: :success).where.not(delete_original_index_at: nil) }
   scope :old_indices_to_be_deleted, -> { old_indices_scheduled_for_deletion.where('delete_original_index_at < NOW()') }
 
   before_save :set_in_progress_flag
diff --git a/ee/lib/ee/gitlab/usage_data.rb b/ee/lib/ee/gitlab/usage_data.rb
index 617ad0ea5c524..3cc65f4163f02 100644
--- a/ee/lib/ee/gitlab/usage_data.rb
+++ b/ee/lib/ee/gitlab/usage_data.rb
@@ -610,7 +610,7 @@ def projects_with_sectional_code_owner_rules(time_period)
             ::ApprovalMergeRequestRule
               .code_owner
               .joins(:merge_request)
-              .where("section != ?", ::Gitlab::CodeOwners::Entry::DEFAULT_SECTION)
+              .where.not(section: ::Gitlab::CodeOwners::Entry::DEFAULT_SECTION)
               .where(time_period),
             'merge_requests.target_project_id',
             start: project_minimum_id,
diff --git a/ee/spec/migrations/nullify_feature_flag_plaintext_tokens_spec.rb b/ee/spec/migrations/nullify_feature_flag_plaintext_tokens_spec.rb
index 827a9d893dbac..4e816edc23dc6 100644
--- a/ee/spec/migrations/nullify_feature_flag_plaintext_tokens_spec.rb
+++ b/ee/spec/migrations/nullify_feature_flag_plaintext_tokens_spec.rb
@@ -35,7 +35,7 @@
       }
 
       migration.after -> {
-        expect(feature_flags_clients.where('token IS NOT NULL').count).to eq(0)
+        expect(feature_flags_clients.where.not(token: nil).count).to eq(0)
 
         feature_flag1.reload
         expect(feature_flag1.token).to be_nil
diff --git a/ee/spec/models/ee/description_version_spec.rb b/ee/spec/models/ee/description_version_spec.rb
index 8cabbcfdac692..e9a09f96fde03 100644
--- a/ee/spec/models/ee/description_version_spec.rb
+++ b/ee/spec/models/ee/description_version_spec.rb
@@ -49,7 +49,7 @@
     def deleted_count
       DescriptionVersion
         .where('issue_id = ? or epic_id = ? or merge_request_id = ?', issue.id, epic.id, merge_request.id)
-        .where('deleted_at IS NOT NULL')
+        .where.not(deleted_at: nil)
         .count
     end
 
diff --git a/lib/gitlab/background_migration/populate_merge_request_assignees_table.rb b/lib/gitlab/background_migration/populate_merge_request_assignees_table.rb
index eb4bc0aaf28ce..28cc4a5e3fa9b 100644
--- a/lib/gitlab/background_migration/populate_merge_request_assignees_table.rb
+++ b/lib/gitlab/background_migration/populate_merge_request_assignees_table.rb
@@ -11,7 +11,7 @@ def perform(from_id, to_id)
           MergeRequest
             .where(merge_request_assignees_not_exists_clause)
             .where(id: from_id..to_id)
-            .where('assignee_id IS NOT NULL')
+            .where.not(assignee_id: nil)
             .select(:id, :assignee_id)
             .to_sql
 
diff --git a/lib/gitlab/background_migration/wrongfully_confirmed_email_unconfirmer.rb b/lib/gitlab/background_migration/wrongfully_confirmed_email_unconfirmer.rb
index 5b01141d8c196..665ad7abcbb07 100644
--- a/lib/gitlab/background_migration/wrongfully_confirmed_email_unconfirmer.rb
+++ b/lib/gitlab/background_migration/wrongfully_confirmed_email_unconfirmer.rb
@@ -27,7 +27,7 @@ def self.wrongfully_confirmed_emails(start_id, stop_id)
           joins(:user)
             .merge(UserModel.active)
             .where(id: (start_id..stop_id))
-            .where('emails.confirmed_at IS NOT NULL')
+            .where.not('emails.confirmed_at' => nil)
             .where('emails.confirmed_at = users.confirmed_at')
             .where('emails.email <> users.email')
             .where('NOT EXISTS (SELECT 1 FROM user_synced_attributes_metadata WHERE user_id=users.id AND email_synced IS true)')
diff --git a/lib/gitlab/import_export/uploads_manager.rb b/lib/gitlab/import_export/uploads_manager.rb
index 428bcbe8dc574..2f15cdd750624 100644
--- a/lib/gitlab/import_export/uploads_manager.rb
+++ b/lib/gitlab/import_export/uploads_manager.rb
@@ -76,7 +76,7 @@ def each_uploader
       def project_uploads_except_avatar(avatar_path)
         return @project.uploads unless avatar_path
 
-        @project.uploads.where("path != ?", avatar_path)
+        @project.uploads.where.not(path: avatar_path)
       end
 
       def download_and_copy(upload)
diff --git a/spec/lib/gitlab/analytics/cycle_analytics/stage_events/code_stage_start_spec.rb b/spec/lib/gitlab/analytics/cycle_analytics/stage_events/code_stage_start_spec.rb
index 52e9f2d98467a..b6f9c8106c9a2 100644
--- a/spec/lib/gitlab/analytics/cycle_analytics/stage_events/code_stage_start_spec.rb
+++ b/spec/lib/gitlab/analytics/cycle_analytics/stage_events/code_stage_start_spec.rb
@@ -15,7 +15,7 @@
 
     other_merge_request = create(:merge_request, source_project: project, source_branch: 'a', target_branch: 'master')
 
-    records = subject.apply_query_customization(MergeRequest.all).where('merge_requests_closing_issues.issue_id IS NOT NULL')
+    records = subject.apply_query_customization(MergeRequest.all).where.not('merge_requests_closing_issues.issue_id' => nil)
     expect(records).to eq([merge_request])
     expect(records).not_to include(other_merge_request)
   end
diff --git a/spec/migrations/clean_up_noteable_id_for_notes_on_commits_spec.rb b/spec/migrations/clean_up_noteable_id_for_notes_on_commits_spec.rb
index 531c1dbb76aae..268fadee0afc5 100644
--- a/spec/migrations/clean_up_noteable_id_for_notes_on_commits_spec.rb
+++ b/spec/migrations/clean_up_noteable_id_for_notes_on_commits_spec.rb
@@ -25,7 +25,7 @@
   end
 
   def dirty_notes_on_commits
-    notes.where(noteable_type: 'Commit').where('noteable_id IS NOT NULL')
+    notes.where(noteable_type: 'Commit').where.not(noteable_id: nil)
   end
 
   def other_notes
diff --git a/spec/migrations/migrate_bot_type_to_user_type_spec.rb b/spec/migrations/migrate_bot_type_to_user_type_spec.rb
index 2b85f2a7f691a..fcd7f1ebcb83c 100644
--- a/spec/migrations/migrate_bot_type_to_user_type_spec.rb
+++ b/spec/migrations/migrate_bot_type_to_user_type_spec.rb
@@ -15,6 +15,6 @@
 
     migrate!
 
-    expect(users.where('user_type IS NOT NULL').map(&:user_type)).to match_array([1, 2, 3])
+    expect(users.where.not(user_type: nil).map(&:user_type)).to match_array([1, 2, 3])
   end
 end
-- 
GitLab