Add type de champ date

Add hour and minute on UI for datetime
This commit is contained in:
Xavier J 2016-06-22 11:25:41 +02:00
parent 2fa10a43e5
commit 4355d4775a
13 changed files with 148 additions and 37 deletions

View file

@ -77,12 +77,21 @@
}
}
.type_champ-datetime {
.type_champ-date {
@extend .col-md-2;
@extend .col-lg-2;
input[type='number'] {
width: 100%;
input[type='date'] {
width: 120px;
}
}
.type_champ-datetime {
@extend .col-md-4;
@extend .col-lg-4;
input[type='datetime'] {
width: 120px;
}
}

View file

@ -46,6 +46,14 @@ class Users::DescriptionController < UsersController
@dossier.champs.each do |champ|
champ.value = params[:champs]["'#{champ.id}'"]
if champ.type_champ == 'datetime'
champ.value = params[:champs]["'#{champ.id}'"]+
' ' +
params[:time_hour]["'#{champ.id}'"] +
':' +
params[:time_minute]["'#{champ.id}'"]
end
if champ.mandatory? && (champ.value.nil? || champ.value.blank?)
flash.now.alert = "Le champ #{champ.libelle} doit être rempli."
return render 'show'

View file

@ -9,7 +9,28 @@ class Champ < ActiveRecord::Base
end
def data_provide
return 'datepicker' if type_champ == 'datetime'
return 'datepicker' if type_champ == 'datetime' || type_champ == 'date'
return 'typeahead' if type_champ == 'address'
end
def data_date_format
('dd/mm/yyyy' if type_champ == 'datetime' || type_champ == 'date')
end
def same_hour? num
same_date? num, '%H'
end
def same_minute? num
same_date? num, '%M'
end
def same_date? num, compare
if type_champ == 'datetime' && !value.nil?
if value.to_datetime.strftime(compare) == num
return true
end
end
false
end
end

View file

@ -2,6 +2,7 @@ class TypeDeChamp < ActiveRecord::Base
enum type_champs: {
text: 'text',
textarea: 'textarea',
date: 'date',
datetime: 'datetime',
number: 'number',
checkbox: 'checkbox',

View file

@ -15,19 +15,13 @@
= '*'
-if champ.type_champ == 'textarea'
%textarea.form-control.wysihtml5{name:"champs['#{champ.id}']",
placeholder: champ.description,
id: "champs_#{champ.id}",
row: '6'}
=champ.value
-elsif champ.type_champ == 'civilite'
%label.radio-inline
= radio_button_tag "champs['#{champ.id}']", "M.", champ.value == 'Mme' ? false : true
Monsieur
=render partial: 'users/description/champs/textarea', locals: {champ: champ}
%label.radio-inline
= radio_button_tag "champs['#{champ.id}']", "Mme", champ.value == 'Mme'
Madame
-elsif champ.type_champ == 'civilite'
=render partial: 'users/description/champs/civilite', locals: {champ: champ}
- elsif champ.type_champ == 'datetime'
=render partial: 'users/description/champs/datetime', locals: {champ: champ}
-else
%input.form-control{name:"champs['#{champ.id}']",
@ -36,7 +30,7 @@
value: champ.value,
type: champ.type_champ,
'data-provide' => champ.data_provide,
'data-date-format' => ('dd/mm/yyyy' if champ.type_champ == 'datetime')}
'data-date-format' => champ.data_date_format}
- unless champ.description.empty?
.row

View file

@ -0,0 +1,7 @@
%label.radio-inline
= radio_button_tag "champs['#{champ.id}']", "M.", champ.value == 'Mme' ? false : true
Monsieur
%label.radio-inline
= radio_button_tag "champs['#{champ.id}']", "Mme", champ.value == 'Mme'
Madame

View file

@ -0,0 +1,21 @@
%input.form-control{name:"champs['#{champ.id}']",
placeholder: champ.libelle,
id: "champs_#{champ.id}",
value: (champ.value.split(/[ ][0-9]*:[0-9]*/).first unless champ.value.nil?),
type: champ.type_champ,
'data-provide' => champ.data_provide,
'data-date-format' => champ.data_date_format}
%select.form-control{name:"time_hour['#{champ.id}']", style: 'margin-left: 5px', id:"time_hour_#{champ.id}"}
- (0..23).each do |num|
- num = "%.2i" %num
%option{value: num, selected: (:selected if champ.same_hour?(num))}
= num
h
%select.form-control{name:"time_minute['#{champ.id}']", id:"time_minute_#{champ.id}"}
- (0..59).each do |num|
- num = "%.2i" %num
- if num.to_i%5 == 0
%option{value: num, selected: (:selected if champ.same_minute?(num))}
= num
min

View file

@ -0,0 +1,5 @@
%textarea.form-control.wysihtml5{name:"champs['#{champ.id}']",
placeholder: champ.description,
id: "champs_#{champ.id}",
row: '6'}
=champ.value

View file

@ -0,0 +1,15 @@
class ConvertAllDatetimeToDateOnDatabase < ActiveRecord::Migration
class TypeDeChamp < ActiveRecord::Base
end
def change
TypeDeChamp.all.each do |type_de_champ|
if type_de_champ.type_champ == 'datetime'
type_de_champ.type_champ = 'date'
type_de_champ.save
end
end
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160609145737) do
ActiveRecord::Schema.define(version: 20160622081321) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

View file

@ -1,9 +1,9 @@
require 'spec_helper'
describe Users::DescriptionController, type: :controller, vcr: { cassette_name: 'controllers_users_description_controller' } do
describe Users::DescriptionController, type: :controller, vcr: {cassette_name: 'controllers_users_description_controller'} do
let(:user) { create(:user) }
let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, cerfa_flag: true) }
let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: true) }
let(:dossier) { create(:dossier, procedure: procedure, user: user) }
let(:dossier_id) { dossier.id }
@ -67,11 +67,10 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
let(:description) { 'Description de test Coucou, je suis un saut à la ligne Je suis un double saut la ligne.' }
context 'Tous les attributs sont bons' do
# TODO separer en deux tests : check donnees et check redirect
describe 'Premier enregistrement des données' do
before do
dossier.draft!
post :create, dossier_id: dossier_id, nom_projet: nom_projet, description: description
post :create, dossier_id: dossier_id, nom_projet: nom_projet
dossier.reload
end
@ -79,12 +78,13 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
expect(response).to redirect_to("/users/dossiers/#{dossier_id}/recapitulatif")
end
it { expect(dossier.nom_projet).to eq nom_projet }
it 'etat du dossier est soumis' do
expect(dossier.state).to eq('initiated')
end
end
# TODO changer les valeurs des champs et check in bdd
context 'En train de manipuler un dossier non brouillon' do
before do
dossier.initiated!
@ -119,7 +119,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
end
context 'Quand la procédure accepte les CERFA' do
context 'Sauvegarde du CERFA PDF', vcr: { cassette_name: 'controllers_users_description_controller_save_cerfa' } do
context 'Sauvegarde du CERFA PDF', vcr: {cassette_name: 'controllers_users_description_controller_save_cerfa'} do
before do
post :create, dossier_id: dossier_id,
nom_projet: nom_projet,
@ -128,7 +128,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
dossier.reload
end
context 'when a CERFA PDF is sent', vcr: { cassette_name: 'controllers_users_description_controller_cerfa_is_sent' } do
context 'when a CERFA PDF is sent', vcr: {cassette_name: 'controllers_users_description_controller_cerfa_is_sent'} do
subject { dossier.cerfa.first }
it 'content' do
@ -180,13 +180,23 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
describe 'Sauvegarde des champs' do
let(:champs_dossier) { dossier.champs }
let(:dossier_champs_first) { 'test value' }
let(:dossier_date_value) { '23/06/2016' }
let(:dossier_hour_value) { '17' }
let(:dossier_minute_value) { '00' }
before do
post :create, {dossier_id: dossier_id,
nom_projet: nom_projet,
description: description,
champs: {
"'#{dossier.champs.first.id}'" => dossier_champs_first
"'#{dossier.champs.first.id}'" => dossier_champs_first,
"'#{dossier.champs.second.id}'" => dossier_date_value
},
time_hour: {
"'#{dossier.champs.second.id}'" => dossier_hour_value,
},
time_minute: {
"'#{dossier.champs.second.id}'" => dossier_minute_value,
}
}
dossier.reload
@ -195,6 +205,10 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
it { expect(dossier.champs.first.value).to eq(dossier_champs_first) }
it { expect(response).to redirect_to users_dossier_recapitulatif_path }
context 'when champs is type_de_champ datetime' do
it { expect(dossier.champs.second.value).to eq(dossier_date_value+' '+dossier_hour_value+':'+dossier_minute_value) }
end
context 'when champs value is empty' do
let(:dossier_champs_first) { '' }
@ -202,7 +216,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
it { expect(response).to redirect_to users_dossier_recapitulatif_path }
context 'when champs is mandatory' do
let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ_mandatory, cerfa_flag: true) }
let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ_mandatory, :with_datetime, cerfa_flag: true) }
it { expect(response).not_to redirect_to users_dossier_recapitulatif_path }
it { expect(flash[:alert]).to be_present }
@ -210,7 +224,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
end
end
context 'Sauvegarde des pièces justificatives', vcr: { cassette_name: 'controllers_users_description_controller_sauvegarde_pj' } do
context 'Sauvegarde des pièces justificatives', vcr: {cassette_name: 'controllers_users_description_controller_sauvegarde_pj'} do
let(:all_pj_type) { dossier.procedure.type_de_piece_justificative_ids }
before do
post :create, {dossier_id: dossier_id,
@ -221,7 +235,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
dossier.reload
end
describe 'clamav anti-virus presence', vcr: { cassette_name: 'controllers_users_description_controller_clamav_presence' } do
describe 'clamav anti-virus presence', vcr: {cassette_name: 'controllers_users_description_controller_clamav_presence'} do
it 'ClamavService safe_file? is call' do
expect(ClamavService).to receive(:safe_file?).twice
@ -230,8 +244,6 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
description: description,
'piece_justificative_'+all_pj_type[0].to_s => piece_justificative_0,
'piece_justificative_'+all_pj_type[1].to_s => piece_justificative_1}
end
end
@ -248,7 +260,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
end
end
describe 'POST #pieces_justificatives', vcr: { cassette_name: 'controllers_users_description_controller_pieces_justificatives' } do
describe 'POST #pieces_justificatives', vcr: {cassette_name: 'controllers_users_description_controller_pieces_justificatives'} do
let(:all_pj_type) { dossier.procedure.type_de_piece_justificative_ids }
subject { patch :pieces_justificatives, {dossier_id: dossier.id,
@ -275,7 +287,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
end
end
context 'when PJ have already a document', vcr: { cassette_name: 'controllers_users_description_controller_pj_already_exist' } do
context 'when PJ have already a document', vcr: {cassette_name: 'controllers_users_description_controller_pj_already_exist'} do
before do
create :piece_justificative, :rib, dossier: dossier, type_de_piece_justificative_id: all_pj_type[0]
create :piece_justificative, :contrat, dossier: dossier, type_de_piece_justificative_id: all_pj_type[1]
@ -283,7 +295,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
it { expect(dossier.pieces_justificatives.size).to eq 2 }
context 'when upload two PJ', vcr: { cassette_name: 'controllers_users_description_controller_pj_already_exist_upload_2pj' } do
context 'when upload two PJ', vcr: {cassette_name: 'controllers_users_description_controller_pj_already_exist_upload_2pj'} do
before do
subject
dossier.reload
@ -345,7 +357,7 @@ describe Users::DescriptionController, type: :controller, vcr: { cassette_name:
it { expect(dossier.pieces_justificatives.size).to eq 2 }
context 'when upload two PJ', vcr: { cassette_name: 'controllers_users_description_controller_upload_2pj' } do
context 'when upload two PJ', vcr: {cassette_name: 'controllers_users_description_controller_upload_2pj'} do
before do
subject
dossier.reload

View file

@ -36,6 +36,14 @@ FactoryGirl.define do
end
end
trait :with_datetime do
after(:build) do |procedure, _evaluator|
type_de_champ = create(:type_de_champ, mandatory: true, type_champ: :datetime)
procedure.types_de_champ << type_de_champ
end
end
trait :with_two_type_de_piece_justificative do
after(:build) do |procedure, _evaluator|
rib = create(:type_de_piece_justificative, :rib)

View file

@ -3,7 +3,7 @@ require 'spec_helper'
describe 'users/description/show.html.haml', type: :view do
let(:user) { create(:user) }
let(:cerfa_flag) { true }
let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, cerfa_flag: cerfa_flag) }
let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: cerfa_flag) }
let(:dossier) { create(:dossier, procedure: procedure, user: user) }
let(:dossier_id) { dossier.id }
@ -82,8 +82,12 @@ describe 'users/description/show.html.haml', type: :view do
context 'Champs' do
let(:champs) { dossier.champs }
let(:types_de_champ) { procedure.types_de_champ.where(type_champ: 'datetime').first }
let(:champ_datetime) { champs.where(type_de_champ_id: types_de_champ.id).first }
before do
champ_datetime.value = "22/06/2016 12:05"
champ_datetime.save
render
end
@ -98,6 +102,12 @@ describe 'users/description/show.html.haml', type: :view do
it { expect(rendered).to have_css(".type_champ-#{subject.type_champ}") }
it { expect(rendered).to have_css("#champs_#{subject.id}") }
end
describe 'datetime value is correctly setup when is not nil' do
it { expect(rendered).to have_css("input[type='datetime'][id='champs_#{champ_datetime.id}'][value='22/06/2016']") }
it { expect(rendered).to have_css("option[value='12'][selected='selected']")}
it { expect(rendered).to have_css("option[value='05'][selected='selected']")}
end
end
context 'Pièces justificatives' do
@ -156,7 +166,7 @@ describe 'users/description/show.html.haml', type: :view do
end
context 'when procedure have cerfa flag true' do
let(:procedure) {create(:procedure, cerfa_flag: true)}
let(:procedure) { create(:procedure, cerfa_flag: true) }
it { expect(rendered).to have_content 'Documents administratifs' }
end