Skip to content

Commit

Permalink
Merge pull request #9939 from demarches-simplifiees/feat/9937
Browse files Browse the repository at this point in the history
ETQ Super-Admin, je souhaite marquer une démarche comme modèle
  • Loading branch information
kara22 authored Jan 31, 2024
2 parents 3c57b64 + 4674809 commit 17a471e
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/controllers/administrateurs/procedures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ def detail
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)
Expand Down Expand Up @@ -417,13 +416,14 @@ def filter_procedures(filter)
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

Expand Down
12 changes: 12 additions & 0 deletions app/controllers/manager/procedures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ def add_tags
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

Expand Down Expand Up @@ -176,6 +184,10 @@ def tags_params
params.require(:procedure).permit(:tags)
end

def template_params
params.require(:procedure).permit(:template)
end

def tags_csv_file
params[:tags_csv_file]
end
Expand Down
4 changes: 3 additions & 1 deletion app/dashboards/procedure_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -79,6 +80,7 @@ class ProcedureDashboard < Administrate::BaseDashboard
:libelle,
:description,
:tags,
:template,
:lien_site_web,
:organisation,
:zones,
Expand Down
4 changes: 2 additions & 2 deletions app/models/procedure_detail.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion app/models/procedures_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(admin, params)

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
Expand All @@ -35,6 +35,10 @@ def tags
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
Expand Down
6 changes: 5 additions & 1 deletion app/views/administrateurs/procedures/_detail.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/views/administrateurs/procedures/all.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
10 changes: 10 additions & 0 deletions app/views/layouts/all.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
7 changes: 7 additions & 0 deletions app/views/manager/procedures/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -103,6 +104,12 @@ as well as a link to its edit page.
<button class="mt-1">Ajouter des tags</button>
<% 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 %>
</dd>
<% end %>
<dd class="attribute-data">
Expand Down
1 change: 1 addition & 0 deletions config/locales/models/procedure/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ en:
published_at: Publication date
aasm_state: Status
admin_count: Administrators count
template: Is a template
errors:
models:
procedure:
Expand Down
1 change: 1 addition & 0 deletions config/locales/models/procedure/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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]
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240126071130_add_is_template_to_procedures.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddIsTemplateToProcedures < ActiveRecord::Migration[7.0]
def change
add_column :procedures, :template, :boolean, default: false, null: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -890,6 +890,7 @@
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
Expand Down
11 changes: 11 additions & 0 deletions spec/controllers/administrateurs/procedures_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,17 @@
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 »") }
Expand Down

0 comments on commit 17a471e

Please sign in to comment.