Créé la liste de nouveaux tags en base et les associe aux démarches

This commit is contained in:
Kara Diaby 2024-10-09 09:34:09 +02:00
parent 953ccbcfb6
commit 2c85757289
No known key found for this signature in database
GPG key ID: C4D1B0CF9F24D759
2 changed files with 142 additions and 0 deletions

View file

@ -0,0 +1,110 @@
# frozen_string_literal: true
# this task is used to create the procedure_tags and backfill the procedures that have the tag in their tags array
module Maintenance
class CreateProcedureTagsTask < MaintenanceTasks::Task
include RunnableOnDeployConcern
include StatementsHelpersConcern
run_on_first_deploy
def collection
[
"Aap",
"Accompagnement",
"Action sociale",
"Adeli",
"Affectation",
"Agrément",
"Agriculture",
"agroécologie",
"Aide aux entreprises",
"Aide financière",
"Appel à manifestation d'intérêt",
"AMI",
"Animaux",
"Appel à projets",
"Association",
"Auto-école",
"Autorisation",
"Autorisation d'exercer",
"Bilan",
"Biodiversité",
"Candidature",
"Cerfa",
"Chasse",
"Cinéma",
"Cmg",
"Collectivé territoriale",
"Collège",
"Convention",
"Covid",
"Culture",
"Dérogation",
"Diplôme",
"Drone",
"DSDEN",
"Eau",
"Ecoles",
"Education",
"Elections",
"Energie",
"Enseignant",
"ENT",
"Environnement",
"Étrangers",
"Formation",
"FPRNM",
"Funéraire",
"Handicap",
"Hygiène",
"Industrie",
"innovation",
"Inscription",
"Logement",
"Lycée",
"Manifestation",
"Médicament",
"Micro-crèche",
"MODELE DS",
"Numérique",
"Permis",
"Pompiers",
"Préfecture",
"Professionels de santé",
"Recrutement",
"Rh",
"Santé",
"Scolaire",
"SDIS",
"Sécurité",
"Sécurité routière",
"Sécurité sociale",
"Séjour",
"Service civique",
"Subvention",
"Supérieur",
"Taxi",
"Télétravail",
"Tirs",
"Transition écologique",
"Transport",
"Travail",
"Université",
"Urbanisme"
]
end
def process(tag)
procedure_tag = ProcedureTag.find_or_create_by(name: tag)
Procedure.where("? ILIKE ANY(tags)", tag).find_each(batch_size: 500) do |procedure|
procedure.procedure_tags << procedure_tag unless procedure.procedure_tags.include?(procedure_tag)
end
end
def count
collection.size
end
end
end

View file

@ -0,0 +1,32 @@
# frozen_string_literal: true
require "rails_helper"
module Maintenance
RSpec.describe CreateProcedureTagsTask do
describe "#process" do
subject(:process) { described_class.new.process(tag) }
let(:tag) { "Accompagnement" }
let!(:procedure) { create(:procedure, tags: ["Accompagnement"]) }
it "creates the ProcedureTag if it does not exist" do
expect { process }.to change { ProcedureTag.count }.by(1)
expect(ProcedureTag.last.name).to eq(tag)
end
context "when the ProcedureTag already exists" do
let!(:procedure_tag) { ProcedureTag.create(name: tag) }
it "does not create a duplicate ProcedureTag" do
expect { process }.not_to change { ProcedureTag.count }
end
end
it "associates procedures with the ProcedureTag" do
process
expect(procedure.reload.procedure_tags.map(&:name)).to include(tag)
end
end
end
end