From fdb4d15bcf6b44fd85c64a5c2536d809ab826fe6 Mon Sep 17 00:00:00 2001 From: Simon Lehericey Date: Fri, 9 Jun 2017 13:28:47 +0200 Subject: [PATCH] [Fix #196] AttestationTemplate: add edition --- app/assets/stylesheets/application.scss | 1 + .../attestation_template_edit.scss | 30 +++++ .../admin/attestation_templates_controller.rb | 49 ++++++++ app/models/attestation_template.rb | 36 ++++++ .../attestation_templates/edit.html.haml | 71 +++++++++++ ...estationtemplatescontroller_edit.html.haml | 1 + ...dmin_procedurescontroller_navbar.html.haml | 5 + config/routes.rb | 5 + .../attestation_templates_controller_spec.rb | 119 ++++++++++++++++++ 9 files changed, 317 insertions(+) create mode 100644 app/assets/stylesheets/attestation_template_edit.scss create mode 100644 app/controllers/admin/attestation_templates_controller.rb create mode 100644 app/views/admin/attestation_templates/edit.html.haml create mode 100644 app/views/layouts/left_panels/_left_panel_admin_attestationtemplatescontroller_edit.html.haml create mode 100644 spec/controllers/admin/attestation_templates_controller_spec.rb diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index aac24c497..5368690e7 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -44,6 +44,7 @@ // = require switch_menu // = require typeahead // = require users +// = require attestation_template_edit // = require_self // = require bootstrap-datepicker3 diff --git a/app/assets/stylesheets/attestation_template_edit.scss b/app/assets/stylesheets/attestation_template_edit.scss new file mode 100644 index 000000000..f6105a8f6 --- /dev/null +++ b/app/assets/stylesheets/attestation_template_edit.scss @@ -0,0 +1,30 @@ +#attestation-template-edit { + .notice { + margin-bottom: 30px; + } + + .image-upload { + display: flex; + align-items: center; + + input { + margin: 10px 0; + } + } + + .thumbnail { + width: 90px; + margin-right: 15px; + } + + .balises { + max-height: 180px; + overflow-y: scroll; + margin-bottom: 20px; + } + + .table { + border: 1px solid #DDDDDD; + margin-bottom: 0px; + } +} diff --git a/app/controllers/admin/attestation_templates_controller.rb b/app/controllers/admin/attestation_templates_controller.rb new file mode 100644 index 000000000..ca96f235e --- /dev/null +++ b/app/controllers/admin/attestation_templates_controller.rb @@ -0,0 +1,49 @@ +class Admin::AttestationTemplatesController < AdminController + before_action :retrieve_procedure + + def edit + @attestation_template = @procedure.attestation_template || AttestationTemplate.new(procedure: @procedure) + end + + def update + attestation_template = @procedure.attestation_template + + if attestation_template.update_attributes(activated_attestation_params) + flash.notice = "L'attestation a bien été modifiée" + else + flash.alert = attestation_template.errors.full_messages.join('
') + end + + redirect_to edit_admin_procedure_attestation_template_path(@procedure) + end + + def create + attestation_template = AttestationTemplate.new(activated_attestation_params.merge(procedure_id: @procedure.id)) + + if attestation_template.save + flash.notice = "L'attestation a bien été sauvegardée" + else + flash.alert = attestation_template.errors.full_messages.join('
') + end + + redirect_to edit_admin_procedure_attestation_template_path(@procedure) + end + + def disactivate + attestation_template = @procedure.attestation_template + attestation_template.activated = false + attestation_template.save + + flash.notice = "L'attestation a bien été désactivée" + + redirect_to edit_admin_procedure_attestation_template_path(@procedure) + end + + private + + def activated_attestation_params + params.require(:attestation_template) + .permit(:title, :body, :footer, :logo, :signature) + .merge(activated: true) + end +end diff --git a/app/models/attestation_template.rb b/app/models/attestation_template.rb index fd3ca4575..cd625e39d 100644 --- a/app/models/attestation_template.rb +++ b/app/models/attestation_template.rb @@ -10,6 +10,16 @@ class AttestationTemplate < ApplicationRecord FILE_MAX_SIZE_IN_MB = 0.5 + def tags + if procedure.for_individual? + identity_tags = individual_tags + else + identity_tags = entreprise_tags + etablissement_tags + end + + identity_tags + dossier_tags + procedure_type_de_champ_public_private_tags + end + def dup result = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated) @@ -37,4 +47,30 @@ class AttestationTemplate < ApplicationRecord errors.add(file_name, " : vous ne pouvez pas charger une image de plus de #{number_with_delimiter(FILE_MAX_SIZE_IN_MB, locale: :fr)} Mo") end end + + def procedure_type_de_champ_public_private_tags + (procedure.types_de_champ + procedure.types_de_champ_private) + .map { |tdc| { libelle: tdc.libelle, description: tdc.description } } + end + + def dossier_tags + [{ libelle: 'motivation', description: '', target: 'motivation' }] + end + + def individual_tags + [{ libelle: 'civilité', description: 'M., Mme' }, + { libelle: 'nom', description: "nom de l'usager" }, + { libelle: 'prénom', description: "prénom de l'usager" }] + end + + def entreprise_tags + [{ libelle: 'SIREN', description: '' }, + { libelle: 'numéro de TVA intracommunautaire', description: '' }, + { libelle: 'SIRET du siège social', description: '' }, + { libelle: 'raison sociale', description: '' }] + end + + def etablissement_tags + [{ libelle: 'adresse', description: '' }] + end end diff --git a/app/views/admin/attestation_templates/edit.html.haml b/app/views/admin/attestation_templates/edit.html.haml new file mode 100644 index 000000000..9b3069919 --- /dev/null +++ b/app/views/admin/attestation_templates/edit.html.haml @@ -0,0 +1,71 @@ +#attestation-template-edit.row.white-back + = form_for @attestation_template, url: admin_procedure_attestation_template_path do |f| + .row + .col-md-10 + %h1 + Attestation + - if @attestation_template.activated? + %small.text-success Activée + - else + %small Désactivée + + %p.notice Les attestation, si elles sont activées, sont délivrées par email aux usagers lorsque leurs dossiers sont acceptés, et sont également disponibles au téléchargement sur leur espace personnel. + + .image-upload + - if @attestation_template.logo.present? + = image_tag @attestation_template.logo.url, class: 'thumbnail' + .form-group + = f.label :logo, "Logo de l'attestation" + = f.file_field :logo, accept: 'image/png, image/jpg, image/jpeg' + %p.help-block + Fichier accepté : JPG / JPEG / PNG + %br + Dimensions conseillées : au minimum 500 px de largeur ou de hauteur, poids maximum : 0,5 Mo. + + .form-group + = f.label :title, 'Titre' + = f.text_field :title, class: 'form-control' + + .form-group + = f.label :body, 'Corps du document' + ~ f.text_area :body, class: 'form-control', rows: 10 + + .row + .col-md-12.balises + %table.table.table-striped + %tr + %th.col-md-3 + Balise + %th + Description + - @attestation_template.tags.each do |tag| + %tr + %td + = "--#{tag[:libelle]}--" + %td + = tag[:description] + + .image-upload + - if @attestation_template.signature.present? + = image_tag @attestation_template.signature.url, class: 'thumbnail' + .form-group + = f.label :signature, "Tampon de l'attestation" + = f.file_field :signature, accept: 'image/png, image/jpg, image/jpeg' + %p.help-block + Fichier accepté : JPG / JPEG / PNG + %br + Dimensions conseillées : au minimum 500 px de largeur ou de hauteur, poids maximum : 0,5 Mo. + + .form-group + = f.label :footer, 'Adresse en bas de page' + ~ f.text_area :footer, class: 'form-control', rows: 2, + placeholder: "Direction interministérielle du numérique et du système d'information et de communication de l'Etat (DINSIC)" + + .pull-right + - if @attestation_template.activated + %button.btn.btn-warning{ formaction: admin_procedure_attestation_template_disactivate_path } désactiver l'attestation + + - if @attestation_template.id.nil? || !@attestation_template.activated + %button.btn.btn-success Activer l'attestation + - else + %button.btn.btn-success Enregistrer diff --git a/app/views/layouts/left_panels/_left_panel_admin_attestationtemplatescontroller_edit.html.haml b/app/views/layouts/left_panels/_left_panel_admin_attestationtemplatescontroller_edit.html.haml new file mode 100644 index 000000000..832761229 --- /dev/null +++ b/app/views/layouts/left_panels/_left_panel_admin_attestationtemplatescontroller_edit.html.haml @@ -0,0 +1 @@ += render partial: 'layouts/left_panels/left_panel_admin_procedurescontroller_navbar', locals: { active: 'Attestation' } diff --git a/app/views/layouts/left_panels/_left_panel_admin_procedurescontroller_navbar.html.haml b/app/views/layouts/left_panels/_left_panel_admin_procedurescontroller_navbar.html.haml index 0bb60d458..60ee10888 100644 --- a/app/views/layouts/left_panels/_left_panel_admin_procedurescontroller_navbar.html.haml +++ b/app/views/layouts/left_panels/_left_panel_admin_procedurescontroller_navbar.html.haml @@ -45,6 +45,11 @@ .procedure-list-element{ class: ('active' if active == 'Prévisualisation') } Prévisualisation + - unless @procedure.locked? + %a#onglet-attestation{ href: url_for(edit_admin_procedure_attestation_template_path(@procedure)) } + .procedure-list-element{ class: ('active' if active == 'Attestation') } + Attestation + .split-hr-left diff --git a/config/routes.rb b/config/routes.rb index c09431946..0a2075ca8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -143,6 +143,11 @@ Rails.application.routes.draw do resource :accompagnateurs, only: [:show, :update] resource :previsualisation, only: [:show] + + resource :attestation_template, only: [:edit, :update, :create] + + post 'attestation_template/disactivate' => 'attestation_templates#disactivate' + patch 'attestation_template/disactivate' => 'attestation_templates#disactivate' end namespace :accompagnateurs do diff --git a/spec/controllers/admin/attestation_templates_controller_spec.rb b/spec/controllers/admin/attestation_templates_controller_spec.rb new file mode 100644 index 000000000..7f40c6ecc --- /dev/null +++ b/spec/controllers/admin/attestation_templates_controller_spec.rb @@ -0,0 +1,119 @@ +describe Admin::AttestationTemplatesController, type: :controller do + let!(:attestation_template) { create(:attestation_template) } + let(:admin) { create(:administrateur) } + let!(:procedure) { create :procedure, administrateur: admin, attestation_template: attestation_template } + let(:logo) { fixture_file_upload('spec/fixtures/white.png', 'image/png') } + let(:signature) { fixture_file_upload('spec/fixtures/black.png', 'image/png') } + + before do + sign_in admin + end + + describe 'GET #edit' do + before { get :edit, params: { procedure_id: procedure.id } } + + context 'if an attestation template exists on the procedure' do + it { expect(subject.status).to eq(200) } + it { expect(assigns(:attestation_template)).to eq(attestation_template) } + end + + context 'if an attestation template does not exist on the procedure' do + let(:attestation_template) { nil } + it { expect(subject.status).to eq(200) } + it { expect(assigns(:attestation_template).id).to be_nil } + it { expect(assigns(:attestation_template)).to be_an_instance_of(AttestationTemplate) } + end + end + + describe 'POST #create' do + let(:attestation_template) { nil } + let(:attestation_params) { { title: 't', body: 'b', footer: 'f' } } + + context 'nominal' do + before do + post :create, + params: { procedure_id: procedure.id, + attestation_template: attestation_params.merge(logo: logo, signature: signature) } + procedure.reload + end + + it { expect(procedure.attestation_template).to have_attributes(attestation_params) } + it { expect(procedure.attestation_template.activated).to be true } + it { expect(procedure.attestation_template.logo.read).to eq(logo.read) } + it { expect(procedure.attestation_template.signature.read).to eq(signature.read) } + it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) } + it { expect(flash.notice).to eq("L'attestation a bien été sauvegardée") } + + after { procedure.attestation_template.destroy } + end + + context 'when something wrong happens in the attestation template creation' do + before do + expect_any_instance_of(AttestationTemplate).to receive(:save) + .and_return(false) + expect_any_instance_of(AttestationTemplate).to receive(:errors) + .and_return(double(full_messages: ['nop'])) + + post :create, + params: { procedure_id: procedure.id, + attestation_template: attestation_params } + procedure.reload + end + + it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) } + it { expect(flash.alert).to eq('nop') } + end + end + + describe 'PATCH #update' do + let(:attestation_params) { { title: 't', body: 'b', footer: 'f' } } + let(:attestation_params_with_images) { attestation_params.merge(logo: logo, signature: signature) } + + context 'nominal' do + before do + patch :update, + params: { procedure_id: procedure.id, + attestation_template: attestation_params_with_images } + procedure.reload + end + + it { expect(procedure.attestation_template).to have_attributes(attestation_params) } + it { expect(procedure.attestation_template.logo.read).to eq(logo.read) } + it { expect(procedure.attestation_template.signature.read).to eq(signature.read) } + it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) } + it { expect(flash.notice).to eq("L'attestation a bien été modifiée") } + + after { procedure.attestation_template.destroy } + end + + context 'when something wrong happens in the attestation template creation' do + before do + expect_any_instance_of(AttestationTemplate).to receive(:update_attributes).and_return(false) + expect_any_instance_of(AttestationTemplate).to receive(:errors) + .and_return(double(full_messages: ['nop'])) + + patch :update, + params: { procedure_id: procedure.id, + attestation_template: attestation_params_with_images } + procedure.reload + end + + it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) } + it { expect(flash.alert).to eq('nop') } + end + end + + describe 'post #disactivate' do + context 'when the attestation_template is activated' do + let(:attestation_template) { create(:attestation_template, activated: true) } + + before do + post :disactivate, params: { procedure_id: procedure.id } + attestation_template.reload + end + + it { expect(attestation_template.activated).to be false } + it { expect(flash.notice).to eq("L'attestation a bien été désactivée") } + end + end +end