[Fix #196] AttestationTemplate: add edition

This commit is contained in:
Simon Lehericey 2017-06-09 13:28:47 +02:00
parent d7ff3d6139
commit fdb4d15bcf
9 changed files with 317 additions and 0 deletions

View file

@ -44,6 +44,7 @@
// = require switch_menu // = require switch_menu
// = require typeahead // = require typeahead
// = require users // = require users
// = require attestation_template_edit
// = require_self // = require_self
// = require bootstrap-datepicker3 // = require bootstrap-datepicker3

View file

@ -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;
}
}

View file

@ -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('<br>')
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('<br>')
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

View file

@ -10,6 +10,16 @@ class AttestationTemplate < ApplicationRecord
FILE_MAX_SIZE_IN_MB = 0.5 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 def dup
result = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated) 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") 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
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 end

View file

@ -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

View file

@ -0,0 +1 @@
= render partial: 'layouts/left_panels/left_panel_admin_procedurescontroller_navbar', locals: { active: 'Attestation' }

View file

@ -45,6 +45,11 @@
.procedure-list-element{ class: ('active' if active == 'Prévisualisation') } .procedure-list-element{ class: ('active' if active == 'Prévisualisation') }
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 .split-hr-left

View file

@ -143,6 +143,11 @@ Rails.application.routes.draw do
resource :accompagnateurs, only: [:show, :update] resource :accompagnateurs, only: [:show, :update]
resource :previsualisation, only: [:show] 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 end
namespace :accompagnateurs do namespace :accompagnateurs do

View file

@ -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