Commit 6ad4f10a authored by Siddharth Asthana's avatar Siddharth Asthana Committed by Peter Leitzen

Resolve Rails/IndexBy rubocop offenses

Exclude Rails/IndexBy from `tooling/danger` as it's not available.

Changelog: other
EE: true
parent fa5af694
......@@ -239,6 +239,10 @@ Rails/FindBy:
- 'spec/**/*.rb'
- 'ee/spec/**/*.rb'
Rails/IndexBy:
Exclude:
- 'tooling/danger/**/*.rb'
# This is currently exiting with a rubocop exception error and should be
# resolved hopefully a future update
# An error occurred while Rails/UniqueValidationWithoutIndex cop was inspecting
......
......@@ -458,24 +458,6 @@ Rails/HasManyOrHasOneDependent:
Rails/HelperInstanceVariable:
Enabled: false
# Offense count: 17
# Cop supports --auto-correct.
Rails/IndexBy:
Exclude:
- 'app/graphql/types/design_management/design_fields.rb'
- 'app/models/ci/pipeline.rb'
- 'app/services/ci/pipeline_processing/atomic_processing_service/status_collection.rb'
- 'ee/app/services/projects/update_mirror_service.rb'
- 'ee/app/services/security/store_report_service.rb'
- 'ee/lib/ee/gitlab/auth/ldap/sync/group.rb'
- 'ee/lib/ee/gitlab/background_migration/backfill_version_data_from_gitaly.rb'
- 'ee/lib/gitlab/analytics/type_of_work/tasks_by_type.rb'
- 'ee/lib/gitlab/elastic/document_reference.rb'
- 'ee/lib/gitlab/group_plans_preloader.rb'
- 'lib/gitlab/database/count/reltuples_count_strategy.rb'
- 'lib/gitlab/language_detection.rb'
- 'tooling/danger/sidekiq_queues.rb'
# Offense count: 47
# Cop supports --auto-correct.
Rails/IndexWith:
......
......@@ -62,7 +62,7 @@ module Types
def cached_actions_for_version(version)
Gitlab::SafeRequestStore.fetch(['DesignFields', 'actions_for_version', version.id]) do
version.actions.to_h { |dv| [dv.design_id, dv] }
version.actions.index_by(&:design_id)
end
end
......
......@@ -421,9 +421,7 @@ module Ci
sql = sql.where(ref: ref) if ref
sql.each_with_object({}) do |pipeline, hash|
hash[pipeline.sha] = pipeline
end
sql.index_by(&:sha)
end
def self.latest_successful_ids_per_project
......
......@@ -91,17 +91,13 @@ module Ci
def all_statuses_by_id
strong_memoize(:all_statuses_by_id) do
all_statuses.to_h do |row|
[row[:id], row]
end
all_statuses.index_by { |row| row[:id] }
end
end
def all_statuses_by_name
strong_memoize(:statuses_by_name) do
all_statuses.to_h do |row|
[row[:name], row]
end
all_statuses.index_by { |row| row[:name] }
end
end
......
......@@ -61,7 +61,7 @@ module Projects
end
def update_branches
local_branches = repository.branches.each_with_object({}) { |branch, branches| branches[branch.name] = branch }
local_branches = repository.branches.index_by(&:name)
errors = []
......@@ -96,7 +96,7 @@ module Projects
end
def update_tags(&block)
old_tags = repository_tags_with_target.each_with_object({}) { |tag, tags| tags[tag.name] = tag }
old_tags = repository_tags_with_target.index_by(&:name)
fetch_result = yield
return fetch_result unless fetch_result&.tags_changed
......
......@@ -293,7 +293,7 @@ module EE
# returns a hash user_id -> LDAP identity in current LDAP provider
def resolve_ldap_identities(for_users:)
::Identity.for_user(for_users).with_provider(provider)
.to_h { |identity| [identity.user_id, identity] }
.index_by(&:user_id)
end
# returns a hash of normalized DN -> user for the current LDAP provider
......
......@@ -96,7 +96,7 @@ module Gitlab
end
def labels_by_id
@labels_by_id = labels.each_with_object({}) { |label, hash| hash[label.id] = label }
@labels_by_id = labels.index_by(&:id)
end
end
end
......
......@@ -32,7 +32,7 @@ module Gitlab
ids = group.map(&:db_id)
records = klass.id_in(ids).preload_indexing_data
records_by_id = records.each_with_object({}) { |record, hash| hash[record.id] = record }
records_by_id = records.index_by(&:id)
group.each do |ref|
ref.database_record = records_by_id[ref.db_id.to_i]
......
......@@ -19,9 +19,7 @@ module Gitlab
activerecord_relation(groups)
)
# A Hash mapping group IDs to their corresponding Group instances.
groups_map = groups_and_ancestors.each_with_object({}) do |group, hash|
hash[group.id] = group
end
groups_map = groups_and_ancestors.index_by(&:id)
all_plan_ids = Set.new
......@@ -45,9 +43,7 @@ module Gitlab
# Grab all the plans for all the Groups, using only a single query.
plans = Plan
.where(id: all_plan_ids.to_a)
.each_with_object({}) do |plan, hash|
hash[plan.id] = plan
end
.index_by(&:id)
# Assign all the plans to the groups that have access to them.
groups.each do |group|
......
......@@ -46,7 +46,7 @@ module Gitlab
end
def table_to_model_mapping
@table_to_model_mapping ||= models.each_with_object({}) { |model, h| h[model.table_name] = model }
@table_to_model_mapping ||= models.index_by(&:table_name)
end
def table_to_model(table_name)
......
......@@ -63,7 +63,7 @@ module Gitlab
@repository
.languages
.first(MAX_LANGUAGES)
.to_h { |l| [l[:label], l] }
.index_by { |l| l[:label] }
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment