diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index 99b0152db..b29493517 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -108,7 +108,7 @@ module Administrateurs flash.now.alert = @procedure.errors.full_messages render 'new' else - @procedure.create_generic_procedure_labels + @procedure.create_generic_labels flash.notice = 'Démarche enregistrée.' current_administrateur.instructeur.assign_to_procedure(@procedure) diff --git a/app/controllers/instructeurs/dossiers_controller.rb b/app/controllers/instructeurs/dossiers_controller.rb index 07193eac2..a5ac2c0db 100644 --- a/app/controllers/instructeurs/dossiers_controller.rb +++ b/app/controllers/instructeurs/dossiers_controller.rb @@ -64,14 +64,14 @@ module Instructeurs end def dossier_labels - labels = params[:procedure_label_id]&.map(&:to_i) || [] + labels = params[:label_id]&.map(&:to_i) || [] @dossier = dossier - labels.each { |params_label| DossierLabel.find_or_create_by(dossier_id: @dossier.id, procedure_label_id: params_label) } + labels.each { |params_label| DossierLabel.find_or_create_by(dossier_id: @dossier.id, label_id: params_label) } - all_labels = DossierLabel.where(dossier_id: @dossier.id).pluck(:procedure_label_id) + all_labels = DossierLabel.where(dossier_id: @dossier.id).pluck(:label_id) - (all_labels - labels).each { DossierLabel.find_by(dossier_id: @dossier.id, procedure_label_id: _1).destroy } + (all_labels - labels).each { DossierLabel.find_by(dossier_id: @dossier.id, label_id: _1).destroy } render :change_state end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index ae8e46e34..a07393b05 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -133,7 +133,7 @@ class Dossier < ApplicationRecord belongs_to :transfer, class_name: 'DossierTransfer', foreign_key: 'dossier_transfer_id', optional: true, inverse_of: :dossiers has_many :transfer_logs, class_name: 'DossierTransferLog', dependent: :destroy has_many :dossier_labels, dependent: :destroy - has_many :procedure_labels, through: :dossier_labels + has_many :labels, through: :dossier_labels after_destroy_commit :log_destroy diff --git a/app/models/dossier_label.rb b/app/models/dossier_label.rb index dfbf47fe0..6e9cc96f5 100644 --- a/app/models/dossier_label.rb +++ b/app/models/dossier_label.rb @@ -2,5 +2,5 @@ class DossierLabel < ApplicationRecord belongs_to :dossier - belongs_to :procedure_label + belongs_to :label end diff --git a/app/models/procedure_label.rb b/app/models/label.rb similarity index 89% rename from app/models/procedure_label.rb rename to app/models/label.rb index d8ab558f4..6c4e73e13 100644 --- a/app/models/procedure_label.rb +++ b/app/models/label.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class ProcedureLabel < ApplicationRecord +class Label < ApplicationRecord belongs_to :procedure has_many :dossier_labels, dependent: :destroy diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 32d246bb5..3aebfec72 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -60,7 +60,7 @@ class Procedure < ApplicationRecord has_and_belongs_to_many :procedure_tags has_many :bulk_messages, dependent: :destroy - has_many :procedure_labels, dependent: :destroy + has_many :labels, dependent: :destroy def active_dossier_submitted_message published_dossier_submitted_message || draft_dossier_submitted_message @@ -529,7 +529,7 @@ class Procedure < ApplicationRecord procedure.closing_notification_en_cours = false procedure.template = false procedure.monavis_embed = nil - procedure.procedure_labels = procedure_labels.map(&:dup) + procedure.labels = labels.map(&:dup) if !procedure.valid? procedure.errors.attribute_names.each do |attribute| @@ -937,9 +937,9 @@ class Procedure < ApplicationRecord end end - def create_generic_procedure_labels - ProcedureLabel::GENERIC_LABELS.each do |label| - ProcedureLabel.create(name: label[:name], color: label[:color], procedure_id: self.id) + def create_generic_labels + Label::GENERIC_LABELS.each do |label| + Label.create(name: label[:name], color: label[:color], procedure_id: self.id) end end diff --git a/app/services/dossier_filter_service.rb b/app/services/dossier_filter_service.rb index ac6148a99..54e700965 100644 --- a/app/services/dossier_filter_service.rb +++ b/app/services/dossier_filter_service.rb @@ -131,7 +131,7 @@ class DossierFilterService assert_supported_column(table, column) dossiers .joins(:dossier_labels) - .where(dossier_labels: { procedure_label_id: values }) + .where(dossier_labels: { label_id: values }) when 'groupe_instructeur' assert_supported_column(table, column) diff --git a/app/services/dossier_projection_service.rb b/app/services/dossier_projection_service.rb index abe79ae32..a79aeacba 100644 --- a/app/services/dossier_projection_service.rb +++ b/app/services/dossier_projection_service.rb @@ -128,9 +128,9 @@ class DossierProjectionService id_value_h = DossierLabel - .includes(:procedure_label) + .includes(:label) .where(dossier_id: dossiers_ids) - .pluck('dossier_id, procedure_labels.name, procedure_labels.color') + .pluck('dossier_id, labels.name, labels.color') .group_by { |dossier_id, _| dossier_id } fields[0][:id_value_h] = id_value_h.transform_values { |v| { value: v, type: :label } } diff --git a/app/tasks/maintenance/backfill_procedure_labels_for_procedures_task.rb b/app/tasks/maintenance/backfill_labels_for_procedures_task.rb similarity index 60% rename from app/tasks/maintenance/backfill_procedure_labels_for_procedures_task.rb rename to app/tasks/maintenance/backfill_labels_for_procedures_task.rb index d6aeab298..b207454f1 100644 --- a/app/tasks/maintenance/backfill_procedure_labels_for_procedures_task.rb +++ b/app/tasks/maintenance/backfill_labels_for_procedures_task.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Maintenance - class BackfillProcedureLabelsForProceduresTask < MaintenanceTasks::Task + class BackfillLabelsForProceduresTask < MaintenanceTasks::Task # Cette tâche permet de créer un jeu de labels génériques pour les anciennes procédures # Plus d'informations sur l'implémentation des labels ici : https://github.com/demarches-simplifiees/demarches-simplifiees.fr/issues/9787 # 2024-10-15 @@ -12,13 +12,13 @@ module Maintenance def collection Procedure - .includes(:procedure_labels) - .where(procedure_labels: { id: nil }) + .includes(:labels) + .where(labels: { id: nil }) end def process(procedure) - ProcedureLabel::GENERIC_LABELS.each do |label| - ProcedureLabel.create(name: label[:name], color: label[:color], procedure_id: procedure.id) + Label::GENERIC_LABELS.each do |label| + Label.create(name: label[:name], color: label[:color], procedure_id: procedure.id) end end end diff --git a/app/views/instructeurs/dossiers/_header_top.html.haml b/app/views/instructeurs/dossiers/_header_top.html.haml index 44a117c15..c0f406abb 100644 --- a/app/views/instructeurs/dossiers/_header_top.html.haml +++ b/app/views/instructeurs/dossiers/_header_top.html.haml @@ -26,22 +26,22 @@ %p.fr-mb-1w %small L’usager a supprimé son compte. Vous pouvez archiver puis supprimer le dossier. - - if dossier.procedure.procedure_labels.present? + - if dossier.procedure.labels.present? .fr-mb-3w - - if dossier.procedure_labels.present? - - dossier.procedure_labels.each do |label| + - if dossier.labels.present? + - dossier.labels.each do |label| = tag_label(label.name, label.color) = render Dropdown::MenuComponent.new(wrapper: :span, button_options: { class: ['fr-btn--sm fr-btn--tertiary-no-outline fr-pl-1v']}, menu_options: { class: ['dropdown-label left-aligned'] }) do |menu| - - if dossier.procedure_labels.empty? + - if dossier.labels.empty? - menu.with_button_inner_html do Ajouter un label - menu.with_form do = form_with(url: dossier_labels_instructeur_dossier_path(dossier_id: dossier.id, procedure_id: dossier.procedure.id), method: :post, class: 'fr-p-3w', data: { controller: 'autosubmit', turbo: 'true' }) do |f| %fieldset.fr-fieldset.fr-mt-2w.fr-mb-0 - = f.collection_check_boxes :procedure_label_id, dossier.procedure.procedure_labels, :id, :name, include_hidden: false do |b| + = f.collection_check_boxes :label_id, dossier.procedure.labels, :id, :name, include_hidden: false do |b| .fr-fieldset__element .fr-checkbox-group.fr-checkbox-group--sm.fr-mb-1w - = b.check_box(checked: DossierLabel.find_by(dossier_id: dossier.id, procedure_label_id: b.value).present? ) + = b.check_box(checked: DossierLabel.find_by(dossier_id: dossier.id, label_id: b.value).present? ) = b.label(class: "fr-label fr-tag fr-tag--sm fr-tag--#{b.object.color}") { b.text } diff --git a/db/migrate/20240924151336_create_procedure_labels.rb b/db/migrate/20240924151336_create_labels.rb similarity index 64% rename from db/migrate/20240924151336_create_procedure_labels.rb rename to db/migrate/20240924151336_create_labels.rb index 0d916fd25..b6897db2a 100644 --- a/db/migrate/20240924151336_create_procedure_labels.rb +++ b/db/migrate/20240924151336_create_labels.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -class CreateProcedureLabels < ActiveRecord::Migration[7.0] +class CreateLabels < ActiveRecord::Migration[7.0] def change - create_table :procedure_labels do |t| + create_table :labels do |t| t.string :name t.string :color t.references :procedure, null: false, foreign_key: true diff --git a/db/migrate/20240925133719_create_dossier_labels.rb b/db/migrate/20240925133719_create_dossier_labels.rb index 0fab9bfc6..5b4ed3ac9 100644 --- a/db/migrate/20240925133719_create_dossier_labels.rb +++ b/db/migrate/20240925133719_create_dossier_labels.rb @@ -4,7 +4,7 @@ class CreateDossierLabels < ActiveRecord::Migration[7.0] def change create_table :dossier_labels do |t| t.references :dossier, null: false, foreign_key: true - t.references :procedure_label, null: false, foreign_key: true + t.references :label, null: false, foreign_key: true t.timestamps end diff --git a/db/schema.rb b/db/schema.rb index 0235b3b0f..bdb19f2c4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -419,10 +419,10 @@ ActiveRecord::Schema[7.0].define(version: 2024_10_14_084333) do create_table "dossier_labels", force: :cascade do |t| t.datetime "created_at", null: false t.bigint "dossier_id", null: false - t.bigint "procedure_label_id", null: false + t.bigint "label_id", null: false t.datetime "updated_at", null: false t.index ["dossier_id"], name: "index_dossier_labels_on_dossier_id" - t.index ["procedure_label_id"], name: "index_dossier_labels_on_procedure_label_id" + t.index ["label_id"], name: "index_dossier_labels_on_label_id" end create_table "dossier_operation_logs", force: :cascade do |t| @@ -829,6 +829,15 @@ ActiveRecord::Schema[7.0].define(version: 2024_10_14_084333) do t.index ["email", "dossier_id"], name: "index_invites_on_email_and_dossier_id", unique: true end + create_table "labels", force: :cascade do |t| + t.string "color" + t.datetime "created_at", null: false + t.string "name" + t.bigint "procedure_id", null: false + t.datetime "updated_at", null: false + t.index ["procedure_id"], name: "index_labels_on_procedure_id" + end + create_table "maintenance_tasks_runs", force: :cascade do |t| t.text "arguments" t.text "backtrace" @@ -878,15 +887,6 @@ ActiveRecord::Schema[7.0].define(version: 2024_10_14_084333) do t.index ["from"], name: "index_path_rewrites_on_from", unique: true end - create_table "procedure_labels", force: :cascade do |t| - t.string "color" - t.datetime "created_at", null: false - t.string "name" - t.bigint "procedure_id", null: false - t.datetime "updated_at", null: false - t.index ["procedure_id"], name: "index_procedure_labels_on_procedure_id" - end - create_table "procedure_presentations", id: :serial, force: :cascade do |t| t.jsonb "a_suivre_filters", default: [], null: false, array: true t.jsonb "archives_filters", default: [], null: false, array: true @@ -1299,7 +1299,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_10_14_084333) do add_foreign_key "dossier_corrections", "commentaires" add_foreign_key "dossier_corrections", "dossiers" add_foreign_key "dossier_labels", "dossiers" - add_foreign_key "dossier_labels", "procedure_labels" + add_foreign_key "dossier_labels", "labels" add_foreign_key "dossier_operation_logs", "bill_signatures" add_foreign_key "dossier_transfer_logs", "dossiers" add_foreign_key "dossiers", "batch_operations" @@ -1320,8 +1320,8 @@ ActiveRecord::Schema[7.0].define(version: 2024_10_14_084333) do add_foreign_key "groupe_instructeurs", "procedures" add_foreign_key "initiated_mails", "procedures" add_foreign_key "instructeurs", "users" + add_foreign_key "labels", "procedures" add_foreign_key "merge_logs", "users" - add_foreign_key "procedure_labels", "procedures" add_foreign_key "procedure_presentations", "assign_tos" add_foreign_key "procedure_revision_types_de_champ", "procedure_revision_types_de_champ", column: "parent_id" add_foreign_key "procedure_revision_types_de_champ", "procedure_revisions", column: "revision_id" diff --git a/spec/controllers/administrateurs/procedures_controller_spec.rb b/spec/controllers/administrateurs/procedures_controller_spec.rb index 89184cf56..03e87e346 100644 --- a/spec/controllers/administrateurs/procedures_controller_spec.rb +++ b/spec/controllers/administrateurs/procedures_controller_spec.rb @@ -515,8 +515,8 @@ describe Administrateurs::ProceduresController, type: :controller do end it "create generic labels" do - expect(subject.procedure_labels.size).to eq(3) - expect(subject.procedure_labels.first.name).to eq('à relancer') + expect(subject.labels.size).to eq(3) + expect(subject.labels.first.name).to eq('à relancer') end end @@ -684,9 +684,9 @@ describe Administrateurs::ProceduresController, type: :controller do expect(Procedure.last.cloned_from_library).to be_falsey expect(Procedure.last.notice.attached?).to be_truthy expect(Procedure.last.deliberation.attached?).to be_truthy - expect(Procedure.last.procedure_labels.present?).to be_truthy - expect(Procedure.last.procedure_labels.first.procedure_id).to eq(Procedure.last.id) - expect(procedure.procedure_labels.first.procedure_id).to eq(procedure.id) + expect(Procedure.last.labels.present?).to be_truthy + expect(Procedure.last.labels.first.procedure_id).to eq(Procedure.last.id) + expect(procedure.labels.first.procedure_id).to eq(procedure.id) expect(flash[:notice]).to have_content 'Démarche clonée. Pensez à vérifier la présentation et choisir le service à laquelle cette démarche est associée.' end @@ -709,7 +709,7 @@ describe Administrateurs::ProceduresController, type: :controller do it 'creates a new procedure and redirect to it' do expect(response).to redirect_to admin_procedure_path(id: Procedure.last.id) - expect(Procedure.last.procedure_labels.present?).to be_truthy + expect(Procedure.last.labels.present?).to be_truthy expect(flash[:notice]).to have_content 'Démarche clonée. Pensez à vérifier la présentation et choisir le service à laquelle cette démarche est associée.' end end diff --git a/spec/controllers/instructeurs/dossiers_controller_spec.rb b/spec/controllers/instructeurs/dossiers_controller_spec.rb index 7ecddb561..c543ba69b 100644 --- a/spec/controllers/instructeurs/dossiers_controller_spec.rb +++ b/spec/controllers/instructeurs/dossiers_controller_spec.rb @@ -1523,7 +1523,7 @@ describe Instructeurs::DossiersController, type: :controller do let(:procedure) { create(:procedure, :with_labels, instructeurs: [instructeur]) } let!(:dossier) { create(:dossier, :en_construction, procedure:) } context 'it create dossier labels' do - subject { post :dossier_labels, params: { procedure_id: procedure.id, dossier_id: dossier.id, procedure_label_id: [ProcedureLabel.first.id] }, format: :turbo_stream } + subject { post :dossier_labels, params: { procedure_id: procedure.id, dossier_id: dossier.id, label_id: [Label.first.id] }, format: :turbo_stream } it 'works' do subject dossier.reload @@ -1536,10 +1536,10 @@ describe Instructeurs::DossiersController, type: :controller do context 'it remove dossier labels' do before do - DossierLabel.create(dossier_id: dossier.id, procedure_label_id: dossier.procedure.procedure_labels.first.id) + DossierLabel.create(dossier_id: dossier.id, label_id: dossier.procedure.labels.first.id) end - subject { post :dossier_labels, params: { procedure_id: procedure.id, dossier_id: dossier.id, procedure_label_id: [] }, format: :turbo_stream } + subject { post :dossier_labels, params: { procedure_id: procedure.id, dossier_id: dossier.id, label_id: [] }, format: :turbo_stream } it 'works' do expect(dossier.dossier_labels.count).to eq(1) diff --git a/spec/controllers/instructeurs/procedures_controller_spec.rb b/spec/controllers/instructeurs/procedures_controller_spec.rb index c7e4f932f..c14575ef5 100644 --- a/spec/controllers/instructeurs/procedures_controller_spec.rb +++ b/spec/controllers/instructeurs/procedures_controller_spec.rb @@ -643,19 +643,19 @@ describe Instructeurs::ProceduresController, type: :controller do let!(:dossier) { create(:dossier, :en_construction, procedure:, groupe_instructeur: gi_2) } let!(:dossier_2) { create(:dossier, :en_construction, procedure:, groupe_instructeur: gi_2) } let(:statut) { 'tous' } - let(:procedure_label_id) { procedure.find_column(label: 'Labels') } + let(:label_id) { procedure.find_column(label: 'Labels') } let!(:procedure_presentation) do ProcedurePresentation.create!(assign_to: AssignTo.first) end render_views before do - DossierLabel.create(dossier_id: dossier.id, procedure_label_id: dossier.procedure.procedure_labels.first.id) - DossierLabel.create(dossier_id: dossier.id, procedure_label_id: dossier.procedure.procedure_labels.second.id) - DossierLabel.create(dossier_id: dossier_2.id, procedure_label_id: dossier.procedure.procedure_labels.last.id) + DossierLabel.create(dossier_id: dossier.id, label_id: dossier.procedure.labels.first.id) + DossierLabel.create(dossier_id: dossier.id, label_id: dossier.procedure.labels.second.id) + DossierLabel.create(dossier_id: dossier_2.id, label_id: dossier.procedure.labels.last.id) procedure_presentation.update(displayed_columns: [ - procedure_label_id.id + label_id.id ]) subject diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 9e0e4cfaa..672aa4d2a 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -294,7 +294,7 @@ FactoryBot.define do trait :with_labels do after(:create) do |procedure, _evaluator| - procedure.create_generic_procedure_labels + procedure.create_generic_labels end end end diff --git a/spec/models/concerns/columns_concern_spec.rb b/spec/models/concerns/columns_concern_spec.rb index e11514646..1f6f0b98b 100644 --- a/spec/models/concerns/columns_concern_spec.rb +++ b/spec/models/concerns/columns_concern_spec.rb @@ -51,7 +51,7 @@ describe ColumnsConcern do { label: 'Groupe instructeur', table: 'groupe_instructeur', column: 'id', displayable: true, type: :enum, scope: '', value_column: :value, filterable: true }, { label: 'Avis oui/non', table: 'avis', column: 'question_answer', displayable: true, type: :text, scope: '', value_column: :value, filterable: false }, { label: 'France connecté ?', table: 'self', column: 'user_from_france_connect?', displayable: false, type: :text, scope: '', value_column: :value, filterable: false }, - { label: "Labels", table: "dossier_labels", column: "procedure_label_id", displayable: true, scope: '', value_column: :value, filterable: true }, + { label: "Labels", table: "dossier_labels", column: "label_id", displayable: true, scope: '', value_column: :value, filterable: true }, { label: 'SIREN', table: 'etablissement', column: 'entreprise_siren', displayable: true, type: :text, scope: '', value_column: :value, filterable: true }, { label: 'Forme juridique', table: 'etablissement', column: 'entreprise_forme_juridique', displayable: true, type: :text, scope: '', value_column: :value, filterable: true }, { label: 'Nom commercial', table: 'etablissement', column: 'entreprise_nom_commercial', displayable: true, type: :text, scope: '', value_column: :value, filterable: true }, diff --git a/spec/system/instructeurs/procedure_filters_spec.rb b/spec/system/instructeurs/procedure_filters_spec.rb index fb8fec122..1cd23ea13 100644 --- a/spec/system/instructeurs/procedure_filters_spec.rb +++ b/spec/system/instructeurs/procedure_filters_spec.rb @@ -174,8 +174,8 @@ describe "procedure filters" do describe 'dossier labels' do scenario "should be able to filter by dossier labels", js: true do - DossierLabel.create!(dossier_id: new_unfollow_dossier.id, procedure_label_id: procedure.procedure_labels.first.id) - add_filter('Labels', procedure.procedure_labels.first.name, type: :enum) + DossierLabel.create!(dossier_id: new_unfollow_dossier.id, label_id: procedure.labels.first.id) + add_filter('Labels', procedure.labels.first.name, type: :enum) expect(page).to have_link(new_unfollow_dossier.id.to_s) expect(page).not_to have_link(new_unfollow_dossier_2.id.to_s) end diff --git a/spec/views/instructeur/dossiers/show.html.haml_spec.rb b/spec/views/instructeur/dossiers/show.html.haml_spec.rb index c277e28e9..056ab6622 100644 --- a/spec/views/instructeur/dossiers/show.html.haml_spec.rb +++ b/spec/views/instructeur/dossiers/show.html.haml_spec.rb @@ -244,7 +244,7 @@ describe 'instructeurs/dossiers/show', type: :view do context "Dossier with labels" do before do - DossierLabel.create(dossier_id: dossier.id, procedure_label_id: dossier.procedure.procedure_labels.first.id) + DossierLabel.create(dossier_id: dossier.id, label_id: dossier.procedure.labels.first.id) end it 'displays labels and button without text to add label' do