Refactor (Rubocop): replace map{ … }.compact by filter_map

In Ruby 2.7, Enumerable#filter_map has been added.
This cop identifies places where map { … }.compact can be replaced by filter_map.

See: https://docs.rubocop.org/rubocop-performance/cops_performance.html#performancemapcompact
This commit is contained in:
François Vantomme 2021-06-10 15:24:15 +02:00 committed by Fabrice Gangler
parent 7fb86c34e6
commit c9b1095d1e
13 changed files with 22 additions and 20 deletions

View file

@ -9,6 +9,7 @@ inherit_gem:
- config/rails.yml - config/rails.yml
AllCops: AllCops:
TargetRubyVersion: 2.7
Exclude: Exclude:
- "db/schema.rb" - "db/schema.rb"
- "db/migrate/20190730153555_recreate_structure.rb" - "db/migrate/20190730153555_recreate_structure.rb"

View file

@ -289,7 +289,7 @@ class StatsController < ApplicationController
dossiers_grouped_by_groupe_instructeur = dossier_plucks.group_by { |(groupe_instructeur_id, *_)| groupe_instructeur_id } dossiers_grouped_by_groupe_instructeur = dossier_plucks.group_by { |(groupe_instructeur_id, *_)| groupe_instructeur_id }
# Compute the mean time for this procedure # Compute the mean time for this procedure
procedure_processing_times = dossiers_grouped_by_groupe_instructeur.map do |groupe_instructeur_id, procedure_dossiers| procedure_processing_times = dossiers_grouped_by_groupe_instructeur.filter_map do |groupe_instructeur_id, procedure_dossiers|
procedure_fields_count = groupe_instructeur_id_type_de_champs_count[groupe_instructeur_id] procedure_fields_count = groupe_instructeur_id_type_de_champs_count[groupe_instructeur_id]
if (procedure_fields_count == 0 || procedure_fields_count.nil?) if (procedure_fields_count == 0 || procedure_fields_count.nil?)
@ -302,7 +302,6 @@ class StatsController < ApplicationController
# We normalize the data for 24 fields # We normalize the data for 24 fields
procedure_mean * (MEAN_NUMBER_OF_CHAMPS_IN_A_FORM / procedure_fields_count) procedure_mean * (MEAN_NUMBER_OF_CHAMPS_IN_A_FORM / procedure_fields_count)
end end
.compact
# Compute the average mean time for all the procedures of this month # Compute the average mean time for all the procedures of this month
month_average = mean(procedure_processing_times) month_average = mean(procedure_processing_times)

View file

@ -45,13 +45,13 @@ class AttestationTemplate < ApplicationRecord
acc acc
end end
used_tags.map do |used_tag| used_tags.filter_map do |used_tag|
corresponding_champ = all_champs_with_libelle_index[used_tag] corresponding_champ = all_champs_with_libelle_index[used_tag]
if corresponding_champ && corresponding_champ.value.blank? if corresponding_champ && corresponding_champ.value.blank?
corresponding_champ corresponding_champ
end end
end.compact end
end end
def dup def dup

View file

@ -56,9 +56,9 @@ class Champs::CarteChamp < Champ
:zones_humides, :zones_humides,
:znieff, :znieff,
:cadastres :cadastres
].map do |layer| ].filter_map do |layer|
layer_enabled?(layer) ? layer : nil layer_enabled?(layer) ? layer : nil
end.compact end
end end
def render_options def render_options
@ -82,7 +82,7 @@ class Champs::CarteChamp < Champ
bounding_box = RGeo::Cartesian::BoundingBox.new(factory) bounding_box = RGeo::Cartesian::BoundingBox.new(factory)
if geo_areas.present? if geo_areas.present?
geo_areas.map(&:rgeo_geometry).compact.each do |geometry| geo_areas.filter_map(&:rgeo_geometry).each do |geometry|
bounding_box.add(geometry) bounding_box.add(geometry)
end end
elsif dossier.present? elsif dossier.present?

View file

@ -879,7 +879,7 @@ class Dossier < ApplicationRecord
end end
def linked_dossiers_for(instructeur_or_expert) def linked_dossiers_for(instructeur_or_expert)
dossier_ids = champs.filter(&:dossier_link?).map(&:value).compact dossier_ids = champs.filter(&:dossier_link?).filter_map(&:value)
instructeur_or_expert.dossiers.where(id: dossier_ids) instructeur_or_expert.dossiers.where(id: dossier_ids)
end end
@ -920,7 +920,7 @@ class Dossier < ApplicationRecord
factory = RGeo::Geographic.simple_mercator_factory factory = RGeo::Geographic.simple_mercator_factory
bounding_box = RGeo::Cartesian::BoundingBox.new(factory) bounding_box = RGeo::Cartesian::BoundingBox.new(factory)
geo_areas.map(&:rgeo_geometry).compact.each do |geometry| geo_areas.filter_map(&:rgeo_geometry).each do |geometry|
bounding_box.add(geometry) bounding_box.add(geometry)
end end

View file

@ -135,8 +135,8 @@ class ProcedurePresentation < ApplicationRecord
case table case table
when 'self' when 'self'
dates = values dates = values
.map { |v| Time.zone.parse(v).beginning_of_day rescue nil } .filter_map { |v| Time.zone.parse(v).beginning_of_day rescue nil }
.compact
dossiers.filter_by_datetimes(column, dates) dossiers.filter_by_datetimes(column, dates)
when TYPE_DE_CHAMP when TYPE_DE_CHAMP
dossiers.with_type_de_champ(column) dossiers.with_type_de_champ(column)
@ -147,8 +147,8 @@ class ProcedurePresentation < ApplicationRecord
when 'etablissement' when 'etablissement'
if column == 'entreprise_date_creation' if column == 'entreprise_date_creation'
dates = values dates = values
.map { |v| v.to_date rescue nil } .filter_map { |v| v.to_date rescue nil }
.compact
dossiers dossiers
.includes(table) .includes(table)
.where(table.pluralize => { column => dates }) .where(table.pluralize => { column => dates })

View file

@ -81,6 +81,7 @@ class DossierProjectionService
.pluck(:id, *fields.map { |f| f[COLUMN].to_sym }) .pluck(:id, *fields.map { |f| f[COLUMN].to_sym })
.each { |id, *columns| fields.zip(columns).each { |field, value| field[:id_value_h][id] = value } } .each { |id, *columns| fields.zip(columns).each { |field, value| field[:id_value_h][id] = value } }
when 'followers_instructeurs' when 'followers_instructeurs'
# rubocop:disable Style/HashTransformValues
fields[0][:id_value_h] = Follow fields[0][:id_value_h] = Follow
.active .active
.joins(instructeur: :user) .joins(instructeur: :user)
@ -88,6 +89,7 @@ class DossierProjectionService
.pluck('dossier_id, users.email') .pluck('dossier_id, users.email')
.group_by { |dossier_id, _| dossier_id } .group_by { |dossier_id, _| dossier_id }
.to_h { |dossier_id, dossier_id_emails| [dossier_id, dossier_id_emails.sort.map { |_, email| email }&.join(', ')] } .to_h { |dossier_id, dossier_id_emails| [dossier_id, dossier_id_emails.sort.map { |_, email| email }&.join(', ')] }
# rubocop:enable Style/HashTransformValues
end end
end end

View file

@ -12,8 +12,8 @@ class IPService
if ENV['TRUSTED_NETWORKS'].present? if ENV['TRUSTED_NETWORKS'].present?
ENV['TRUSTED_NETWORKS'] ENV['TRUSTED_NETWORKS']
.split .split
.map { |string| parse_address(string) } .filter_map { |string| parse_address(string) }
.compact
else else
[] []
end end

View file

@ -81,7 +81,7 @@ class PiecesJustificativesService
end end
def self.pjs_for_dossier(dossier) def self.pjs_for_dossier(dossier)
bill_signatures = dossier.dossier_operation_logs.map(&:bill_signature).compact.uniq bill_signatures = dossier.dossier_operation_logs.filter_map(&:bill_signature).uniq
[ [
dossier.justificatif_motivation, dossier.justificatif_motivation,

View file

@ -32,7 +32,7 @@ class ProcedureExportService
[dossier.champs, dossier.champs_private] [dossier.champs, dossier.champs_private]
.flatten .flatten
.filter { |champ| champ.is_a?(Champs::SiretChamp) } .filter { |champ| champ.is_a?(Champs::SiretChamp) }
end.map(&:etablissement).compact + dossiers.map(&:etablissement).compact end.filter_map(&:etablissement) + dossiers.filter_map(&:etablissement)
end end
def avis def avis

View file

@ -272,7 +272,7 @@ describe Experts::AvisController, type: :controller do
context 'when the expert also shares the linked dossiers' do context 'when the expert also shares the linked dossiers' do
context 'and the expert can access the linked dossiers' do context 'and the expert can access the linked dossiers' do
let(:created_avis) { create(:avis, dossier: dossier, claimant: claimant, email: "toto3@gmail.com") } let(:created_avis) { create(:avis, dossier: dossier, claimant: claimant, email: "toto3@gmail.com") }
let(:linked_dossier) { Dossier.find_by(id: dossier.reload.champs.filter(&:dossier_link?).map(&:value).compact) } let(:linked_dossier) { Dossier.find_by(id: dossier.reload.champs.filter(&:dossier_link?).filter_map(&:value)) }
let(:linked_avis) { create(:avis, dossier: linked_dossier, claimant: claimant) } let(:linked_avis) { create(:avis, dossier: linked_dossier, claimant: claimant) }
let(:invite_linked_dossiers) { true } let(:invite_linked_dossiers) { true }

View file

@ -520,7 +520,7 @@ describe Instructeurs::DossiersController, type: :controller do
context 'and the expert can access the linked dossiers' do context 'and the expert can access the linked dossiers' do
let(:saved_avis) { Avis.last(2).first } let(:saved_avis) { Avis.last(2).first }
let(:linked_avis) { Avis.last } let(:linked_avis) { Avis.last }
let(:linked_dossier) { Dossier.find_by(id: dossier.reload.champs.filter(&:dossier_link?).map(&:value).compact) } let(:linked_dossier) { Dossier.find_by(id: dossier.reload.champs.filter(&:dossier_link?).filter_map(&:value)) }
let(:invite_linked_dossiers) do let(:invite_linked_dossiers) do
instructeur.assign_to_procedure(linked_dossier.procedure) instructeur.assign_to_procedure(linked_dossier.procedure)
true true

View file

@ -8,7 +8,7 @@ feature 'Inviting an expert:', js: true do
let(:expert_password) { 'mot de passe dexpert' } let(:expert_password) { 'mot de passe dexpert' }
let(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) } let(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
let(:dossier) { create(:dossier, :en_construction, :with_dossier_link, procedure: procedure) } let(:dossier) { create(:dossier, :en_construction, :with_dossier_link, procedure: procedure) }
let(:linked_dossier) { Dossier.find_by(id: dossier.reload.champs.filter(&:dossier_link?).map(&:value).compact) } let(:linked_dossier) { Dossier.find_by(id: dossier.reload.champs.filter(&:dossier_link?).filter_map(&:value)) }
context 'as an Instructeur' do context 'as an Instructeur' do
scenario 'I can invite an expert' do scenario 'I can invite an expert' do