Skip to content

Commit

Permalink
rubocop: Style/MapToHash, Style/MapToSet
Browse files Browse the repository at this point in the history
Change-Id: I75a78a3d86d5875b450d4f32c2f71d8a44a735f4
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/315125
Tested-by: Service Cloud Jenkins <[email protected]>
Reviewed-by: Aaron Ogata <[email protected]>
QA-Review: Cody Cutrer <[email protected]>
Product-Review: Cody Cutrer <[email protected]>
  • Loading branch information
ccutrer committed Apr 11, 2023
1 parent 3ac04f0 commit f46fef5
Show file tree
Hide file tree
Showing 34 changed files with 59 additions and 59 deletions.
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2806,12 +2806,12 @@ def set_js_assignment_data
permissions[:manage_assignments_delete] = permissions[:manage_assignments]
permissions[:manage] = permissions[:manage_assignments]
end
permissions[:by_assignment_id] = @context.assignments.map do |assignment|
permissions[:by_assignment_id] = @context.assignments.to_h do |assignment|
[assignment.id, {
update: assignment.user_can_update?(@current_user, session),
delete: assignment.grants_right?(@current_user, :delete)
}]
end.to_h
end

current_user_has_been_observer_in_this_course = @context.user_has_been_observer?(@current_user)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ def restrict_item

mc_tag = @template.content_tag_for(item)
if value_to_boolean(params[:restricted])
custom_restrictions = params[:restrictions] && params[:restrictions].to_unsafe_h.map { |k, v| [k.to_sym, value_to_boolean(v)] }.to_h
custom_restrictions = params[:restrictions] && params[:restrictions].to_unsafe_h.to_h { |k, v| [k.to_sym, value_to_boolean(v)] }
mc_tag.restrictions = custom_restrictions || @template.default_restrictions_for(item)
mc_tag.use_default_restrictions = !custom_restrictions
else
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/pseudonyms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def confirm_change_password
end
@password_pseudonyms = @cc.user.pseudonyms.active_only.select { |p| p.account.canvas_authentication? }
js_env PASSWORD_POLICY: @domain_root_account.password_policy,
PASSWORD_POLICIES: @password_pseudonyms.map { |p| [p.id, p.account.password_policy] }.to_h
PASSWORD_POLICIES: @password_pseudonyms.to_h { |p| [p.id, p.account.password_policy] }
end
end

Expand Down
6 changes: 3 additions & 3 deletions app/controllers/quizzes/quizzes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def show
@stored_params = (@submission.temporary_data rescue nil) if params[:take] && @submission && (@submission.untaken? || @submission.preview?)
@stored_params ||= {}
hash = {
ATTACHMENTS: @attachments.map { |_, a| [a.id, attachment_hash(a)] }.to_h,
ATTACHMENTS: @attachments.to_h { |_, a| [a.id, attachment_hash(a)] },
CONTEXT_ACTION_SOURCE: :quizzes,
COURSE_ID: @context.id,
LOCKDOWN_BROWSER: @quiz.require_lockdown_browser?,
Expand Down Expand Up @@ -333,9 +333,9 @@ def edit
flash[:notice] = t("notices.has_submissions_already", "Keep in mind, some students have already taken or started taking this quiz")
end

regrade_options = @quiz.current_quiz_question_regrades.map do |qqr|
regrade_options = @quiz.current_quiz_question_regrades.to_h do |qqr|
[qqr.quiz_question_id, qqr.regrade_option]
end.to_h
end
sections = @context.course_sections.active

max_name_length_required_for_account = AssignmentUtil.name_length_required_for_account?(@context)
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/mutations/base_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def validation_error(message, attribute: "message")
# `override_keys` is a hash where the key is the field at the table and the value
# is the alias of the field; the keys and values need to be set as symbols.
def errors_for(model, override_keys = {})
input_fields = self.class.arguments.values.map { |a| [a.keyword, a.name] }.to_h
input_fields = self.class.arguments.values.to_h { |a| [a.keyword, a.name] }

{
errors: model.errors.entries.map do |attribute, message|
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/outcomes_service_alignments_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_os_aligned_outcomes(context)

# minify response to reduce cache size
# needs to be updated after OUT-5473 and OUT-5474 are merged
(resp_body[:outcomes] || []).map { |o| [o[:external_id], o[:alignments] || []] }.to_h
(resp_body[:outcomes] || []).to_h { |o| [o[:external_id], o[:alignments] || []] }
end
end

Expand Down
18 changes: 9 additions & 9 deletions app/models/assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ def course_broadcast_data
p.to do |assignment|
# everyone who is _not_ covered by an assignment override affecting due_at
# (the AssignmentOverride records will take care of notifying those users)
excluded_ids = participants_with_overridden_due_at.map(&:id).to_set
excluded_ids = participants_with_overridden_due_at.to_set(&:id)
BroadcastPolicies::AssignmentParticipants.new(assignment, excluded_ids).to
end
p.whenever do |assignment|
Expand Down Expand Up @@ -2330,9 +2330,9 @@ def submit_homework(original_student, opts = {})

# move the following 2 lines out of the trnx
# make the trnx simpler. The trnx will have fewer locks and rollbacks.
homework_lti_user_id_hash = students.map do |student|
homework_lti_user_id_hash = students.to_h do |student|
[student.global_id, Lti::Asset.opaque_identifier_for(student)]
end.to_h
end
submissions = find_or_create_submissions(students, Submission.preload(:grading_period)).sort_by(&:id)

transaction do
Expand Down Expand Up @@ -2510,21 +2510,21 @@ def representatives(user:, includes: [:inactive], group_id: nil, section_id: nil
return visible_students_for_speed_grader(user: user, includes: includes, group_id: group_id, section_id: section_id) unless grade_as_group?

submissions = self.submissions.to_a
user_ids_with_submissions = submissions.select(&:has_submission?).map(&:user_id).to_set
user_ids_with_submissions = submissions.select(&:has_submission?).to_set(&:user_id)
user_ids_with_turnitin_data = if turnitin_enabled?
submissions.reject { |s| s.turnitin_data.blank? }.map(&:user_id).to_set
submissions.reject { |s| s.turnitin_data.blank? }.to_set(&:user_id)
else
[]
end
user_ids_with_vericite_data = if vericite_enabled?
submissions
.reject { |s| s.turnitin_data.blank? }
.map(&:user_id).to_set
.to_set(&:user_id)
else
[]
end
# this only includes users with a submission who are unexcused
user_ids_who_arent_excused = submissions.reject(&:excused?).map(&:user_id).to_set
user_ids_who_arent_excused = submissions.reject(&:excused?).to_set(&:user_id)

enrollment_state =
context.all_accepted_student_enrollments.pluck(:user_id, :workflow_state).to_h
Expand All @@ -2533,7 +2533,7 @@ def representatives(user:, includes: [:inactive], group_id: nil, section_id: nil
enrollment_priority = { "active" => 1, "inactive" => 2 }
enrollment_priority.default = 100

visible_student_ids = visible_students_for_speed_grader(user: user, includes: includes).map(&:id).to_set
visible_student_ids = visible_students_for_speed_grader(user: user, includes: includes).to_set(&:id)

reps_and_others = groups_and_ungrouped(user, includes: includes).filter_map do |group_name, group_info|
group_students = group_info[:users]
Expand Down Expand Up @@ -2790,7 +2790,7 @@ def current_submissions_and_assessors
{ student_ids: student_ids,
submissions: submissions,
submission_ids: Set.new(submissions.pluck(:id)),
assessor_id_map: submissions.map { |s| [s.id, s.assessment_requests.map(&:assessor_asset_id)] }.to_h }
assessor_id_map: submissions.to_h { |s| [s.id, s.assessment_requests.map(&:assessor_asset_id)] } }
end

def sorted_review_candidates(peer_review_params, current_submission, candidate_set)
Expand Down
2 changes: 1 addition & 1 deletion app/models/discussion_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def self.max_depth

def self.rating_sums(entry_ids)
sums = where(id: entry_ids).where("COALESCE(rating_sum, 0) != 0")
sums.map { |x| [x.id, x.rating_sum] }.to_h
sums.to_h { |x| [x.id, x.rating_sum] }
end

def set_depth
Expand Down
2 changes: 1 addition & 1 deletion app/models/discussion_entry_participant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def self.forced_read_state_entry_ids(entry_ids, user)

def self.entry_ratings(entry_ids, user)
ratings = where(user_id: user, discussion_entry_id: entry_ids).where.not(rating: nil)
ratings.map { |x| [x.discussion_entry_id, x.rating] }.to_h
ratings.to_h { |x| [x.discussion_entry_id, x.rating] }
end

def self.not_null_column_object(column: nil, entry: nil, user: nil)
Expand Down
2 changes: 1 addition & 1 deletion app/models/discussion_topic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1342,7 +1342,7 @@ def users_with_section_visibility(users)
def participants(include_observers = false)
participants = context.participants(include_observers: include_observers, by_date: true)
participants_in_section = users_with_section_visibility(participants.compact)
if user && !participants_in_section.map(&:id).to_set.include?(user.id)
if user && !participants_in_section.to_set(&:id).include?(user.id)
participants_in_section += [user]
end
participants_in_section
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,12 @@ def self.bulk_insert_objects(objects, excluded_columns: ["primary_key"])
hashed_objects = []
excluded_columns << objects.first.class.primary_key if excluded_columns.delete("primary_key")
objects.each do |object|
hashed_objects << object.attributes.except(excluded_columns.join(",")).map do |(name, value)|
hashed_objects << object.attributes.except(excluded_columns.join(",")).to_h do |name, value|
if (type = object.class.attribute_types[name]).is_a?(ActiveRecord::Type::Serialized)
value = type.serialize(value)
end
[name, value]
end.to_h
end
end
objects.first.class.bulk_insert(hashed_objects)
end
Expand Down
2 changes: 1 addition & 1 deletion gems/acts_as_list/lib/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def acts_as_list(options = {})
scope = new_scope

# build the conditions hash, using literal values or the attribute if it's self
conditions = scope.map { |k, v| [k, v == self ? k : v.inspect] }.to_h
conditions = scope.to_h { |k, v| [k, v == self ? k : v.inspect] }
conditions = conditions.map { |c, v| "#{c}: #{v}" }.join(", ")
# build the in_scope method, matching literals or requiring a foreign keys
# to be non-nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def ensure_filtered_dependencies_pinned
end
end

proven_pinned = check_dependencies.map { |x| [x, false] }.to_h
proven_pinned = check_dependencies.to_h { |x| [x, false] } # rubocop:disable Rails/IndexWith

valid_sources = []

Expand Down
2 changes: 1 addition & 1 deletion gems/dynamic_settings/lib/dynamic_settings/prefix_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def full_key(key, global: false)
end

def populate_cache(subtree, ttl)
cache.write_set(subtree.map { |st| [CACHE_KEY_PREFIX + st[:key], st[:value]] }.to_h, ttl: ttl)
cache.write_set(subtree.to_h { |st| [CACHE_KEY_PREFIX + st[:key], st[:value]] }, ttl: ttl)
end
end
end
2 changes: 1 addition & 1 deletion gems/i18n_tasks/lib/i18n_tasks/csv_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def load_csv(filename)
ret = {}
csv_locales.each do |locale|
ret[locale.to_sym] = {
scope.to_sym => data.map { |row| [row["key"].to_sym, row[locale]] }.to_h.compact
scope.to_sym => data.to_h { |row| [row["key"].to_sym, row[locale]] }.compact
}
end
ret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def outcome_scope
def export_outcomes(csv, headers, include_ratings)
I18n.locale = account.default_locale if account.default_locale.present?
outcomes = outcome_scope
friendly_descriptions = resolve_friendly_descriptions(account, nil, outcomes.map(&:id)).map { |description| [description.learning_outcome_id, description.description] }.to_h
friendly_descriptions = resolve_friendly_descriptions(account, nil, outcomes.map(&:id)).to_h { |description| [description.learning_outcome_id, description.description] }
outcomes.find_each do |row|
outcome_model = row.learning_outcome_content
outcome = row.attributes.dup
Expand Down
2 changes: 1 addition & 1 deletion lib/address_book/messageable_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MessageableUser < AddressBook::Base
def known_users(users, options = {})
options = { strict_checks: true }.merge(options)
if options[:context]
user_ids = users.map { |user| Shard.global_id_for(user) }.to_set
user_ids = users.to_set { |user| Shard.global_id_for(user) }
asset_string = options[:context].respond_to?(:asset_string) ? options[:context].asset_string : options[:context]
known_users = @sender.messageable_user_calculator
.messageable_users_in_context(asset_string, admin_context: admin_context?(options[:context]))
Expand Down
2 changes: 1 addition & 1 deletion lib/api/v1/assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ def submissions_hash(include_params, assignments, submissions_for_user = nil)
has_observed_users = include_params.include?("observed_users")

subs_list = if submissions_for_user
assignment_ids = assignments.map(&:id).to_set
assignment_ids = assignments.to_set(&:id)
submissions_for_user.select do |s|
assignment_ids.include?(s.assignment_id)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/api/v1/assignment_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def update_assignment_override_without_save(override, override_data)
defunct_student_ids = if override.new_record?
Set.new
else
override.assignment_override_students.map(&:user_id).to_set
override.assignment_override_students.to_set(&:user_id)
end

override.changed_student_ids = Set.new
Expand Down
4 changes: 2 additions & 2 deletions lib/api/v1/rubric_assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ def rubric_assessment_json(rubric_assessment, user, session, opts = {})
end

def indexed_rubric_assessment_json(rubric_assessment)
rubric_assessment.data.map do |r|
rubric_assessment.data.to_h do |r|
[r[:criterion_id], { rating_id: r[:id] }.merge(r.slice(:comments, :points))]
end.to_h
end
end

def full_rubric_assessment_json_for_submissions(rubric_assessment, user, session)
Expand Down
2 changes: 1 addition & 1 deletion lib/canvas/icu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def #{attribute}=(value)
:lower_first => 24,
:upper_first => 25,
}.freeze
ATTRIBUTE_VALUES_INVERSE = ATTRIBUTE_VALUES.map { |k, v| [v, k] }.to_h.freeze
ATTRIBUTE_VALUES_INVERSE = ATTRIBUTE_VALUES.to_h { |k, v| [v, k] }.freeze
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/cc/importer/canvas/blueprint_settings_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def convert_blueprint_settings(doc)

blueprint_settings = doc.at_css("blueprint_settings")
hash["use_default_restrictions_by_type"] = get_bool_val(blueprint_settings, "use_default_restrictions_by_type", false)
hash["restrictions"] = blueprint_settings.css("restrictions > restriction").map { |node| convert_restriction(node) }.to_h
hash["restrictions"] = blueprint_settings.css("restrictions > restriction").to_h { |node| convert_restriction(node) }
hash["restricted_items"] = blueprint_settings.css("restricted_items > item").map { |node| convert_item(node) }

hash
Expand Down
2 changes: 1 addition & 1 deletion lib/locale_selection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def crowdsourced_locales

def self.locales_with_aliases
@locales_with_aliases ||= begin
locales = I18n.available_locales.map { |l| [l.to_s, nil] }.to_h
locales = I18n.available_locales.to_h { |l| [l.to_s, nil] }
locales.keys.each do |locale| # rubocop:disable Style/HashEachMethods mutation during iteration
aliases = Array.wrap(I18n.send(:t, :aliases, locale: locale, default: nil))
aliases.each do |a|
Expand Down
6 changes: 3 additions & 3 deletions lib/sticky_sis_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,21 @@ def reload(*a)
private

def load_stuck_sis_fields_cache
@stuck_sis_fields_cache ||= (read_attribute(:stuck_sis_fields) || "").split(",").map(&:to_sym).to_set
@stuck_sis_fields_cache ||= (read_attribute(:stuck_sis_fields) || "").split(",").to_set(&:to_sym)
end

def calculate_currently_stuck_sis_fields
@sis_fields_to_stick ||= [].to_set
@sis_fields_to_unstick ||= [].to_set
changed_sis_fields = self.class.sticky_sis_fields & (changed.map(&:to_sym).to_set | @sis_fields_to_stick)
changed_sis_fields = self.class.sticky_sis_fields & (changed.to_set(&:to_sym) | @sis_fields_to_stick)
(load_stuck_sis_fields_cache | changed_sis_fields) - @sis_fields_to_unstick
end
end

module ClassMethods
# specify which fields are able to be stuck
def are_sis_sticky(*fields)
self.sticky_sis_fields = fields.map(&:to_sym).to_set
self.sticky_sis_fields = fields.to_set(&:to_sym)
end

# takes a block and runs it with the following options:
Expand Down
2 changes: 1 addition & 1 deletion spec/apis/v1/grade_change_audit_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def fetch_for_course_and_other_contexts(contexts, options = {})
sorted_contexts = contexts.select { |key, _| expected_contexts.include?(key) }
.sort_by { |key, _| expected_contexts.index(key) }

arguments = sorted_contexts.map { |key, value| ["#{key}_id".to_sym, value.id] }.to_h
arguments = sorted_contexts.to_h { |key, value| ["#{key}_id".to_sym, value.id] }
arguments.merge!({
controller: :grade_change_audit_api,
action: :for_course_and_other_parameters,
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/groups_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@
users_json = parsed_json.first["users"]
expect(users_json).not_to be_nil
expect(users_json.length).to eq 2
ids_json = users_json.map { |u| u["id"] }.to_set
ids_json = users_json.to_set { |u| u["id"] }
expect(ids_json).to eq [student1.id, student2.id].to_set
names_json = users_json.map { |u| u["name"] }.to_set
names_json = users_json.to_set { |u| u["name"] }
expect(names_json).to eq [student1.name, student2.name].to_set
expect(response).to be_successful
end
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/rubric_assessment_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def rubric_assessment_model(opts = {})
user: opts[:user],
assessor: opts[:assessor] || opts[:user],
artifact: rubric_association.association_object.submit_homework(opts[:user]),
assessment: rubric_association.rubric.criteria_object.map { |x| ["criterion_#{x.id}".to_sym, {}] }.to_h.merge(
assessment: rubric_association.rubric.criteria_object.to_h { |x| ["criterion_#{x.id}".to_sym, {}] }.merge(
assessment_type: (opts[:assessment_type] || "no_reason")
)
)
Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/mutations/update_assignment_override_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def execute_with_input(update_input, user_executing = @teacher)
#

def assert_adhoc_override(result_override, student_ids, assignment_id = @assignment_id)
expect(result_override["set"]["students"].map { |s| s["_id"].to_i }.to_set).to eq student_ids.to_set
expect(result_override["set"]["students"].to_set { |s| s["_id"].to_i }).to eq student_ids.to_set
override = Assignment.find(assignment_id).assignment_overrides.detect { |e| e.id.to_s == result_override["_id"] }
expect(override).to_not be_nil
override_set = override.set
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/outcomes_service_alignments_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def mock_aligned_outcomes_response(outcomes, quizzes)
end

def mock_minified_aligned_outcomes_response(response)
(response[:outcomes] || []).map { |o| [o[:external_id], o[:alignments]] }.to_h
(response[:outcomes] || []).to_h { |o| [o[:external_id], o[:alignments]] }
end

around do |example|
Expand Down
4 changes: 2 additions & 2 deletions spec/models/active_record_base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def create_courses(account, start_times)
# updated_at
expect(account.courses.count_by_date).to eql({ start_times.first.to_date => 10 })

expect(account.courses.count_by_date(column: :start_at)).to eql start_times.each_with_index.map { |t, i| [t.to_date, i + 1] }.to_h
expect(account.courses.count_by_date(column: :start_at)).to eql(start_times.each_with_index.to_h { |t, i| [t.to_date, i + 1] })
end

it "justs do the last 20 days by default" do
Expand All @@ -59,7 +59,7 @@ def create_courses(account, start_times)
# updated_at
expect(account.courses.count_by_date).to eql({ start_times.first.to_date => 10 })

expect(account.courses.count_by_date(column: :start_at)).to eql start_times[0..1].each_with_index.map { |t, i| [t.to_date, i + 1] }.to_h
expect(account.courses.count_by_date(column: :start_at)).to eql(start_times[0..1].each_with_index.to_h { |t, i| [t.to_date, i + 1] })
end
end

Expand Down
Loading

0 comments on commit f46fef5

Please sign in to comment.