Merge pull request #1819 from betagouv/fix_932_template_for_pj

[fix #932] Let an adminstrateur upload a pj template
This commit is contained in:
gregoirenovel 2018-04-09 18:33:15 +02:00 committed by GitHub
commit 5942a661d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 92 additions and 3 deletions

View file

@ -28,6 +28,8 @@ function on_change_type_de_champ_select (){
parent.removeClass('header-section');
parent.children(".drop-down-list").removeClass('show-inline');
parent.children(".pj-template").removeClass('show-inline');
$('.mandatory', parent).show();
switch(this.value){
@ -38,6 +40,9 @@ function on_change_type_de_champ_select (){
case 'multiple_drop_down_list':
parent.children(".drop-down-list").addClass('show-inline');
break;
case 'piece_justificative':
parent.children(".pj-template").addClass('show-inline');
break;
case 'explication':
$('.mandatory', parent).hide();
break;

View file

@ -31,7 +31,8 @@
display: inline-block !important;
}
.form-group.drop-down-list {
.form-group.drop-down-list,
.form-group.pj-template {
display: none;
}

View file

@ -40,12 +40,15 @@ class TypeDeChamp < ApplicationRecord
end
has_one :drop_down_list
has_one_attached :piece_justificative_template
accepts_nested_attributes_for :drop_down_list
validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :type_champ, presence: true, allow_blank: false, allow_nil: false
before_validation :check_mandatory
before_save :remove_piece_justificative_template, if: -> { type_champ_changed? }
def params_for_champ
{
@ -77,4 +80,12 @@ class TypeDeChamp < ApplicationRecord
def self.type_champ_to_class_name(type_champ)
"TypesDeChamp::#{type_champ.classify}TypeDeChamp"
end
private
def remove_piece_justificative_template
if type_champ != 'piece_justificative' && piece_justificative_template.attached?
piece_justificative_template.purge_later
end
end
end

View file

@ -13,6 +13,7 @@ class TypesDeChampService
:type_champ,
:id,
:mandatory,
:piece_justificative_template,
drop_down_list_attributes: [:value, :id]
])

View file

@ -21,6 +21,23 @@
~ fff.text_area :value, class: 'form-control drop_down_list', placeholder: "Ecrire une valeur par ligne et --valeur-- pour un séparateur.", rows: 3, cols: 30
= fff.hidden_field :id
.form-group.pj-template{ class: (type_champ == 'piece_justificative') ? 'show-inline' : nil }
%h4 Modèle
- if type_champ == 'piece_justificative'
- template = ff.object.object.piece_justificative_template
- if !template.attached?
= ff.file_field :piece_justificative_template,
direct_upload: true
- else
= link_to template.filename.to_s, url_for(template), target: '_blank'
%br
Modifier :
= ff.file_field :piece_justificative_template,
direct_upload: true
- else
= ff.file_field :piece_justificative_template,
direct_upload: true
- hide_mandatory = (ff.object.object.private? || type_champ == 'explication')
.form-group.mandatory{ style: hide_mandatory ? 'visibility: hidden;' : nil }
@ -46,7 +63,10 @@
.form-group
%br &nbsp;
- if ff.object.id.nil?
= f.submit "Ajouter le champ", class: 'btn btn-success', id: @types_de_champ_facade.add_button_id
= f.button 'Ajouter le champ',
id: @types_de_champ_facade.add_button_id,
class: 'btn btn-success',
data: { disable_with: 'Envoi...' }
- else
= link_to("", @types_de_champ_facade.delete_url(ff), method: :delete, remote: true, id: "delete_type_de_champ_#{ff.object.id}", class: %w(form-control btn btn-danger fa fa-trash-o) )

View file

@ -1,6 +1,11 @@
= form_for [:admin, @procedure], url: @types_de_champ_facade.url, remote: true do |f|
= render partial: 'admin/types_de_champ/fields', locals: { types_de_champ: @types_de_champ_facade.types_de_champ, f: f }
= f.submit "Enregistrer", class: 'btn btn-success', id: :save
= f.button 'Enregistrer',
id: :save,
class: 'btn btn-success',
data: { disable_with: 'Envoi...' }
%hr
#new_type_de_champ
= render partial: 'admin/types_de_champ/fields', locals: { types_de_champ: @types_de_champ_facade.new_type_de_champ, f: f }

View file

@ -1,5 +1,10 @@
- pj = champ.piece_justificative_file
- if champ.type_de_champ.piece_justificative_template.attached?
%p.edit-pj-template.mb-1
Veuillez télécharger, remplir et joindre le
= link_to('le modèle suivant', url_for(champ.type_de_champ.piece_justificative_template), target: '_blank')
- if !pj.attached?
= form.file_field :piece_justificative_file,
id: "champs_#{champ.id}",

View file

@ -38,5 +38,46 @@ shared_examples 'type_de_champ_spec' do
it { is_expected.to allow_value('').for(:description) }
it { is_expected.to allow_value('blabla').for(:description) }
end
context 'remove piece_justificative_template' do
context 'when the tdc is piece_justificative' do
let(:template_double) { double('template', attached?: attached, purge_later: true) }
let(:tdc) { create(:type_de_champ_piece_justificative) }
subject { template_double }
before do
allow(tdc).to receive(:piece_justificative_template).and_return(template_double)
tdc.update_attribute('type_champ', target_type_champ)
end
context 'when the target type_champ is not pj' do
let(:target_type_champ) { 'text' }
context 'calls template.purge_later when a file is attached' do
let(:attached) { true }
it { is_expected.to have_received(:purge_later) }
end
context 'does not call template.purge_later when no file is attached' do
let(:attached) { false }
it { is_expected.not_to have_received(:purge_later) }
end
end
context 'when the target type_champ is pj' do
let(:target_type_champ) { 'piece_justificative' }
context 'does not call template.purge_later when a file is attached' do
let(:attached) { true }
it { is_expected.not_to have_received(:purge_later) }
end
end
end
end
end
end