Merge pull request #75 from sgmap/champs_service_refactor
Champs service refactor + un peu de DescriptionController
This commit is contained in:
commit
d531ed057c
6 changed files with 135 additions and 51 deletions
|
@ -5,7 +5,7 @@ class Backoffice::PrivateFormulairesController < ApplicationController
|
|||
dossier = current_gestionnaire.dossiers.find(params[:dossier_id])
|
||||
|
||||
unless params[:champs].nil?
|
||||
champs_service_errors = ChampsService.save_formulaire dossier.champs_private, params
|
||||
champs_service_errors = ChampsService.save_champs dossier.champs_private, params
|
||||
|
||||
if champs_service_errors.empty?
|
||||
flash[:notice] = "Formulaire enregistré"
|
||||
|
|
|
@ -34,18 +34,12 @@ class Users::DescriptionController < UsersController
|
|||
|
||||
@champs = @dossier.ordered_champs
|
||||
|
||||
mandatory = true
|
||||
mandatory = !(params[:submit].keys.first == 'brouillon') unless params[:submit].nil?
|
||||
check_mandatory_fields = !draft_submission?
|
||||
|
||||
unless @dossier.update_attributes(create_params)
|
||||
@dossier = @dossier.decorate
|
||||
|
||||
flash.alert = @dossier.errors.full_messages.join('<br />').html_safe
|
||||
return redirect_to users_dossier_description_path(dossier_id: @dossier.id)
|
||||
end
|
||||
|
||||
unless params[:champs].nil?
|
||||
champs_service_errors = ChampsService.save_formulaire @dossier.champs, params, mandatory
|
||||
if params[:champs]
|
||||
champs_service_errors = ChampsService.save_champs @dossier.champs,
|
||||
params,
|
||||
check_mandatory_fields
|
||||
|
||||
unless champs_service_errors.empty?
|
||||
flash.alert = (champs_service_errors.inject('') { |acc, error| acc+= error[:message]+'<br>' }).html_safe
|
||||
|
@ -53,33 +47,30 @@ class Users::DescriptionController < UsersController
|
|||
end
|
||||
end
|
||||
|
||||
if @procedure.cerfa_flag?
|
||||
unless params[:cerfa_pdf].nil?
|
||||
if @procedure.cerfa_flag? && params[:cerfa_pdf]
|
||||
cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: @dossier, user: current_user)
|
||||
unless cerfa.save
|
||||
flash.alert = cerfa.errors.full_messages.join('<br />').html_safe
|
||||
return redirect_to users_dossier_description_path(dossier_id: @dossier.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
unless (errors_upload = PiecesJustificativesService.upload!(@dossier, current_user, params)).empty?
|
||||
errors_upload = PiecesJustificativesService.upload!(@dossier, current_user, params)
|
||||
unless errors_upload.empty?
|
||||
flash.alert = errors_upload.html_safe
|
||||
return redirect_to users_dossier_description_path(dossier_id: @dossier.id)
|
||||
end
|
||||
|
||||
|
||||
if mandatory
|
||||
if draft_submission?
|
||||
flash.notice = 'Votre brouillon a bien été sauvegardé.'
|
||||
redirect_to url_for(controller: :dossiers, action: :index, liste: :brouillon)
|
||||
else
|
||||
if @dossier.draft?
|
||||
@dossier.initiated!
|
||||
NotificationMailer.send_notification(@dossier, @dossier.procedure.initiated_mail).deliver_now!
|
||||
end
|
||||
|
||||
flash.notice = 'Félicitations, votre demande a bien été enregistrée.'
|
||||
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier.id)
|
||||
else
|
||||
flash.notice = 'Votre brouillon a bien été sauvegardé.'
|
||||
redirect_to url_for(controller: :dossiers, action: :index, liste: :brouillon)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -123,6 +114,10 @@ class Users::DescriptionController < UsersController
|
|||
|
||||
private
|
||||
|
||||
def draft_submission?
|
||||
params[:submit] && params[:submit].keys.first == 'brouillon'
|
||||
end
|
||||
|
||||
def check_autorisation_donnees
|
||||
@dossier = current_user_dossier
|
||||
|
||||
|
@ -137,9 +132,4 @@ class Users::DescriptionController < UsersController
|
|||
redirect_to url_for(users_dossier_path(@dossier.id))
|
||||
end
|
||||
end
|
||||
|
||||
def create_params
|
||||
params.permit()
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -28,6 +28,10 @@ class Champ < ActiveRecord::Base
|
|||
same_date? num, '%M'
|
||||
end
|
||||
|
||||
def mandatory_and_blank?
|
||||
mandatory? && value.blank?
|
||||
end
|
||||
|
||||
def same_date? num, compare
|
||||
if type_champ == 'datetime' && !value.nil?
|
||||
if value.to_datetime.strftime(compare) == num
|
||||
|
|
|
@ -1,27 +1,41 @@
|
|||
class ChampsService
|
||||
def self.save_formulaire champs, params, check_mandatory=true
|
||||
errors = Array.new
|
||||
class << self
|
||||
def save_champs(champs, params, check_mandatory = true)
|
||||
fill_champs(champs, params)
|
||||
|
||||
champs.each do |champ|
|
||||
champ.value = params[:champs]["'#{champ.id}'"]
|
||||
champs.select(&:changed?).each(&:save)
|
||||
|
||||
if champ.type_champ == 'datetime'
|
||||
champ.value = params[:champs]["'#{champ.id}'"]+
|
||||
' ' +
|
||||
params[:time_hour]["'#{champ.id}'"] +
|
||||
':' +
|
||||
params[:time_minute]["'#{champ.id}'"]
|
||||
check_mandatory ? build_error_messages(champs) : []
|
||||
end
|
||||
|
||||
if check_mandatory
|
||||
if champ.mandatory? && (champ.value.nil? || champ.value.blank?)
|
||||
errors.push({message: "Le champ #{champ.libelle} doit être rempli."})
|
||||
end
|
||||
private
|
||||
|
||||
def fill_champs(champs, h)
|
||||
datetimes, not_datetimes = champs.partition { |c| c.type_champ == 'datetime' }
|
||||
|
||||
not_datetimes.each { |c| c.value = h[:champs]["'#{c.id}'"] }
|
||||
datetimes.each { |c| c.value = parse_datetime(c.id, h) }
|
||||
end
|
||||
|
||||
champ.save if champ.changed?
|
||||
def parse_datetime(champ_id, h)
|
||||
"#{h[:champs]["'#{champ_id}'"]} #{extract_hour(champ_id, h)}:#{extract_minute(champ_id, h)}"
|
||||
end
|
||||
|
||||
errors
|
||||
def extract_hour(champ_id, h)
|
||||
h[:time_hour]["'#{champ_id}'"]
|
||||
end
|
||||
|
||||
def extract_minute(champ_id, h)
|
||||
h[:time_minute]["'#{champ_id}'"]
|
||||
end
|
||||
|
||||
def build_error_messages(champs)
|
||||
champs.select(&:mandatory_and_blank?)
|
||||
.map { |c| build_champ_error_message(c) }
|
||||
end
|
||||
|
||||
def build_champ_error_message(champ)
|
||||
{ message: "Le champ #{champ.libelle} doit être rempli." }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -14,6 +14,33 @@ shared_examples 'champ_spec' do
|
|||
it { is_expected.to delegate_method(:order_place).to(:type_de_champ) }
|
||||
end
|
||||
|
||||
describe 'mandatory_and_blank?' do
|
||||
let(:type_de_champ) { TypeDeChamp.new(mandatory: mandatory) }
|
||||
let(:champ) { Champ.new(type_de_champ: type_de_champ, value: value) }
|
||||
let(:value) { '' }
|
||||
let(:mandatory) { true }
|
||||
|
||||
context 'when mandatory and blank' do
|
||||
it { expect(champ.mandatory_and_blank?).to be(true) }
|
||||
end
|
||||
|
||||
context 'when not blank' do
|
||||
let(:value) { 'yop' }
|
||||
it { expect(champ.mandatory_and_blank?).to be(false) }
|
||||
end
|
||||
|
||||
context 'when not mandatory' do
|
||||
let(:mandatory) { false }
|
||||
it { expect(champ.mandatory_and_blank?).to be(false) }
|
||||
end
|
||||
|
||||
context 'when not mandatory or blank' do
|
||||
let(:value) { 'u' }
|
||||
let(:mandatory) { false }
|
||||
it { expect(champ.mandatory_and_blank?).to be(false) }
|
||||
end
|
||||
end
|
||||
|
||||
describe 'data_provide' do
|
||||
let(:champ) { create :champ }
|
||||
|
||||
|
|
49
spec/services/champs_service_spec.rb
Normal file
49
spec/services/champs_service_spec.rb
Normal file
|
@ -0,0 +1,49 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe ChampsService do
|
||||
describe 'save_champs' do
|
||||
let!(:champ) { Champ.create(value: 'toto', type_de_champ: TypeDeChamp.new) }
|
||||
let!(:champ_mandatory_empty) { Champ.create(type_de_champ: TypeDeChamp.new(libelle: 'mandatory', mandatory: true)) }
|
||||
let!(:champ_datetime) do
|
||||
champ_datetime = TypeDeChamp.new(type_champ: 'datetime')
|
||||
Champ.create(type_de_champ: champ_datetime)
|
||||
end
|
||||
let!(:champs) { [champ, champ_mandatory_empty, champ_datetime] }
|
||||
|
||||
before :each do
|
||||
params_hash = {
|
||||
champs: {
|
||||
"'#{champ.id}'" => 'yop',
|
||||
"'#{champ_datetime.id}'" => 'd'
|
||||
},
|
||||
time_hour: { "'#{champ_datetime.id}'" => '12' },
|
||||
time_minute: { "'#{champ_datetime.id}'" => '24' }
|
||||
}
|
||||
@errors = ChampsService.save_champs(champs, params_hash, check_mandatory)
|
||||
champs.each(&:reload)
|
||||
end
|
||||
|
||||
context 'check_mandatory is true' do
|
||||
let(:check_mandatory) { true }
|
||||
it 'saves the changed champ' do
|
||||
expect(champ.value).to eq('yop')
|
||||
end
|
||||
|
||||
it 'parses and save the date' do
|
||||
expect(champ_datetime.value).to eq('d 12:24')
|
||||
end
|
||||
|
||||
it 'adds error for the missing mandatory champ' do
|
||||
expect(@errors).to match([{ message: 'Le champ mandatory doit être rempli.' }])
|
||||
end
|
||||
end
|
||||
|
||||
context 'check_mandatory is false' do
|
||||
let(:check_mandatory) { false }
|
||||
|
||||
it 'does not add errors' do
|
||||
expect(@errors).to match([])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue