From 41a5ed59f36bc9af327afdc7e53af17ffb86ee93 Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Fri, 9 Jun 2017 11:45:50 +0200 Subject: [PATCH] [Fix #376] Force every browser to use native input[type=date] on date champ --- app/decorators/champ_decorator.rb | 12 +++++++++--- app/models/champ.rb | 4 ++-- app/views/users/description/champs/_date.html.haml | 4 +--- spec/decorators/champ_decorator_spec.rb | 14 ++++++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app/decorators/champ_decorator.rb b/app/decorators/champ_decorator.rb index 1688c5f91..a56f24947 100644 --- a/app/decorators/champ_decorator.rb +++ b/app/decorators/champ_decorator.rb @@ -2,9 +2,15 @@ class ChampDecorator < Draper::Decorator delegate_all def value - return object.value == 'on' ? 'Oui' : 'Non' if type_champ == 'checkbox' - return JSON.parse(object.value).join(', ') if type_champ == 'multiple_drop_down_list' && object.value.present? - object.value + if type_champ == "date" && object.value.present? + Date.parse(object.value).strftime("%d/%m/%Y") + elsif type_champ == 'checkbox' + object.value == 'on' ? 'Oui' : 'Non' + elsif type_champ == 'multiple_drop_down_list' && object.value.present? + JSON.parse(object.value).join(', ') + else + object.value + end end def description_with_links diff --git a/app/models/champ.rb b/app/models/champ.rb index ce12e32a3..7e7a49811 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -12,12 +12,12 @@ class Champ < ActiveRecord::Base end def data_provide - return 'datepicker' if (type_champ == 'datetime' || type_champ == 'date') && !(BROWSER.value.chrome? || BROWSER.value.edge?) + 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' || type_champ == 'date') + ('dd/mm/yyyy' if type_champ == 'datetime') end def same_hour? num diff --git a/app/views/users/description/champs/_date.html.haml b/app/views/users/description/champs/_date.html.haml index 33a20565a..f04b581a6 100644 --- a/app/views/users/description/champs/_date.html.haml +++ b/app/views/users/description/champs/_date.html.haml @@ -2,6 +2,4 @@ placeholder: "JJ/MM/AAAA", id: "champs_#{champ.id}", value: champ.value, - type: champ.type_champ, - "data-provide": champ.data_provide, - "data-date-format": champ.data_date_format } + type: "date" } diff --git a/spec/decorators/champ_decorator_spec.rb b/spec/decorators/champ_decorator_spec.rb index c8b15056c..a5a2554a4 100644 --- a/spec/decorators/champ_decorator_spec.rb +++ b/spec/decorators/champ_decorator_spec.rb @@ -34,5 +34,19 @@ describe ChampDecorator do it { is_expected.to eq '' } end end + + describe "for a date" do + let(:type_champ) { :date } + + context "when value is an ISO date" do + before { champ.update value: "2017-12-31" } + it { is_expected.to eq "31/12/2017" } + end + + context "when value is empty" do + before { champ.update value: nil } + it { is_expected.to eq nil } + end + end end end