diff --git a/Gemfile b/Gemfile index 8666492d0..591ce5f76 100644 --- a/Gemfile +++ b/Gemfile @@ -64,6 +64,8 @@ gem 'leaflet-rails' gem 'leaflet-markercluster-rails', '~> 0.7.0' gem 'leaflet-draw-rails' +gem 'bootstrap-datepicker-rails' + gem 'chartkick' gem 'logstasher' diff --git a/Gemfile.lock b/Gemfile.lock index 0b10d1a88..df20eaa38 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -92,6 +92,8 @@ GEM rubyzip (~> 1.0.0) bcrypt (3.1.11) bindata (2.3.4) + bootstrap-datepicker-rails (1.6.4.1) + railties (>= 3.0) bootstrap-sass (3.3.7) autoprefixer-rails (>= 5.2.1) sass (>= 3.3.4) @@ -729,6 +731,7 @@ DEPENDENCIES active_model_serializers administrate apipie-rails + bootstrap-datepicker-rails bootstrap-sass (~> 3.3.5) bootstrap-wysihtml5-rails (~> 0.3.3.8) brakeman diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 62f9b7d15..d5ce61307 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -17,6 +17,8 @@ //= require chartkick //= require_tree ./old_design //= require bootstrap-sprockets +//= require bootstrap-datepicker/core +//= require bootstrap-datepicker/locales/bootstrap-datepicker.fr.js //= require leaflet.js //= require d3.min diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index ed0ae2bf5..99cf3f12a 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -46,6 +46,7 @@ // = require attestation_recapitulatif // = require_self +// = require bootstrap-datepicker3 // = require leaflet // = require font-awesome // = require franceconnect diff --git a/app/assets/stylesheets/description.scss b/app/assets/stylesheets/description.scss index 3c97d8476..3efa4d01b 100644 --- a/app/assets/stylesheets/description.scss +++ b/app/assets/stylesheets/description.scss @@ -1,4 +1,5 @@ @import "bootstrap"; +@import "bootstrap-datepicker3"; #description-page #liste-champs { diff --git a/app/decorators/champ_decorator.rb b/app/decorators/champ_decorator.rb index ee6e524e3..4652d5d08 100644 --- a/app/decorators/champ_decorator.rb +++ b/app/decorators/champ_decorator.rb @@ -19,16 +19,6 @@ class ChampDecorator < Draper::Decorator end end - def date_for_input - if object.value.present? - if type_champ == "date" - object.value - elsif type_champ == "datetime" && object.value != ' 00:00' - DateTime.parse(object.value, "%d/%m/%Y %H:%M").strftime("%d/%m/%Y") - end - end - end - def description_with_links description.gsub(URI.regexp, '\0') if description end diff --git a/app/models/champ.rb b/app/models/champ.rb index abba4569b..ca6a59c8c 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -17,6 +17,15 @@ class Champ < ActiveRecord::Base mandatory end + def data_provide + return 'datepicker' if (type_champ == 'datetime') && !(BROWSER.value.chrome? || BROWSER.value.edge?) + return 'typeahead' if type_champ == 'address' + end + + def data_date_format + ('dd/mm/yyyy' if type_champ == 'datetime') + end + def same_hour? num same_date? num, '%H' end diff --git a/app/views/users/description/champs/_date.html.haml b/app/views/users/description/champs/_date.html.haml index 7e0d94563..74088b67c 100644 --- a/app/views/users/description/champs/_date.html.haml +++ b/app/views/users/description/champs/_date.html.haml @@ -1,5 +1,5 @@ %input.form-control{ name: "champs['#{champ.id}']", placeholder: "JJ/MM/AAAA", id: "champs_#{champ.id}", - value: champ.date_for_input, + value: champ.value ? champ.object.value : champ.value, type: "date" } diff --git a/app/views/users/description/champs/_datetime.html.haml b/app/views/users/description/champs/_datetime.html.haml index 298d66860..061ed821d 100644 --- a/app/views/users/description/champs/_datetime.html.haml +++ b/app/views/users/description/champs/_datetime.html.haml @@ -1,13 +1,18 @@ -= render partial: 'users/description/champs/date', locals: { champ: champ } +%input.form-control{ name: "champs['#{champ.id}']", + placeholder: champ.libelle, + id: "champs_#{champ.id}", + value: (champ.value.split(/[ ][0-9]*:[0-9]*/).first if champ.value.present?), + type: champ.type_champ, + 'data-provide' => champ.data_provide, + 'data-date-format' => champ.data_date_format } -%br -%select.form-control{ name: "time_hour['#{champ.id}']", style: 'width:50px;display:inline;', id: "time_hour_#{champ.id}" } +%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}']", style: 'width:50px;display:inline;', id: "time_minute_#{champ.id}" } +%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 diff --git a/spec/models/champ_shared_example.rb b/spec/models/champ_shared_example.rb index 60dc38e9a..c8bb4360e 100644 --- a/spec/models/champ_shared_example.rb +++ b/spec/models/champ_shared_example.rb @@ -26,6 +26,28 @@ shared_examples 'champ_spec' do end end + describe 'data_provide' do + let(:champ) { create :champ } + + subject { champ.data_provide } + + context 'when type_champ is datetime' do + before do + champ.type_de_champ = create :type_de_champ_public, type_champ: 'datetime' + end + + it { is_expected.to eq 'datepicker' } + end + + context 'when type_champ is address' do + before do + champ.type_de_champ = create :type_de_champ_public, type_champ: 'address' + end + + it { is_expected.to eq 'typeahead' } + end + end + describe '.departement', vcr: { cassette_name: 'call_geo_api_departements' } do subject { Champ.departements } diff --git a/spec/views/users/description/show.html.haml_spec.rb b/spec/views/users/description/show.html.haml_spec.rb index c4d78f860..4f7bf3d62 100644 --- a/spec/views/users/description/show.html.haml_spec.rb +++ b/spec/views/users/description/show.html.haml_spec.rb @@ -82,7 +82,7 @@ describe 'users/description/show.html.haml', type: :view do end describe 'datetime value is correctly setup when is not nil' do - it { expect(rendered).to have_css("input[type='date'][id='champs_#{champ_datetime.id}'][value='22/06/2016']") } + 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