Add Style/CollectionMethods to rubocop

This commit is contained in:
Paul Chavard 2019-09-12 11:26:22 +02:00
parent 3f970eee74
commit 3b8d3b7967
37 changed files with 67 additions and 60 deletions

View file

@ -866,7 +866,14 @@ Style/ClassVars:
Enabled: false
Style/CollectionMethods:
Enabled: false
PreferredMethods:
collect: map
collect!: map!
inject: reduce
detect: find
select: filter
find_all: filter
Enabled: true
Style/ColonMethodCall:
Enabled: true

View file

@ -36,7 +36,7 @@ module CreateAvisConcern
if failed.any?
flash.now.alert = failed
.select { |avis| avis.errors.present? }
.filter { |avis| avis.errors.present? }
.map { |avis| "#{avis.email} : #{avis.errors.full_messages.join(', ')}" }
# When an error occurs, return the avis back to the controller

View file

@ -76,7 +76,7 @@ module Instructeurs
if @current_filters.count > 0
filtered_ids = procedure_presentation.filtered_ids(@dossiers, statut)
filtered_sorted_ids = sorted_ids.select { |id| filtered_ids.include?(id) }
filtered_sorted_ids = sorted_ids.filter { |id| filtered_ids.include?(id) }
else
filtered_sorted_ids = sorted_ids
end

View file

@ -24,11 +24,11 @@ class RootController < ApplicationController
.map.with_index { |type_de_champ, i| type_de_champ.champ.build(id: i) }
all_champs
.select { |champ| champ.type_champ == TypeDeChamp.type_champs.fetch(:header_section) }
.filter { |champ| champ.type_champ == TypeDeChamp.type_champs.fetch(:header_section) }
.each { |champ| champ.type_de_champ.libelle = 'un super titre de section' }
all_champs
.select { |champ| [TypeDeChamp.type_champs.fetch(:drop_down_list), TypeDeChamp.type_champs.fetch(:multiple_drop_down_list)].include?(champ.type_champ) }
.filter { |champ| [TypeDeChamp.type_champs.fetch(:drop_down_list), TypeDeChamp.type_champs.fetch(:multiple_drop_down_list)].include?(champ.type_champ) }
.each do |champ|
champ.type_de_champ.drop_down_list = DropDownList.new(type_de_champ: champ.type_de_champ)
champ.drop_down_list.value =
@ -47,7 +47,7 @@ class RootController < ApplicationController
type_champ_values.each do |(type_champ, value)|
all_champs
.select { |champ| champ.type_champ == type_champ }
.filter { |champ| champ.type_champ == type_champ }
.each { |champ| champ.value = value }
end

View file

@ -356,7 +356,7 @@ class StatsController < ApplicationController
if weekly_dossiers_count == 0
result = 0
else
weekly_dossier_with_avis_count = weekly_dossiers.select { |dossier| dossier.avis.present? }.count
weekly_dossier_with_avis_count = weekly_dossiers.filter { |dossier| dossier.avis.present? }.count
result = percentage(weekly_dossier_with_avis_count, weekly_dossiers_count)
end

View file

@ -6,7 +6,7 @@ module Users
.includes(:procedure)
.map(&:procedure)
.uniq
.select(&:publiee?)
.filter(&:publiee?)
@popular_demarches = Procedure
.includes(:service)

View file

@ -2,19 +2,19 @@ class Champs::CarteChamp < Champ
# We are not using scopes here as we want to access
# the following collections on unsaved records.
def cadastres
geo_areas.select do |area|
geo_areas.filter do |area|
area.source == GeoArea.sources.fetch(:cadastre)
end
end
def quartiers_prioritaires
geo_areas.select do |area|
geo_areas.filter do |area|
area.source == GeoArea.sources.fetch(:quartier_prioritaire)
end
end
def parcelles_agricoles
geo_areas.select do |area|
geo_areas.filter do |area|
area.source == GeoArea.sources.fetch(:parcelle_agricole)
end
end

View file

@ -30,7 +30,7 @@ class Champs::LinkedDropDownListChamp < Champ
end
def to_s
value.present? ? [primary_value, secondary_value].select(&:present?).join(' / ') : ""
value.present? ? [primary_value, secondary_value].filter(&:present?).join(' / ') : ""
end
def for_export

View file

@ -4,6 +4,6 @@ class Champs::PaysChamp < Champs::TextChamp
end
def self.disabled_options
pays.select { |v| (v =~ /^--.*--$/).present? }
pays.filter { |v| (v =~ /^--.*--$/).present? }
end
end

View file

@ -179,7 +179,7 @@ module TagsSubstitutionConcern
raise NameError.new("The class #{self.class.name} includes TagsSubstitutionConcern, it should define the DOSSIER_STATE constant but it does not", :DOSSIER_STATE)
end
tags.select { |tag| tag[:available_for_states].include?(self.class::DOSSIER_STATE) }
tags.filter { |tag| tag[:available_for_states].include?(self.class::DOSSIER_STATE) }
end
def champ_public_tags
@ -213,12 +213,12 @@ module TagsSubstitutionConcern
tags_and_datas
.map { |(tags, data)| [filter_tags(tags), data] }
.inject(text) { |acc, (tags, data)| replace_tags_with_values_from_data(acc, tags, data) }
.reduce(text) { |acc, (tags, data)| replace_tags_with_values_from_data(acc, tags, data) }
end
def replace_tags_with_values_from_data(text, tags, data)
if data.present?
tags.inject(text) do |acc, tag|
tags.reduce(text) do |acc, tag|
replace_tag(acc, tag, data)
end
else

View file

@ -426,15 +426,15 @@ class Dossier < ApplicationRecord
end
def check_mandatory_champs
(champs + champs.select(&:repetition?).flat_map(&:champs))
.select(&:mandatory_and_blank?)
(champs + champs.filter(&:repetition?).flat_map(&:champs))
.filter(&:mandatory_and_blank?)
.map do |champ|
"Le champ #{champ.libelle.truncate(200)} doit être rempli."
end
end
def modifier_annotations!(instructeur)
champs_private.select(&:value_previously_changed?).each do |champ|
champs_private.filter(&:value_previously_changed?).each do |champ|
log_dossier_operation(instructeur, :modifier_annotation, champ)
end
end

View file

@ -9,7 +9,7 @@ class DropDownList < ApplicationRecord
end
def disabled_options
options.select { |v| (v =~ /^--.*--$/).present? }
options.filter { |v| (v =~ /^--.*--$/).present? }
end
def multiple

View file

@ -74,7 +74,7 @@ class Instructeur < ApplicationRecord
active_procedure_overviews = procedures
.publiees
.map { |procedure| procedure.procedure_overview(start_date) }
.select(&:had_some_activities?)
.filter(&:had_some_activities?)
if active_procedure_overviews.count == 0
nil

View file

@ -30,7 +30,7 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
description: "#{description} (menu primaire)",
lambda: -> (champs) {
champs
.detect { |champ| champ.type_de_champ == tdc }
.find { |champ| champ.type_de_champ == tdc }
&.primary_value
}
}
@ -41,7 +41,7 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
description: "#{description} (menu secondaire)",
lambda: -> (champs) {
champs
.detect { |champ| champ.type_de_champ == tdc }
.find { |champ| champ.type_de_champ == tdc }
&.secondary_value
}
}

View file

@ -14,7 +14,7 @@ class TypesDeChamp::TypeDeChampBase
libelle: libelle,
description: description,
lambda: -> (champs) {
champs.detect { |champ| champ.type_de_champ == tdc }
champs.find { |champ| champ.type_de_champ == tdc }
}
}
]

View file

@ -39,7 +39,7 @@ class AdministrateurUsageStatisticsService
ds_nb_demarches_brouillons: nb_demarches_by_administrateur_id_and_state[[administrateur.id, "brouillon"]],
nb_demarches_test: nb_dossiers_by_procedure_id
.select { |procedure_id, count| count > 0 && is_brouillon(procedure_id) }
.filter { |procedure_id, count| count > 0 && is_brouillon(procedure_id) }
.count,
nb_demarches_prod: nb_dossiers_by_procedure_id
.reject { |procedure_id, count| count == 0 || is_brouillon(procedure_id) }

View file

@ -1,7 +1,7 @@
class PiecesJustificativesService
def self.liste_pieces_justificatives(dossier)
champs_blocs_repetables = dossier.champs
.select { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:repetition) }
.filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:repetition) }
.flat_map(&:champs)
champs_pieces_justificatives_with_attachments(
@ -15,7 +15,7 @@ class PiecesJustificativesService
end
def self.serialize_types_de_champ_as_type_pj(procedure)
tdcs = procedure.types_de_champ.select { |type_champ| type_champ.old_pj.present? }
tdcs = procedure.types_de_champ.filter { |type_champ| type_champ.old_pj.present? }
tdcs.map.with_index do |type_champ, order_place|
description = type_champ.description
if /^(?<original_description>.*?)(?:[\r\n]+)Récupérer le formulaire vierge pour mon dossier : (?<lien_demarche>http.*)$/m =~ description
@ -32,7 +32,7 @@ class PiecesJustificativesService
end
def self.serialize_champs_as_pjs(dossier)
dossier.champs.select { |champ| champ.type_de_champ.old_pj }.map do |champ|
dossier.champs.filter { |champ| champ.type_de_champ.old_pj }.map do |champ|
{
created_at: champ.created_at&.in_time_zone('UTC'),
type_de_piece_justificative_id: champ.type_de_champ.old_pj[:stable_id],
@ -46,7 +46,7 @@ class PiecesJustificativesService
def self.champs_pieces_justificatives_with_attachments(champs)
champs
.select { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) }
.filter { |c| c.type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative) }
.filter { |pj| pj.piece_justificative_file.attached? }
end
end

View file

@ -122,7 +122,7 @@ class ProcedureExportService
@etablissements = @dossiers.flat_map do |dossier|
[dossier.champs, dossier.champs_private]
.flatten
.select { |champ| champ.is_a?(Champs::SiretChamp) }
.filter { |champ| champ.is_a?(Champs::SiretChamp) }
end.map(&:etablissement).compact
if @etablissements.any?

View file

@ -40,7 +40,7 @@ class ProcedureExportV2Service
@etablissements ||= dossiers.flat_map do |dossier|
[dossier.champs, dossier.champs_private]
.flatten
.select { |champ| champ.is_a?(Champs::SiretChamp) }
.filter { |champ| champ.is_a?(Champs::SiretChamp) }
end.map(&:etablissement).compact + dossiers.map(&:etablissement).compact
end
@ -52,7 +52,7 @@ class ProcedureExportV2Service
@champs_repetables ||= dossiers.flat_map do |dossier|
[dossier.champs, dossier.champs_private]
.flatten
.select { |champ| champ.is_a?(Champs::RepetitionChamp) }
.filter { |champ| champ.is_a?(Champs::RepetitionChamp) }
end.group_by(&:libelle)
end

View file

@ -7,7 +7,7 @@ class ZxcvbnService
wxcvbn = compute_zxcvbn
score = wxcvbn.score
length = @password.blank? ? 0 : @password.length
vulnerabilities = wxcvbn.match_sequence.map { |m| m.matched_word.nil? ? m.token : m.matched_word }.select { |s| s.length > 2 }.join(', ')
vulnerabilities = wxcvbn.match_sequence.map { |m| m.matched_word.nil? ? m.token : m.matched_word }.filter { |s| s.length > 2 }.join(', ')
[score, vulnerabilities, length]
end

View file

@ -3,7 +3,7 @@ namespace :'2017_07_18_clean_followed_dossiers' do
Follow.where(gestionnaire_id: nil).destroy_all
Follow.where(dossier_id: nil).destroy_all
duplicate_follows = Follow.group('gestionnaire_id', 'dossier_id').count.select { |_gestionnaire_id_dossier_id, count| count > 1 }.keys
duplicate_follows = Follow.group('gestionnaire_id', 'dossier_id').count.filter { |_gestionnaire_id_dossier_id, count| count > 1 }.keys
duplicate_ids = duplicate_follows.map { |gestionnaire_id, dossier_id| Follow.where(gestionnaire_id: gestionnaire_id, dossier_id: dossier_id).pluck(:id) }

View file

@ -3,7 +3,7 @@ require Rails.root.join("lib", "tasks", "task_helper")
namespace :'2017_07_26_clean_birthdate_on_individual' do
task clean: :environment do
# remove duplicates
duplicate_individuals = Individual.group("dossier_id").count.select { |_dossier_id, count| count > 1 }.keys
duplicate_individuals = Individual.group("dossier_id").count.filter { |_dossier_id, count| count > 1 }.keys
duplicate_individuals.each { |dossier_id| Individual.where(dossier_id: dossier_id, nom: nil).delete_all }
# Match "" => nil
@ -11,13 +11,13 @@ namespace :'2017_07_26_clean_birthdate_on_individual' do
individuals_with_date = Individual.where.not(birthdate: nil)
# Match 31/12/2017 => 2017-12-31
individuals_with_date.select { |i| /^\d{2}\/\d{2}\/\d{4}$/.match(i.birthdate) }.each do |i|
individuals_with_date.filter { |i| /^\d{2}\/\d{2}\/\d{4}$/.match(i.birthdate) }.each do |i|
rake_puts "cleaning #{i.birthdate}"
i.update(birthdate: Date.parse(i.birthdate).iso8601) rescue nil
end
# Match 31/12/17 => 2017-12-31
individuals_with_date.select { |i| /^\d{2}\/\d{2}\/\d{2}$/.match(i.birthdate) }.each do |i|
individuals_with_date.filter { |i| /^\d{2}\/\d{2}\/\d{2}$/.match(i.birthdate) }.each do |i|
rake_puts "cleaning #{i.birthdate}"
new_date = Date.strptime(i.birthdate, "%d/%m/%y")
if new_date.year > 2017

View file

@ -1,6 +1,6 @@
namespace :'2017_08_01_clean_assign_to' do
task clean: :environment do
duplicates = AssignTo.group(:gestionnaire_id, :procedure_id).count.select { |_gestionnaire_id_procedure_id, count| count > 1 }.keys
duplicates = AssignTo.group(:gestionnaire_id, :procedure_id).count.filter { |_gestionnaire_id_procedure_id, count| count > 1 }.keys
duplicate_ids = duplicates.map { |gestionnaire_id, procedure_id| AssignTo.where(gestionnaire_id: gestionnaire_id, procedure_id: procedure_id).pluck(:id) }

View file

@ -5,20 +5,20 @@ namespace :'2018_01_18_clean_datetime_in_champs' do
datetime_champs = TypeDeChamp.where(type_champ: "datetime").flat_map(&:champ)
# Match " HH:MM" => nil a datetime is not valid if not composed by date AND time
datetime_champs.select { |c| /^\s\d{2}:\d{2}$/.match(c.value) }.each do |c|
datetime_champs.filter { |c| /^\s\d{2}:\d{2}$/.match(c.value) }.each do |c|
rake_puts "cleaning #{c.value} => nil"
c.update_columns(value: nil)
end
# Match "dd/mm/YYYY HH:MM" => "YYYY-mm-dd HH:MM"
datetime_champs.select { |c| /^\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}$/ =~ c.value }.each do |c|
datetime_champs.filter { |c| /^\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}$/ =~ c.value }.each do |c|
formated_date = Time.zone.strptime(c.value, "%d/%m/%Y %H:%M").strftime("%Y-%m-%d %H:%M")
rake_puts "cleaning #{c.value} => #{formated_date}"
c.update_columns(value: formated_date)
end
# Match "ddmmYYYY HH:MM" => "YYYY-mm-dd HH:MM"
datetime_champs.select { |c| /^\d{8}\s\d{2}:\d{2}$/ =~ c.value }.each do |c|
datetime_champs.filter { |c| /^\d{8}\s\d{2}:\d{2}$/ =~ c.value }.each do |c|
day = c.value[0, 2]
month = c.value[2, 2]
year = c.value[4, 4]

View file

@ -1,10 +1,10 @@
namespace :'2018_02_28_clean_invalid_emails_accounts' do
task clean: :environment do
Gestionnaire.pluck(:email, :id).select { |e, _id| e.include?(" ") }.each do |_email, id|
Gestionnaire.pluck(:email, :id).filter { |e, _id| e.include?(" ") }.each do |_email, id|
Gestionnaire.find_by(id: id, current_sign_in_at: nil)&.destroy # ensure account was never used
end
User.pluck(:email, :id).select { |e, _id| e.include?(" ") }.each do |_email, id|
User.pluck(:email, :id).filter { |e, _id| e.include?(" ") }.each do |_email, id|
User.find_by(id: id, current_sign_in_at: nil)&.destroy # ensure account was never used
end
end

View file

@ -1,7 +1,7 @@
namespace :'2018_04_03_attestation_closed_mail_discrepancy' do
task mail_adminstrators: :environment do
Administrateur.includes(:procedures).find_each(batch_size: 10) do |admin|
procedures = admin.procedures.where(archived_at: nil).select { |p| p.closed_mail_template_attestation_inconsistency_state == :missing_tag }
procedures = admin.procedures.where(archived_at: nil).filter { |p| p.closed_mail_template_attestation_inconsistency_state == :missing_tag }
if procedures.any?
# Use `deliver_now` because the delayed job cannot find the `Mailers::AttestationClosedMailDiscrepancyMaile` class in production
Mailers::AttestationClosedMailDiscrepancyMailer.missing_attestation_tag_email(admin, procedures).deliver_now!

View file

@ -3,7 +3,7 @@ require Rails.root.join("lib", "tasks", "task_helper")
namespace :'2018_06_05_resend_attestations' do
task set: :environment do
procedure = Procedure.find(4247)
dossiers = procedure.dossiers.includes(:attestation).where(state: 'accepte').select do |d|
dossiers = procedure.dossiers.includes(:attestation).where(state: 'accepte').filter do |d|
d.processed_at < procedure.attestation_template.updated_at
end

View file

@ -14,7 +14,7 @@ namespace :'2018_06_06_users_for_admins_and_gestionnaires' do
.joins("INNER JOIN users ON #{table_name}.email = users.email")
.where(users: { confirmed_at: nil })
.to_a
.select(&block)
.filter(&block)
rake_puts "Sending emails to #{already_activated.count} #{table_name} that were already confirmed"

View file

@ -5,7 +5,7 @@ describe ApplicationController, type: :controller do
it 'is present' do
before_actions = ApplicationController
._process_action_callbacks
.find_all { |process_action_callbacks| process_action_callbacks.kind == :before }
.filter { |process_action_callbacks| process_action_callbacks.kind == :before }
.map(&:filter)
expect(before_actions).to include(:set_raven_context)

View file

@ -5,7 +5,7 @@ describe Instructeurs::InstructeurController, type: :controller do
it 'is present' do
before_actions = Instructeurs::InstructeurController
._process_action_callbacks
.find_all { |process_action_callbacks| process_action_callbacks.kind == :before }
.filter { |process_action_callbacks| process_action_callbacks.kind == :before }
.map(&:filter)
expect(before_actions).to include(:authenticate_instructeur!)

View file

@ -5,7 +5,7 @@ describe Instructeurs::ProceduresController, type: :controller do
it "is present" do
before_actions = Instructeurs::ProceduresController
._process_action_callbacks
.find_all { |process_action_callbacks| process_action_callbacks.kind == :before }
.filter { |process_action_callbacks| process_action_callbacks.kind == :before }
.map(&:filter)
expect(before_actions).to include(:ensure_ownership!)
@ -46,7 +46,7 @@ describe Instructeurs::ProceduresController, type: :controller do
it "is present" do
before_actions = Instructeurs::ProceduresController
._process_action_callbacks
.find_all { |process_action_callbacks| process_action_callbacks.kind == :before }
.filter { |process_action_callbacks| process_action_callbacks.kind == :before }
.map(&:filter)
expect(before_actions).to include(:redirect_to_avis_if_needed)

View file

@ -3,7 +3,7 @@ describe NewAdministrateur::AdministrateurController, type: :controller do
it 'is present' do
before_actions = NewAdministrateur::AdministrateurController
._process_action_callbacks
.find_all { |process_action_callbacks| process_action_callbacks.kind == :before }
.filter { |process_action_callbacks| process_action_callbacks.kind == :before }
.map(&:filter)
expect(before_actions).to include(:authenticate_administrateur!)

View file

@ -7,7 +7,7 @@ describe Users::DossiersController, type: :controller do
it 'are present' do
before_actions = Users::DossiersController
._process_action_callbacks
.find_all { |process_action_callbacks| process_action_callbacks.kind == :before }
.filter { |process_action_callbacks| process_action_callbacks.kind == :before }
.map(&:filter)
expect(before_actions).to include(:ensure_ownership!, :ensure_ownership_or_invitation!, :forbid_invite_submission!)

View file

@ -5,7 +5,7 @@ describe Users::UserController, type: :controller do
it 'is present' do
before_actions = Users::UserController
._process_action_callbacks
.find_all { |process_action_callbacks| process_action_callbacks.kind == :before }
.filter { |process_action_callbacks| process_action_callbacks.kind == :before }
.map(&:filter)
expect(before_actions).to include(:authenticate_user!)

View file

@ -149,12 +149,12 @@ describe AttestationTemplate, type: :model do
context 'and their value in the dossier are not nil' do
before do
dossier.champs
.select { |champ| champ.libelle == 'libelleA' }
.filter { |champ| champ.libelle == 'libelleA' }
.first
.update(value: 'libelle1')
dossier.champs
.select { |champ| champ.libelle == 'libelleB' }
.filter { |champ| champ.libelle == 'libelleB' }
.first
.update(value: 'libelle2')
end

View file

@ -99,12 +99,12 @@ describe TagsSubstitutionConcern, type: :model do
context 'and their value in the dossier are not nil' do
before do
dossier.champs
.select { |champ| champ.libelle == 'libelleA' }
.filter { |champ| champ.libelle == 'libelleA' }
.first
.update(value: 'libelle1')
dossier.champs
.select { |champ| champ.libelle == 'libelleB' }
.filter { |champ| champ.libelle == 'libelleB' }
.first
.update(value: 'libelle2')
end
@ -228,12 +228,12 @@ describe TagsSubstitutionConcern, type: :model do
context 'and its value in the dossier are not nil' do
before do
dossier.champs
.select { |champ| champ.type_champ == TypeDeChamp.type_champs.fetch(:date) }
.filter { |champ| champ.type_champ == TypeDeChamp.type_champs.fetch(:date) }
.first
.update(value: '2017-04-15')
dossier.champs
.select { |champ| champ.type_champ == TypeDeChamp.type_champs.fetch(:datetime) }
.filter { |champ| champ.type_champ == TypeDeChamp.type_champs.fetch(:datetime) }
.first
.update(value: '2017-09-13 09:00')
end

View file

@ -460,7 +460,7 @@ describe Dossier do
before do
(dossier.champs + dossier.champs_private)
.select { |c| c.libelle.match?(/^specified/) }
.filter { |c| c.libelle.match?(/^specified/) }
.each { |c| c.update_attribute(:value, "specified") }
end