Add and apply Style/MapToHash rubocop rule

This rule was added in Rubocop 1.24.0. Applying it slightly simplifies
the code.
This commit is contained in:
Javi Martín
2022-08-25 22:24:36 +02:00
parent 5ec7f4a339
commit 4a851c0d82
7 changed files with 16 additions and 13 deletions

View File

@@ -509,6 +509,9 @@ Style/IdenticalConditionalBranches:
Style/IfWithBooleanLiteralBranches: Style/IfWithBooleanLiteralBranches:
Enabled: true Enabled: true
Style/MapToHash:
Enabled: true
Style/MethodDefParentheses: Style/MethodDefParentheses:
Enabled: true Enabled: true

View File

@@ -13,7 +13,7 @@ module SettingsHelper
end end
def setting def setting
@all_settings ||= Setting.all.map { |s| [s.key, s.value.presence] }.to_h @all_settings ||= Setting.all.to_h { |s| [s.key, s.value.presence] }
end end
def display_setting_name(setting_name) def display_setting_name(setting_name)

View File

@@ -95,7 +95,7 @@ module Statisticable
end end
def participants_by_age def participants_by_age
age_groups.map do |start, finish| age_groups.to_h do |start, finish|
count = participants.between_ages(start, finish).count count = participants.between_ages(start, finish).count
[ [
@@ -106,11 +106,11 @@ module Statisticable
percentage: calculate_percentage(count, total_participants) percentage: calculate_percentage(count, total_participants)
} }
] ]
end.to_h end
end end
def participants_by_geozone def participants_by_geozone
geozone_stats.map do |stats| geozone_stats.to_h do |stats|
[ [
stats.name, stats.name,
{ {
@@ -118,7 +118,7 @@ module Statisticable
percentage: stats.percentage percentage: stats.percentage
} }
] ]
end.to_h end
end end
def calculate_percentage(fraction, total) def calculate_percentage(fraction, total)

View File

@@ -113,9 +113,9 @@ class I18nContent < ApplicationRecord
def self.translations_hash(locale) def self.translations_hash(locale)
Rails.cache.fetch(translation_class.where(locale: locale)) do Rails.cache.fetch(translation_class.where(locale: locale)) do
all.map do |content| all.to_h do |content|
[content.key, translation_class.find_by(i18n_content_id: content, locale: locale)&.value] [content.key, translation_class.find_by(i18n_content_id: content, locale: locale)&.value]
end.to_h end
end end
end end

View File

@@ -84,9 +84,9 @@ class UserSegments
end end
def self.geozones def self.geozones
Geozone.order(:name).map do |geozone| Geozone.order(:name).to_h do |geozone|
[geozone.name.gsub(/./) { |char| character_approximation(char) }.underscore.tr(" ", "_"), geozone] [geozone.name.gsub(/./) { |char| character_approximation(char) }.underscore.tr(" ", "_"), geozone]
end.to_h end
end end
def self.character_approximation(char) def self.character_approximation(char)

View File

@@ -9,9 +9,9 @@ describe "Admin edit translatable records", :admin do
let(:fields) { translatable.translated_attribute_names } let(:fields) { translatable.translated_attribute_names }
let(:attributes) do let(:attributes) do
fields.product(%i[en es]).map do |field, locale| fields.product(%i[en es]).to_h do |field, locale|
[:"#{field}_#{locale}", text_for(field, locale)] [:"#{field}_#{locale}", text_for(field, locale)]
end.to_h end
end end
context "Add a translation" do context "Add a translation" do

View File

@@ -182,9 +182,9 @@ describe "Public area translatable records" do
before { translatable.update(attributes.merge(author: user)) } before { translatable.update(attributes.merge(author: user)) }
let(:attributes) do let(:attributes) do
translatable.translated_attribute_names.product(%i[en es]).map do |field, locale| translatable.translated_attribute_names.product(%i[en es]).to_h do |field, locale|
[:"#{field}_#{locale}", text_for(field, locale)] [:"#{field}_#{locale}", text_for(field, locale)]
end.to_h end
end end
context "Update a translation" do context "Update a translation" do