Merge pull request #75 from sgmap/champs_service_refactor

Champs service refactor + un peu de DescriptionController
This commit is contained in:
gregoirenovel 2017-04-11 12:19:18 +02:00 committed by GitHub
commit d531ed057c
6 changed files with 135 additions and 51 deletions

View file

@ -5,7 +5,7 @@ class Backoffice::PrivateFormulairesController < ApplicationController
dossier = current_gestionnaire.dossiers.find(params[:dossier_id]) dossier = current_gestionnaire.dossiers.find(params[:dossier_id])
unless params[:champs].nil? 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? if champs_service_errors.empty?
flash[:notice] = "Formulaire enregistré" flash[:notice] = "Formulaire enregistré"

View file

@ -34,18 +34,12 @@ class Users::DescriptionController < UsersController
@champs = @dossier.ordered_champs @champs = @dossier.ordered_champs
mandatory = true check_mandatory_fields = !draft_submission?
mandatory = !(params[:submit].keys.first == 'brouillon') unless params[:submit].nil?
unless @dossier.update_attributes(create_params) if params[:champs]
@dossier = @dossier.decorate champs_service_errors = ChampsService.save_champs @dossier.champs,
params,
flash.alert = @dossier.errors.full_messages.join('<br />').html_safe check_mandatory_fields
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
unless champs_service_errors.empty? unless champs_service_errors.empty?
flash.alert = (champs_service_errors.inject('') { |acc, error| acc+= error[:message]+'<br>' }).html_safe flash.alert = (champs_service_errors.inject('') { |acc, error| acc+= error[:message]+'<br>' }).html_safe
@ -53,33 +47,30 @@ class Users::DescriptionController < UsersController
end end
end end
if @procedure.cerfa_flag? if @procedure.cerfa_flag? && params[:cerfa_pdf]
unless params[:cerfa_pdf].nil? cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: @dossier, user: current_user)
cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: @dossier, user: current_user) unless cerfa.save
unless cerfa.save flash.alert = cerfa.errors.full_messages.join('<br />').html_safe
flash.alert = cerfa.errors.full_messages.join('<br />').html_safe return redirect_to users_dossier_description_path(dossier_id: @dossier.id)
return redirect_to users_dossier_description_path(dossier_id: @dossier.id)
end
end 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 flash.alert = errors_upload.html_safe
return redirect_to users_dossier_description_path(dossier_id: @dossier.id) return redirect_to users_dossier_description_path(dossier_id: @dossier.id)
end end
if draft_submission?
if mandatory flash.notice = 'Votre brouillon a bien été sauvegardé.'
redirect_to url_for(controller: :dossiers, action: :index, liste: :brouillon)
else
if @dossier.draft? if @dossier.draft?
@dossier.initiated! @dossier.initiated!
NotificationMailer.send_notification(@dossier, @dossier.procedure.initiated_mail).deliver_now! NotificationMailer.send_notification(@dossier, @dossier.procedure.initiated_mail).deliver_now!
end end
flash.notice = 'Félicitations, votre demande a bien été enregistrée.' flash.notice = 'Félicitations, votre demande a bien été enregistrée.'
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier.id) 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
end end
@ -123,6 +114,10 @@ class Users::DescriptionController < UsersController
private private
def draft_submission?
params[:submit] && params[:submit].keys.first == 'brouillon'
end
def check_autorisation_donnees def check_autorisation_donnees
@dossier = current_user_dossier @dossier = current_user_dossier
@ -137,9 +132,4 @@ class Users::DescriptionController < UsersController
redirect_to url_for(users_dossier_path(@dossier.id)) redirect_to url_for(users_dossier_path(@dossier.id))
end end
end end
def create_params
params.permit()
end
end end

View file

@ -28,6 +28,10 @@ class Champ < ActiveRecord::Base
same_date? num, '%M' same_date? num, '%M'
end end
def mandatory_and_blank?
mandatory? && value.blank?
end
def same_date? num, compare def same_date? num, compare
if type_champ == 'datetime' && !value.nil? if type_champ == 'datetime' && !value.nil?
if value.to_datetime.strftime(compare) == num if value.to_datetime.strftime(compare) == num

View file

@ -1,27 +1,41 @@
class ChampsService class ChampsService
def self.save_formulaire champs, params, check_mandatory=true class << self
errors = Array.new def save_champs(champs, params, check_mandatory = true)
fill_champs(champs, params)
champs.each do |champ| champs.select(&:changed?).each(&:save)
champ.value = params[:champs]["'#{champ.id}'"]
if champ.type_champ == 'datetime' check_mandatory ? build_error_messages(champs) : []
champ.value = params[:champs]["'#{champ.id}'"]+
' ' +
params[:time_hour]["'#{champ.id}'"] +
':' +
params[:time_minute]["'#{champ.id}'"]
end
if check_mandatory
if champ.mandatory? && (champ.value.nil? || champ.value.blank?)
errors.push({message: "Le champ #{champ.libelle} doit être rempli."})
end
end
champ.save if champ.changed?
end end
errors 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
def parse_datetime(champ_id, h)
"#{h[:champs]["'#{champ_id}'"]} #{extract_hour(champ_id, h)}:#{extract_minute(champ_id, h)}"
end
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
end end

View file

@ -14,6 +14,33 @@ shared_examples 'champ_spec' do
it { is_expected.to delegate_method(:order_place).to(:type_de_champ) } it { is_expected.to delegate_method(:order_place).to(:type_de_champ) }
end 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 describe 'data_provide' do
let(:champ) { create :champ } let(:champ) { create :champ }

View 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