Merge pull request #5424 from betagouv/new_design_edit_attestation

New design page admin/attestation_templates/edit
This commit is contained in:
jpoulvel 2020-07-31 11:18:22 +02:00 committed by GitHub
commit bf94cc021b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 275 additions and 322 deletions

View file

@ -1,3 +1,4 @@
#attestation-template-edit {
.notice {
margin-bottom: 30px;

View file

@ -0,0 +1,56 @@
@import "colors";
@import "constants";
#attestation-template-edit {
.text-active {
color: $green;
}
.text-inactive {
color: $grey;
}
}
#tags-table {
margin-top: -20px;
margin-bottom: 40px;
.add-tag-title {
font-size: 18px;
margin-bottom: 16px;
}
.head {
padding: 10px 0px;
font-weight: bold;
display: flex;
}
.items {
max-height: 210px;
overflow-y: auto;
border-bottom: 1px solid $border-grey;
}
.item {
display: flex;
padding: 10px 0;
border-bottom: solid 1px $border-grey;
.tag {
font-weight: bold;
}
&:last-child {
border-bottom: none;
}
}
.tag {
width: 35%;
}
.description {
width: 65%;
}
}

View file

@ -317,4 +317,3 @@
.link {
color: $blue;
}

View file

@ -3,7 +3,7 @@
@import "constants";
.two-columns {
background-color: #FFFFFF;
background-color: $white;
@media (min-width: $two-columns-breakpoint) {
background: linear-gradient(to right, #FFFFFF 0%, #FFFFFF 50%, $light-grey 50%, $light-grey 100%);

View file

@ -51,6 +51,12 @@
opacity: 0.5;
margin-top: $default-spacer * 4;
margin-bottom: $default-spacer * 8;
.notice {
color: $dark-grey;
font-size: 16px;
font-weight: normal;
}
}
.procedure-form__actions {
@ -60,10 +66,20 @@
padding: $default-spacer $default-padding;
background: $light-grey;
border-top: 1px solid $border-grey;
justify-content: space-between;
align-items: center;
.button.send {
margin-left: auto;
}
.actions-left {
margin-right: auto;
}
.actions-right {
margin-left: auto;
}
}
.procedure-form__options-details {

View file

@ -1,101 +0,0 @@
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(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
def preview
attestation = (@procedure.attestation_template || AttestationTemplate.new)
attestation.assign_attributes(activated_attestation_params)
if attestation.valid?
@attestation = attestation.render_attributes_for(activated_attestation_params)
render 'admin/attestation_templates/show', formats: [:pdf]
else
flash.alert = attestation.errors.full_messages.join('<br>')
end
end
def delete_logo
@procedure.attestation_template.logo.purge_later
flash.notice = 'le logo a bien été supprimée'
redirect_to edit_admin_procedure_attestation_template_path(@procedure)
end
def delete_signature
@procedure.attestation_template.signature.purge_later
flash.notice = 'la signature a bien été supprimée'
redirect_to edit_admin_procedure_attestation_template_path(@procedure)
end
private
def activated_attestation_params
# cache result to avoid multiple uninterlaced computations
if @activated_attestation_params.nil?
@activated_attestation_params = params.require(:attestation_template)
.permit(:title, :body, :footer)
.merge(activated: true)
logo_file = params['attestation_template'].delete('logo')
signature_file = params['attestation_template'].delete('signature')
if logo_file.present?
@activated_attestation_params[:logo] = uninterlaced_png(logo_file)
end
if signature_file.present?
@activated_attestation_params[:signature] = uninterlaced_png(signature_file)
end
end
@activated_attestation_params
end
def uninterlaced_png(uploaded_file)
if uploaded_file&.content_type == 'image/png'
chunky_img = ChunkyPNG::Image.from_io(uploaded_file)
chunky_img.save(uploaded_file.tempfile.to_path, interlace: false)
uploaded_file.tempfile.reopen(uploaded_file.tempfile.to_path, 'rb')
end
uploaded_file
end
end

View file

@ -28,7 +28,7 @@ module Instructeurs
def apercu_attestation
@attestation = dossier.procedure.attestation_template.render_attributes_for(dossier: dossier)
render 'admin/attestation_templates/show', formats: [:pdf]
render 'new_administrateur/attestation_templates/show', formats: [:pdf]
end
def bilans_bdf

View file

@ -0,0 +1,70 @@
module NewAdministrateur
class AttestationTemplatesController < AdministrateurController
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(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 preview
attestation = @procedure.attestation_template || AttestationTemplate.new
@attestation = attestation.render_attributes_for({})
render 'new_administrateur/attestation_templates/show', formats: [:pdf]
end
private
def activated_attestation_params
# cache result to avoid multiple uninterlaced computations
if @activated_attestation_params.nil?
@activated_attestation_params = params.require(:attestation_template)
.permit(:title, :body, :footer, :activated, :logo, :signature)
logo_file = params['attestation_template'].delete('logo')
signature_file = params['attestation_template'].delete('signature')
if logo_file.present?
@activated_attestation_params[:logo] = uninterlaced_png(logo_file)
end
if signature_file.present?
@activated_attestation_params[:signature] = uninterlaced_png(signature_file)
end
end
@activated_attestation_params
end
def uninterlaced_png(uploaded_file)
if uploaded_file&.content_type == 'image/png'
chunky_img = ChunkyPNG::Image.from_io(uploaded_file)
chunky_img.save(uploaded_file.tempfile.to_path, interlace: false)
uploaded_file.tempfile.reopen(uploaded_file.tempfile.to_path, 'rb')
end
uploaded_file
end
end
end

View file

@ -0,0 +1,19 @@
module AttachmentUploadHelper
def image_upload_and_render(form, file, direct_upload = nil)
render 'shared/attachment/edit', {
form: form,
attached_file: file,
accept: 'image/png, image/jpg, image/jpeg',
user_can_destroy: true,
direct_upload: direct_upload
}
end
def text_upload_and_render(form, file)
render 'shared/attachment/edit', {
form: form,
attached_file: file,
user_can_destroy: true
}
end
end

View file

@ -86,8 +86,8 @@ class AttestationTemplate < ApplicationRecord
{
created_at: Time.zone.now,
title: dossier ? replace_tags(title, dossier) : params.fetch(:title, ''),
body: dossier ? replace_tags(body, dossier) : params.fetch(:body, ''),
title: dossier ? replace_tags(title, dossier) : params.fetch(:title, title),
body: dossier ? replace_tags(body, dossier) : params.fetch(:body, body),
footer: params.fetch(:footer, footer),
logo: params.fetch(:logo, logo.attached? ? logo : nil),
signature: params.fetch(:signature, signature.attached? ? signature : nil)
@ -110,7 +110,7 @@ class AttestationTemplate < ApplicationRecord
def build_pdf(dossier)
attestation = render_attributes_for(dossier: dossier)
action_view = ActionView::Base.new(ActionController::Base.view_paths, attestation: attestation)
attestation_view = action_view.render(file: 'admin/attestation_templates/show.pdf.prawn')
attestation_view = action_view.render(file: 'new_administrateur/attestation_templates/show', formats: [:pdf])
StringIO.new(attestation_view)
end

View file

@ -1,90 +0,0 @@
= render partial: 'admin/closed_mail_template_attestation_inconsistency_alert'
#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
Lattestation, si elle est activée, est émise au moment où un dossier est accepté.
%br
Lemail daccusé dacceptation envoyé à lusager comporte alors un lien vers lattestation ;
celle-ci est également disponible au téléchargement depuis lespace personnel de lusager.
.image-upload
- if @attestation_template.logo.attached?
= image_tag @attestation_template.logo_url, class: 'thumbnail'
.form-group
= f.label :logo, "Logo de l'attestation"
- if @attestation_template.logo.attached?
= link_to 'Supprimer le logo', admin_procedure_attestation_template_logo_path(@procedure), method: :delete
= 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
%code{ style: "white-space: pre-wrap;" }
= "--#{tag[:libelle]}--"
%td
= tag[:description]
.image-upload
- if @attestation_template.signature.attached?
= image_tag @attestation_template.signature_url, class: 'thumbnail'
.form-group
= f.label :signature, "Tampon de l'attestation"
- if @attestation_template.signature.attached?
= link_to 'Supprimer le tampon', admin_procedure_attestation_template_signature_path(@procedure), method: :delete
= 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, 'Pied de page'
= f.text_field :footer, class: 'form-control', maxlength: 190
- if @attestation_template.activated && @procedure.locked?
.row
.col-md-12
.pull-right
%p.help-block Lattestation ne peut plus être désactivée car la démarche a déjà été publiée.
%button.btn.btn-primary{ formaction: admin_procedure_attestation_template_preview_path, formtarget: '_blank' } Prévisualiser
.pull-right
- if @attestation_template.activated && !@procedure.locked?
%button.btn.btn-warning{ formaction: admin_procedure_attestation_template_disactivate_path } désactiver l'attestation
- if @attestation_template.new_record? || !@attestation_template.activated
%button.btn.btn-success Activer l'attestation
- else
- save_data = @procedure.locked? ? { confirm: "Attention: les modifications n'affecteront pas les attestations déjà délivrées." } : nil
%button.btn.btn-success{ data: save_data } Enregistrer

View file

@ -17,10 +17,7 @@
= form_for @avis, url: instructeur_avis_path(@avis.procedure, @avis), html: { class: 'form' } do |f|
= f.text_area :answer, rows: 3, placeholder: 'Votre avis', required: true
= render 'shared/attachment/edit',
{ form: f,
attached_file: @avis.piece_justificative_file,
user_can_destroy: true }
= text_upload_and_render f, @avis.piece_justificative_file
.flex.justify-between.align-baseline
%p.confidentiel.flex

View file

@ -7,10 +7,7 @@
= f.text_area :introduction, rows: 3, value: avis.introduction || 'Bonjour, merci de me donner votre avis sur ce dossier.', required: true
%p.tab-title Ajouter une pièce jointe
.form-group
= render 'shared/attachment/edit',
{ form: f,
attached_file: avis.introduction_file,
user_can_destroy: true }
= text_upload_and_render f, avis.introduction_file
- if linked_dossiers.present?
= f.check_box :invite_linked_dossiers, {}, true, false

View file

@ -0,0 +1 @@
%iframe{ src: admin_procedure_attestation_template_preview_path, width: '100%', height: '650px' }

View file

@ -0,0 +1,45 @@
= f.label :title do
Titre de l'attestation
%span.mandatory *
= f.text_field :title, class: 'form-control', placeholder: 'Titre de lattestation'
= f.label :body do
Corps du document
%span.mandatory *
= f.text_area :body, rows: '6', placeholder: 'Description de la démarche, destinataires, etc. ', class: 'form-control'
#tags-table
%h2.add-tag-title
Insérer une balise
%p.notice
Copiez-collez les balises ci-dessous pour afficher automatiquement linformation souhaitée.
.head
.tag Balise
.description Description
.items
- @attestation_template.tags.each do |tag|
.item
%code.tag{ style: "white-space: pre-wrap;" }
= "--#{tag[:libelle]}--"
.description
= tag[:description]
%h3.header-subsection Logo de l'attestation
= image_upload_and_render f, @attestation_template.logo, false
%p.notice
Formats acceptés : JPG / JPEG / PNG.
%br
Dimensions conseillées : au minimum 500 px de largeur ou de hauteur, poids maximum : 0,5 Mo.
%h3.header-subsection Tampon de l'attestation
= image_upload_and_render f, @attestation_template.signature, false
%p.notice
Formats acceptés : JPG / JPEG / PNG.
%br
Dimensions conseillées : au minimum 500 px de largeur ou de hauteur, poids maximum : 0,5 Mo.
= f.label :footer do
Pied de page
= f.text_field :footer, class: 'form-control', maxlength: 190

View file

@ -42,7 +42,7 @@
%span.toggle-switch-label.off Attestation désactivée
.actions-right
= link_to 'Annuler', edit_admin_procedure_attestation_template_path(id: @procedure), class: 'button', data: { confirm: 'Êtes-vous sûr de vouloir annuler les modifications effectuées ?'}
= link_to 'Annuler', edit_admin_procedure_attestation_template_path(id: @procedure), class: 'button', data: { confirm: 'Êtes-vous sûr de vouloir annuler les modifications effectuées ?'}
= f.button 'Enregistrer', class: 'button primary send'
.procedure-form__column--preview

View file

@ -14,11 +14,7 @@
= f.text_area :description, rows: '6', placeholder: 'Description de la démarche, destinataires, etc. ', class: 'form-control'
%h3.header-subsection Logo de la démarche
= render 'shared/attachment/edit',
{ form: f,
attached_file: @procedure.logo,
accept: 'image/png, image/jpg, image/jpeg',
user_can_destroy: true }
= image_upload_and_render f, @procedure.logo
- if !@procedure.locked?
%h3.header-subsection Conservation des données
@ -59,10 +55,7 @@
= f.text_field :cadre_juridique, class: 'form-control', placeholder: 'https://www.legifrance.gouv.fr/'
= f.label :deliberation, 'Importer le texte'
= render 'shared/attachment/edit',
{ form: f,
attached_file: @procedure.deliberation,
user_can_destroy: true }
= text_upload_and_render f, @procedure.deliberation
%h3.header-subsection Notice explicative de la démarche
@ -73,10 +66,7 @@
%p.notice
Formats acceptés : .doc, .odt, .pdf, .ppt, .pptx
- notice = @procedure.notice
= render 'shared/attachment/edit',
{ form: f,
attached_file: @procedure.notice,
user_can_destroy: true }
= text_upload_and_render f, @procedure.notice
- if !@procedure.locked?
%h3.header-subsection À qui sadresse ma démarche ?

View file

@ -16,8 +16,9 @@
= render partial: 'new_administrateur/procedures/informations', locals: { f: f }
.procedure-form__actions.sticky--bottom
= link_to 'Annuler', admin_procedure_path(id: @procedure), class: 'button', data: { confirm: 'Êtes-vous sûr de vouloir annuler les modifications effectuées ?'}
= f.button 'Enregistrer', class: 'button primary send'
.actions-right
= link_to 'Annuler', admin_procedure_path(id: @procedure), class: 'button', data: { confirm: 'Êtes-vous sûr de vouloir annuler les modifications effectuées ?'}
= f.button 'Enregistrer', class: 'button primary send'
.procedure-form__column--preview
.procedure-form__preview.sticky--top

View file

@ -16,8 +16,9 @@
= render partial: 'new_administrateur/procedures/informations', locals: { f: f }
.procedure-form__actions.sticky--bottom
= link_to 'Annuler', admin_procedures_path, class: 'button', data: { confirm: 'Êtes-vous sûr de vouloir annuler la création de cette démarche ?'}
= f.button 'Créer la démarche', class: 'button primary send'
.actions-right
= link_to 'Annuler', admin_procedures_path, class: 'button', data: { confirm: 'Êtes-vous sûr de vouloir annuler la création de cette démarche ?'}
= f.button 'Créer la démarche', class: 'button primary send'
.procedure-form__column--preview
.procedure-form__preview.sticky--top

View file

@ -5,6 +5,7 @@
- persisted = attachment && attachment.persisted?
- accept = defined?(accept) ? accept : nil
- user_can_destroy = defined?(user_can_destroy) ? user_can_destroy : false
- direct_upload = direct_upload != nil ? false : true
.attachment
- if defined?(template) && template.attached?
@ -34,5 +35,5 @@
= form.file_field attached_file.name,
class: "attachment-input attachment-input-#{attachment_id} #{'hidden' if persisted}",
accept: accept,
direct_upload: true,
direct_upload: direct_upload,
data: { 'auto-attach-url': auto_attach_url(form, form.object) }

View file

@ -196,17 +196,6 @@ Rails.application.routes.draw do
put 'publish' => 'procedures#publish', as: :publish
post 'transfer' => 'procedures#transfer', as: :transfer
put 'clone' => 'procedures#clone', as: :clone
resource :attestation_template, only: [:edit, :update, :create]
post 'attestation_template/disactivate' => 'attestation_templates#disactivate'
patch 'attestation_template/disactivate' => 'attestation_templates#disactivate'
post 'attestation_template/preview' => 'attestation_templates#preview'
patch 'attestation_template/preview' => 'attestation_templates#preview'
delete 'attestation_template/logo' => 'attestation_templates#delete_logo'
delete 'attestation_template/signature' => 'attestation_templates#delete_signature'
end
namespace :assigns do
@ -408,6 +397,12 @@ Rails.application.routes.draw do
resources :mail_templates, only: [] do
get 'preview', on: :member
end
resource :attestation_template, only: [:edit, :update, :create] do
get 'preview', on: :member
end
# ADDED TO ACCESS IT FROM THE IFRAME
get 'attestation_template/preview' => 'attestation_templates#preview'
end
resources :services, except: [:show] do

View file

@ -1,6 +1,6 @@
include ActionDispatch::TestProcess
describe Admin::AttestationTemplatesController, type: :controller do
describe NewAdministrateur::AttestationTemplatesController, type: :controller do
let!(:attestation_template) { create(:attestation_template) }
let(:admin) { create(:administrateur) }
let!(:procedure) { create :procedure, administrateur: admin, attestation_template: attestation_template }
@ -27,64 +27,28 @@ describe Admin::AttestationTemplatesController, type: :controller do
end
end
describe 'POST #preview' do
let(:upload_params) { { title: 't', body: 'b', footer: 'f' } }
describe 'GET #preview' do
let(:attestation_params) do
{ title: 't', body: 'b', footer: 'f' }
end
before do
post :preview,
get :preview,
params: {
procedure_id: procedure.id,
attestation_template: upload_params
procedure_id: procedure.id
}
procedure.reload
end
context 'with an interlaced png' do
let(:upload_params) { { logo: interlaced_logo } }
it 'displays the logo' do
expect(assigns(:attestation)[:logo].read).to eq(uninterlaced_logo.read)
end
it 'doesnt save the logo permanently yet' do
expect(procedure.attestation_template.logo.attached?).to be(false)
end
end
context 'with an invalid logo' do
let(:upload_params) { { logo: invalid_logo } }
it { expect(procedure.attestation_template.logo.attached?).to be false }
it { expect(flash.alert).to be_present }
end
context 'if an attestation template does not exist on the procedure' do
let(:attestation_template) { nil }
context 'with images' do
let(:upload_params) { { title: 't', body: 'b', footer: 'f', logo: interlaced_logo } }
it { expect(assigns(:attestation)).to include({ title: 't', body: 'b', footer: 'f' }) }
it { expect(assigns(:attestation)[:logo]).to be_present }
it_behaves_like 'rendering a PDF successfully'
end
context 'without images' do
let(:upload_params) { { title: 't', body: 'b', footer: 'f' } }
it { expect(assigns(:attestation)).to include(upload_params) }
it_behaves_like 'rendering a PDF successfully'
end
end
context 'if an attestation template exists on the procedure' do
after { procedure.attestation_template.destroy }
context 'with images' do
let!(:attestation_template) do
create(:attestation_template, logo: logo, signature: signature)
create(:attestation_template, attestation_params.merge(logo: logo, signature: signature))
end
it { expect(assigns(:attestation)).to include(upload_params) }
it { expect(assigns(:attestation)).to include(attestation_params) }
it { expect(assigns(:attestation)[:created_at]).to eq(Time.zone.now) }
it { expect(assigns(:attestation)[:logo].download).to eq(logo2.read) }
it { expect(assigns(:attestation)[:signature].download).to eq(signature2.read) }
@ -92,7 +56,11 @@ describe Admin::AttestationTemplatesController, type: :controller do
end
context 'without images' do
it { expect(assigns(:attestation)).to include(upload_params) }
let!(:attestation_template) do
create(:attestation_template, attestation_params)
end
it { expect(assigns(:attestation)).to include(attestation_params) }
it { expect(assigns(:attestation)[:created_at]).to eq(Time.zone.now) }
it { expect(assigns(:attestation)[:logo]).to eq(nil) }
it { expect(assigns(:attestation)[:signature]).to eq(nil) }
@ -119,7 +87,7 @@ describe Admin::AttestationTemplatesController, type: :controller do
describe 'POST #create' do
let(:attestation_template) { nil }
let(:attestation_params) { { title: 't', body: 'b', footer: 'f' } }
let(:attestation_params) { { title: 't', body: 'b', footer: 'f', activated: true } }
context 'nominal' do
before do
@ -142,12 +110,10 @@ describe Admin::AttestationTemplatesController, type: :controller do
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']))
let(:invalid_footer) { 'f' * 200 }
let(:attestation_params) { { title: 't', body: 'b', footer: invalid_footer, activated: true } }
before do
post :create,
params: {
procedure_id: procedure.id,
@ -157,7 +123,8 @@ describe Admin::AttestationTemplatesController, type: :controller do
end
it { expect(response).to redirect_to edit_admin_procedure_attestation_template_path(procedure) }
it { expect(flash.alert).to eq('nop') }
it { expect(flash.alert).to be_present }
it { expect(procedure.attestation_template).to be nil }
end
end
@ -202,18 +169,4 @@ describe Admin::AttestationTemplatesController, type: :controller do
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

View file

@ -5,12 +5,7 @@ describe 'shared/attachment/_update.html.haml', type: :view do
subject do
form_for(champ.dossier) do |form|
render 'shared/attachment/edit', {
form: form,
attached_file: attached_file,
accept: 'image/png',
user_can_destroy: user_can_destroy
}
view.image_upload_and_render form, attached_file
end
end
@ -24,7 +19,7 @@ describe 'shared/attachment/_update.html.haml', type: :view do
end
end
context 'when there is a attached file' do
context 'when there is an attached file' do
it 'renders a form field for uploading a file' do
expect(subject).to have_selector('input[type=file]:not(.hidden)')
end
@ -33,7 +28,7 @@ describe 'shared/attachment/_update.html.haml', type: :view do
expect(subject).not_to have_content(attached_file.filename.to_s)
end
it 'doesnt render action buttons' do
it 'does not render action buttons' do
expect(subject).not_to have_link('Remplacer')
expect(subject).not_to have_link('Supprimer')
end
@ -45,23 +40,30 @@ describe 'shared/attachment/_update.html.haml', type: :view do
expect(subject).to have_content(attached_file.filename.to_s)
end
it 'renders action buttons' do
expect(subject).to have_button('Remplacer')
end
it 'hides the form field by default' do
expect(subject).to have_selector('input[type=file].hidden')
end
it 'hides the Delete button by default' do
is_expected.not_to have_link('Supprimer')
end
context 'and the user can delete the attachment' do
let(:user_can_destroy) { true }
it { is_expected.to have_link('Supprimer') }
it 'shows the Delete button by default' do
is_expected.to have_link('Supprimer')
end
end
end
context 'when the user cannot destroy the attachment' do
subject do
form_for(champ.dossier) do |form|
render 'shared/attachment/edit', {
form: form,
attached_file: attached_file,
accept: 'image/png',
user_can_destroy: user_can_destroy
}
end
end
it 'hides the Delete button' do
is_expected.not_to have_link('Supprimer')
end
end
end