[Fix #376] Force every browser to use native input[type=date] on date champ

This commit is contained in:
Mathieu Magnin 2017-06-09 11:45:50 +02:00
parent 2653862fa7
commit 41a5ed59f3
4 changed files with 26 additions and 8 deletions

View file

@ -2,9 +2,15 @@ class ChampDecorator < Draper::Decorator
delegate_all delegate_all
def value def value
return object.value == 'on' ? 'Oui' : 'Non' if type_champ == 'checkbox' if type_champ == "date" && object.value.present?
return JSON.parse(object.value).join(', ') if type_champ == 'multiple_drop_down_list' && object.value.present? Date.parse(object.value).strftime("%d/%m/%Y")
object.value 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 end
def description_with_links def description_with_links

View file

@ -12,12 +12,12 @@ class Champ < ActiveRecord::Base
end end
def data_provide 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' return 'typeahead' if type_champ == 'address'
end end
def data_date_format def data_date_format
('dd/mm/yyyy' if type_champ == 'datetime' || type_champ == 'date') ('dd/mm/yyyy' if type_champ == 'datetime')
end end
def same_hour? num def same_hour? num

View file

@ -2,6 +2,4 @@
placeholder: "JJ/MM/AAAA", placeholder: "JJ/MM/AAAA",
id: "champs_#{champ.id}", id: "champs_#{champ.id}",
value: champ.value, value: champ.value,
type: champ.type_champ, type: "date" }
"data-provide": champ.data_provide,
"data-date-format": champ.data_date_format }

View file

@ -34,5 +34,19 @@ describe ChampDecorator do
it { is_expected.to eq '' } it { is_expected.to eq '' }
end end
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
end end