diff --git a/app/controllers/administrateurs/procedures_controller.rb b/app/controllers/administrateurs/procedures_controller.rb index 2a40c1ce3..e48fe675d 100644 --- a/app/controllers/administrateurs/procedures_controller.rb +++ b/app/controllers/administrateurs/procedures_controller.rb @@ -381,7 +381,6 @@ module Administrateurs def all @filter = ProceduresFilter.new(current_administrateur, params) all_procedures = filter_procedures(@filter).map { |p| ProcedureDetail.new(p) } - respond_to do |format| format.html do all_procedures = Kaminari.paginate_array(all_procedures.to_a, offset: 0, limit: ITEMS_PER_PAGE, total_count: all_procedures.count) @@ -417,13 +416,14 @@ module Administrateurs procedures_result = procedures_result.where(hidden_at_as_template: nil) procedures_result = procedures_result.where(aasm_state: filter.statuses) if filter.statuses.present? procedures_result = procedures_result.where("tags @> ARRAY[?]::text[]", filter.tags) if filter.tags.present? + procedures_result = procedures_result.where(template: true) if filter.template? procedures_result = procedures_result.where('published_at >= ?', filter.from_publication_date) if filter.from_publication_date.present? procedures_result = procedures_result.where(service: service) if filter.service_siret.present? procedures_result = procedures_result.where(service: services) if services procedures_result = procedures_result.where('unaccent(libelle) ILIKE unaccent(?)', "%#{filter.libelle}%") if filter.libelle.present? procedures_sql = procedures_result.to_sql - sql = "select id, libelle, published_at, aasm_state, estimated_dossiers_count, count(administrateurs_procedures.administrateur_id) as admin_count from administrateurs_procedures inner join procedures on procedures.id = administrateurs_procedures.procedure_id where procedures.id in (#{procedures_sql}) group by procedures.id order by published_at desc" + sql = "select id, libelle, published_at, aasm_state, estimated_dossiers_count, template, count(administrateurs_procedures.administrateur_id) as admin_count from administrateurs_procedures inner join procedures on procedures.id = administrateurs_procedures.procedure_id where procedures.id in (#{procedures_sql}) group by procedures.id order by published_at desc" ActiveRecord::Base.connection.execute(sql) end diff --git a/app/controllers/manager/procedures_controller.rb b/app/controllers/manager/procedures_controller.rb index 5ecb49df7..6fb14e6d2 100644 --- a/app/controllers/manager/procedures_controller.rb +++ b/app/controllers/manager/procedures_controller.rb @@ -120,6 +120,14 @@ module Manager redirect_to manager_procedure_path(procedure) end + def update_template_status + if procedure.update(template_params) + redirect_to manager_procedure_path(procedure), notice: 'Le statut de modèle a été mis à jour.' + else + flash.alert = procedure.errors.full_messages.join(', ') + end + end + def import_data end @@ -176,6 +184,10 @@ module Manager params.require(:procedure).permit(:tags) end + def template_params + params.require(:procedure).permit(:template) + end + def tags_csv_file params[:tags_csv_file] end diff --git a/app/dashboards/procedure_dashboard.rb b/app/dashboards/procedure_dashboard.rb index 400db2e69..b7d889d37 100644 --- a/app/dashboards/procedure_dashboard.rb +++ b/app/dashboards/procedure_dashboard.rb @@ -47,7 +47,8 @@ class ProcedureDashboard < Administrate::BaseDashboard estimated_duration_visible: Field::Boolean, piece_justificative_multiple: Field::Boolean, replaced_by_procedure_id: Field::String, - tags: Field::Text + tags: Field::Text, + template: Field::Boolean }.freeze # COLLECTION_ATTRIBUTES @@ -79,6 +80,7 @@ class ProcedureDashboard < Administrate::BaseDashboard :libelle, :description, :tags, + :template, :lien_site_web, :organisation, :zones, diff --git a/app/models/procedure_detail.rb b/app/models/procedure_detail.rb index d3dbf3dfb..c590787b3 100644 --- a/app/models/procedure_detail.rb +++ b/app/models/procedure_detail.rb @@ -1,8 +1,8 @@ -ProcedureDetail = Struct.new(:id, :libelle, :published_at, :aasm_state, :estimated_dossiers_count, :admin_count, keyword_init: true) do +ProcedureDetail = Struct.new(:id, :libelle, :published_at, :aasm_state, :estimated_dossiers_count, :admin_count, :template, keyword_init: true) do include SpreadsheetArchitect def spreadsheet_columns - [:id, :libelle, :published_at, :aasm_state, :admin_count].map do |attribute| + [:id, :libelle, :published_at, :aasm_state, :admin_count, :template].map do |attribute| [I18n.t(attribute, scope: 'activerecord.attributes.procedure_export'), attribute] end end diff --git a/app/models/procedures_filter.rb b/app/models/procedures_filter.rb index 988003257..bc3213da7 100644 --- a/app/models/procedures_filter.rb +++ b/app/models/procedures_filter.rb @@ -8,7 +8,7 @@ class ProceduresFilter params[:zone_ids] = admin.zones.pluck(:id) if params[:zone_ids] == 'admin_default' - @params = params.permit(:page, :libelle, :email, :from_publication_date, :service_siret, :service_departement, tags: [], zone_ids: [], statuses: []) + @params = params.permit(:page, :libelle, :email, :from_publication_date, :service_siret, :service_departement, :template, tags: [], zone_ids: [], statuses: []) end def admin_zones @@ -35,6 +35,10 @@ class ProceduresFilter params[:tags].compact_blank.uniq if params[:tags].present? end + def template? + ActiveRecord::Type::Boolean.new.cast(params[:template]) + end + def service_siret params[:service_siret].presence end diff --git a/app/views/administrateurs/procedures/_detail.html.haml b/app/views/administrateurs/procedures/_detail.html.haml index beb4783c6..b2e89c4b7 100644 --- a/app/views/administrateurs/procedures/_detail.html.haml +++ b/app/views/administrateurs/procedures/_detail.html.haml @@ -6,7 +6,11 @@ = button_to detail_admin_procedure_path(procedure["id"]), method: :post, params:, title:, class: [icon, "fr-icon--sm fr-mr-1w fr-mb-1w fr-text-action-high--blue-france fr-btn fr-btn--tertiary-no-outline" ] do = title - %td= procedure.libelle + %td + - if procedure.template + %p.fr-badge.fr-badge--info.fr-badge--sm= "Modèle DS" + %br + = procedure.libelle %td= procedure.id %td= procedure.estimated_dossiers_count %td= procedure.administrateurs.count diff --git a/app/views/administrateurs/procedures/all.html.haml b/app/views/administrateurs/procedures/all.html.haml index 0484ce14e..41eea5eb6 100644 --- a/app/views/administrateurs/procedures/all.html.haml +++ b/app/views/administrateurs/procedures/all.html.haml @@ -41,6 +41,9 @@ - @filter.tags.each do |tag| = link_to tag, all_admin_procedures_path(@filter.without(:tags, tag)), class: 'fr-tag fr-tag--dismiss fr-mb-1w' - params[:tags].delete(tag) + - if @filter.template? + .selected-template.fr-mb-2w + = link_to "Modèle DS", all_admin_procedures_path(@filter.without(:template)), class: 'fr-tag fr-tag--dismiss fr-mb-1w' - if @filter.from_publication_date.present? .selected-from-publication-date.fr-mb-2w = link_to "Depuis #{l(@filter.from_publication_date)}", all_admin_procedures_path(@filter.without(:from_publication_date)), class: 'fr-tag fr-tag--dismiss fr-mb-1w' diff --git a/app/views/layouts/all.html.haml b/app/views/layouts/all.html.haml index bb581b79e..9fac8e547 100644 --- a/app/views/layouts/all.html.haml +++ b/app/views/layouts/all.html.haml @@ -100,6 +100,16 @@ - if @filter.tags.present? - @filter.tags.each do |tag| = f.hidden_field :tags, value: tag, multiple: true, id: "tag-#{tag.tr(' ', '_')}" + %li.fr-py-2w.fr-pl-2w{ 'data-controller': "expand" } + .fr-mb-1w + %button{ 'data-action': 'expand#toggle' } + %span.fr-icon-add-line.fr-icon--sm.fr-mr-1w.fr-text-action-high--blue-france{ 'aria-hidden': 'true', 'data-expand-target': 'icon' } + Démarches modèles + .fr-ml-1w.hidden{ 'data-expand-target': 'content' } + .fr-checkbox-group.fr-ml-2w.fr-py-1w + = f.check_box :template, class: 'fr-input' + = f.label :template, 'Modèle DS', class: 'fr-label' + .fr-col-9 = yield(:results) = render template: 'layouts/application' diff --git a/app/views/manager/procedures/show.html.erb b/app/views/manager/procedures/show.html.erb index 47a4c2b4b..f3ab35375 100644 --- a/app/views/manager/procedures/show.html.erb +++ b/app/views/manager/procedures/show.html.erb @@ -89,6 +89,7 @@ as well as a link to its edit page. <% end %> <% end %> <% if attribute.name == 'tags' %> + <%= form_for procedure, url: add_tags_manager_procedure_path(procedure), html: { class: 'form procedure-form__column--form fr-background-alt--blue-france mt-1' } do %> <%= hidden_field_tag 'procedure[tags]', nil %> <%= react_component("ComboMultiple", @@ -103,6 +104,12 @@ as well as a link to its edit page. <% end %> <% end %> + <% if attribute.name == 'template' %> + <%= form_for procedure, url: update_template_status_manager_procedure_path(procedure), html: { method: :patch, class: 'procedure-form__column--form fr-background-alt--blue-france mt-1', id: 'template-form' } do |f| %> + <%= f.label :template, 'Marquer comme modèle', for: 'template_checkbox' %> + <%= f.check_box :template, id: 'template_checkbox', onchange: 'this.form.submit();' %> + <% end %> + <% end %> <% end %>
diff --git a/config/locales/models/procedure/en.yml b/config/locales/models/procedure/en.yml index d02ff7c63..e999c6afa 100644 --- a/config/locales/models/procedure/en.yml +++ b/config/locales/models/procedure/en.yml @@ -51,6 +51,7 @@ en: published_at: Publication date aasm_state: Status admin_count: Administrators count + template: Is a template errors: models: procedure: diff --git a/config/locales/models/procedure/fr.yml b/config/locales/models/procedure/fr.yml index e88bb2fcc..5fa059494 100644 --- a/config/locales/models/procedure/fr.yml +++ b/config/locales/models/procedure/fr.yml @@ -57,6 +57,7 @@ fr: published_at: 'Date de publication' aasm_state: 'Statut' admin_count: 'Nb administrateurs' + template: 'Est un modèle' errors: models: procedure: diff --git a/config/routes.rb b/config/routes.rb index 77a8e89d9..bf3894cc0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,6 +24,7 @@ Rails.application.routes.draw do post 'add_administrateur_with_confirmation', on: :member post 'change_piece_justificative_template', on: :member patch 'add_tags', on: :member + patch 'update_template_status', on: :member get 'export_mail_brouillons', on: :member resources :confirmation_urls, only: :new resources :administrateur_confirmations, only: [:new, :create] diff --git a/db/migrate/20240126071130_add_is_template_to_procedures.rb b/db/migrate/20240126071130_add_is_template_to_procedures.rb new file mode 100644 index 000000000..72586bf35 --- /dev/null +++ b/db/migrate/20240126071130_add_is_template_to_procedures.rb @@ -0,0 +1,5 @@ +class AddIsTemplateToProcedures < ActiveRecord::Migration[7.0] + def change + add_column :procedures, :template, :boolean, default: false, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 66c256dd0..19982ebfc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_01_23_085909) do +ActiveRecord::Schema[7.0].define(version: 2024_01_26_071130) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -890,6 +890,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_01_23_085909) do t.bigint "service_id" t.jsonb "sva_svr", default: {}, null: false t.text "tags", default: [], array: true + t.boolean "template", default: false, null: false t.datetime "test_started_at", precision: nil t.datetime "unpublished_at", precision: nil t.datetime "updated_at", precision: nil, null: false diff --git a/spec/controllers/administrateurs/procedures_controller_spec.rb b/spec/controllers/administrateurs/procedures_controller_spec.rb index e76d035d5..3499c679a 100644 --- a/spec/controllers/administrateurs/procedures_controller_spec.rb +++ b/spec/controllers/administrateurs/procedures_controller_spec.rb @@ -259,6 +259,17 @@ describe Administrateurs::ProceduresController, type: :controller do end end + context 'with template procedures' do + let!(:template_procedure) { create(:procedure, :published, template: true) } + let!(:other_procedure) { create(:procedure, :published, template: false) } + + it 'identifies a procedure as a template' do + get :all, params: { template: '1' } + expect(assigns(:procedures).any? { |p| p.id == template_procedure.id }).to be_truthy + expect(assigns(:procedures).any? { |p| p.id == other_procedure.id }).to be_falsey + end + end + context 'with libelle search' do let!(:procedure1) { create(:procedure, :published, libelle: 'Demande de subvention') } let!(:procedure2) { create(:procedure, :published, libelle: "Fonds d'aide public « Prime Entrepreneurs des Quartiers »") }