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
AllCops:
TargetRubyVersion: 2.7
Exclude:
- "db/schema.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 }
# 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]
if (procedure_fields_count == 0 || procedure_fields_count.nil?)
@ -302,7 +302,6 @@ class StatsController < ApplicationController
# We normalize the data for 24 fields
procedure_mean * (MEAN_NUMBER_OF_CHAMPS_IN_A_FORM / procedure_fields_count)
end
.compact
# Compute the average mean time for all the procedures of this month
month_average = mean(procedure_processing_times)

View file

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

View file

@ -56,9 +56,9 @@ class Champs::CarteChamp < Champ
:zones_humides,
:znieff,
:cadastres
].map do |layer|
].filter_map do |layer|
layer_enabled?(layer) ? layer : nil
end.compact
end
end
def render_options
@ -82,7 +82,7 @@ class Champs::CarteChamp < Champ
bounding_box = RGeo::Cartesian::BoundingBox.new(factory)
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)
end
elsif dossier.present?

View file

@ -879,7 +879,7 @@ class Dossier < ApplicationRecord
end
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)
end
@ -920,7 +920,7 @@ class Dossier < ApplicationRecord
factory = RGeo::Geographic.simple_mercator_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)
end

View file

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

View file

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

View file

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

View file

@ -81,7 +81,7 @@ class PiecesJustificativesService
end
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,

View file

@ -32,7 +32,7 @@ class ProcedureExportService
[dossier.champs, dossier.champs_private]
.flatten
.filter { |champ| champ.is_a?(Champs::SiretChamp) }
end.map(&:etablissement).compact + dossiers.map(&:etablissement).compact
end.filter_map(&:etablissement) + dossiers.filter_map(&:etablissement)
end
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 'and the expert can access the linked dossiers' do
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(: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
let(:saved_avis) { Avis.last(2).first }
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
instructeur.assign_to_procedure(linked_dossier.procedure)
true

View file

@ -8,7 +8,7 @@ feature 'Inviting an expert:', js: true do
let(:expert_password) { 'mot de passe dexpert' }
let(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
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
scenario 'I can invite an expert' do