RuboCop: Rails/IndexBy

auto-corrected

Change-Id: I7f6ca0f5a10a2f04849c5d0378641e2d3e8fc704
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/278485
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Reviewed-by: Jacob Burroughs <jburroughs@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Cody Cutrer 2021-11-16 15:39:28 -07:00
parent 1c51242f3d
commit 2bc9e0e304
13 changed files with 28 additions and 34 deletions

View File

@ -112,6 +112,8 @@ Rails/HasManyOrHasOneDependent:
Enabled: false # legacy code + most things we soft delete anyway
Rails/HelperInstanceVariable:
Enabled: false # legacy code
Rails/IndexBy:
Severity: error
Rails/IndexWith:
Severity: error
Rails/SkipsModelValidations:

View File

@ -245,7 +245,7 @@ module Lti
end
account_ids = @context.account_chain.map(&:id)
bindings = ToolProxyBinding.where(context_type: 'Account', context_id: account_ids, tool_proxy_id: tool_proxy.id)
binding_lookup = bindings.each_with_object({}) { |binding, hash| hash[binding.context_id] = binding }
binding_lookup = bindings.index_by(&:context_id)
sorted_bindings = account_ids.map { |account_id| binding_lookup[account_id] }
sorted_bindings.first
end

View File

@ -641,7 +641,7 @@ class Quizzes::QuizzesController < ApplicationController
@submissions_from_users = @quiz.quiz_submissions.for_user_ids(students.map(&:id)).not_settings_only.to_a
@submissions_from_users = Hash[@submissions_from_users.map { |s| [s.user_id, s] }]
@submissions_from_users = @submissions_from_users.index_by(&:user_id)
# include logged out submissions
@submissions_from_logged_out = @quiz.quiz_submissions.logged_out.not_settings_only
@ -849,10 +849,8 @@ class Quizzes::QuizzesController < ApplicationController
def setup_attachments
@attachments = if @submission
Hash[@submission.attachments.map do |attachment|
[attachment.id, attachment]
end
]
@submission.attachments.index_by(&:id)
else
{}
end

View File

@ -81,8 +81,8 @@ class ConversationMessage < ActiveRecord::Base
.select("conversation_messages.*, conversation_participant_id, conversation_message_participants.user_id, conversation_message_participants.tags")
.order('conversation_id DESC, user_id DESC, created_at DESC')
.distinct_on(:conversation_id, :user_id).to_a
map = Hash[ret.map { |m| [[m.conversation_id, m.user_id], m] }]
backmap = Hash[ret.map { |m| [m.conversation_participant_id, m] }]
map = ret.index_by { |m| [m.conversation_id, m.user_id] }
backmap = ret.index_by(&:conversation_participant_id)
if author
shard_participants.each { |cp| cp.last_authored_message = map[[cp.conversation_id, cp.user_id]] || backmap[cp.id] }
else

View File

@ -67,9 +67,7 @@ module Quizzes::QuizRegrader
# quiz question regrades keyed by question id
def question_regrades
@questions ||= @quiz.current_quiz_question_regrades.each_with_object({}) do |qr, hash|
hash[qr.quiz_question_id] = qr
end
@questions ||= @quiz.current_quiz_question_regrades.index_by(&:quiz_question_id)
end
end
end

View File

@ -52,8 +52,8 @@ class Quizzes::QuizSortables
end
def all_objects_hash
@quiz_objects_hash ||= all_objects.each_with_object({}) do |obj, hash|
hash["#{obj.class.name.demodulize.underscore}_#{obj.id}"] = obj
@quiz_objects_hash ||= all_objects.index_by do |obj|
"#{obj.class.name.demodulize.underscore}_#{obj.id}"
end
end

View File

@ -66,12 +66,12 @@ class Quizzes::QuizStatistics::StudentAnalysis < Quizzes::QuizStatistics::Report
# "answers"=> [{"id"=>6782},...],
# "assessment_question_id"=>1022,
# }, ...}
questions = Hash[
questions =
(quiz.quiz_data || []).map { |q| q[:questions] || q }
.flatten
.select { |q| q[:answers] }
.map { |q| [q[:id], q] }
]
.index_by { |q| q[:id] }
stats = {}
found_ids = {}
score_counter = Stats::Counter.new
@ -128,10 +128,10 @@ class Quizzes::QuizStatistics::StudentAnalysis < Quizzes::QuizStatistics::Report
end
assessment_questions = if questions_hash.any? { |_, q| q[:assessment_question_id] }
Hash[
AssessmentQuestion.where(:id => questions_hash.keys)
.map { |aq| [aq.id, aq] }
]
AssessmentQuestion.where(:id => questions_hash.keys)
.index_by(&:id)
else
{}
end
@ -173,10 +173,7 @@ class Quizzes::QuizStatistics::StudentAnalysis < Quizzes::QuizStatistics::Report
end.map do |hash|
hash[:attachment_ids]
end.flatten
@attachments = Hash[Attachment.where(:id => ids).map do |a|
[a.id, a]
end
]
@attachments = Attachment.where(:id => ids).index_by(&:id)
end
def attachment_csv(answer)

View File

@ -36,7 +36,7 @@ class Tableless < ActiveRecord::Base
end
def columns_hash
@columns_hash ||= Hash[columns.map { |c| [c.name, c] }]
@columns_hash ||= columns.index_by(&:name)
end
def column(name, sql_type = nil, default = nil, null = true)

View File

@ -883,7 +883,7 @@ module Api::V1::Assignment
else
# assignment id -> specific submission. never return an array when
# include[]=observed_users was _not_ supplied
hash = Hash[subs_list.map { |s| [s.assignment_id, s] }]
hash = subs_list.index_by(&:assignment_id)
end
hash
end

View File

@ -50,7 +50,7 @@ module Api::V1::DiscussionTopics
fields_with_id = fields.unshift(:id)
root_topics_array = DiscussionTopic.select(fields_with_id).find(root_topic_ids)
root_topics_array.map { |root_topic| [root_topic.id, root_topic] }.to_h
root_topics_array.index_by(&:id)
end
# Public: Serialize an array of DiscussionTopic objects for returning as JSON.

View File

@ -28,7 +28,7 @@ module Api::V1::ModerationGrader
provisional_graders = assignment.provisional_moderation_graders
if assignment.can_view_other_grader_identities?(user)
graders = provisional_graders.preload(:user)
graders_by_id = graders.each_with_object({}) { |grader, map| map[grader.id] = grader }
graders_by_id = graders.index_by(&:id)
api_json(graders, user, session, only: %w(id user_id)).tap do |hash|
hash.each do |grader_json|

View File

@ -421,8 +421,8 @@ class GradeCalculator
def group_score_rows
enrollments_by_user.keys.map do |user_id|
current_group_scores = @current_groups[user_id].map { |group| [group[:global_id], group] }.to_h
final_group_scores = @final_groups[user_id].map { |group| [group[:global_id], group] }.to_h
current_group_scores = @current_groups[user_id].index_by { |group| group[:global_id] }
final_group_scores = @final_groups[user_id].index_by { |group| group[:global_id] }
@groups.map do |group|
agid = group.global_id
current = current_group_scores[agid]
@ -763,9 +763,8 @@ class GradeCalculator
end
assignments_by_group_id = visible_assignments.group_by(&:assignment_group_id)
submissions_by_assignment_id = Hash[
submissions.map { |s| [s.assignment_id, s] }
]
submissions_by_assignment_id =
submissions.index_by(&:assignment_id)
@groups.map do |group|
assignments = assignments_by_group_id[group.id] || []

View File

@ -186,7 +186,7 @@ module ConditionalRelease
expected_assignment_set([@student_id], @as2)
details = Stats.student_details(@rule, @student_id).with_indifferent_access
details_by_id = details[:follow_on_assignments].each_with_object({}) { |f, acc| acc[f.dig(:assignment, :id)] = f }
details_by_id = details[:follow_on_assignments].index_by { |f| f.dig(:assignment, :id) }
expect(details_by_id.map { |k, v| [k, v.dig(:submission, :score)] }).to match_array [
[@b1.id, 3], [@b2.id, 88], [@b3.id, nil], [@b4.id, 93], [@b5.id, nil]
]