ChampsService: add tests and refactor
This commit is contained in:
parent
c5159dde4b
commit
4df03fc28e
6 changed files with 119 additions and 23 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é"
|
||||
|
|
|
@ -44,8 +44,10 @@ class Users::DescriptionController < UsersController
|
|||
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,
|
||||
mandatory
|
||||
|
||||
unless champs_service_errors.empty?
|
||||
flash.alert = (champs_service_errors.inject('') { |acc, error| acc+= error[:message]+'<br>' }).html_safe
|
||||
|
|
|
@ -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}'"]
|
||||
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?
|
||||
check_mandatory ? build_error_messages(champs) : []
|
||||
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
|
||||
|
|
|
@ -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