diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index f01dedb40dd3fe72813aa82252d99ea78816e26a..f06496d4519e5da28804608c2fa1e7db05b52c88 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 a77faebb160e447308484b235d2405155b3a12a4..e509cf940b8bf3538fbae5dd0d1241a35cf9d4f1 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 dce9168a9a41553b3b9e5ee8e81666ad0c21f752..1f7f5f6babe10e9d1d9db9b54198ddc3fc3babfd 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 fe6889a680827cdaebd1230e72c6c29185e84fa0..9dd75150ac76d185dd29fb6facfe8d2686805699 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 c9c18d8c96a625568782e76faaa461a6993c5dff..125783e6ee15d19ba15acde7afefc9956765062d 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 96b44c9ac0a3af58218bd3511317a8a7985de20c..4cc65f4e2959656a3b07ef21a1fb62d2be3ae44a 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 eb0382414de50d57ec0fa2d88574bd5adbf1df2d..2431b40a8be3b771d0a6860f88e2f1c4c953f1aa 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 decd1117c6c0208366eeda9d087a249b84e940b8..2cffbf3788286614fbc676d95b31d7ddafe4cc54 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 7378f10e7c4256f03374d289990bd77b106f479a..4e971246185a2e15eaabaf65f1665ab81e44c7d6 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 bd49519d694db7dfc08ec0dcee789a1655036a88..44c3ff231f86fe4a57c883d344bbe874053afac9 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 0000000000000000000000000000000000000000..da8c041ec101d4b60460d873de01fbe25d8a8afb --- /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 76b00796d1ae74f44845a88f42aca7404601b645..ab217ba92abd2b4408336f881acc280d7420b3de 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 891201eaa52af6cca4d63da09c2fef26a692aa13..031d9ea49e2eaf19fc70095504abb737b1d384c5 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 c5d715b118632065278ff5fd81976d9cb5222346..17efca14f9b21e9bcdfdaa9e0e211799bde0fa0b 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 617ad0ea5c52414f4adaa997985b9faab3af3179..3cc65f4163f02ff9f035e4056e07c6257a655e4a 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 827a9d893dbacfd4b73898a5aa0af7a9ba055455..4e816edc23dc636d3ef471bee012ba0bc7af7543 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 8cabbcfdac6926929d82560d965d490448c78561..e9a09f96fde03272f1e4131342aa925af9f667b3 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 eb4bc0aaf28ced7ccb3acd53fce77ca95dc9dd39..28cc4a5e3fa9bc24444535f06b1004c02076a3b6 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 5b01141d8c196d4d86dbc5afc704e9e119a8bdd8..665ad7abcbb07f583bb8feaa5c2d1d843d9dce5e 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 428bcbe8dc574fc12e5372720b95b28d2ccf3341..2f15cdd750624278e1c9850996244d39e4c68fca 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 52e9f2d98467ac244b543d7041ad6b4e9428577b..b6f9c8106c9a2a48020a6630d398bf614bc0b77d 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 531c1dbb76aae2a204d1a956f6e224f45a9e3948..268fadee0afc50b1094be09b45823aa37a987274 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 2b85f2a7f691a06a8e72de22defae629763daec4..fcd7f1ebcb83cfd99d0290ba7b1ded21570a9528 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