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..5b90b309a 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -5,6 +5,7 @@ class Champ < ActiveRecord::Base delegate :libelle, :type_champ, :order_place, :mandatory, :description, :drop_down_list, to: :type_de_champ + before_save :format_date_to_iso, if: Proc.new { type_champ == 'date' } after_save :internal_notification, if: Proc.new { !dossier.nil? } def mandatory? @@ -12,12 +13,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 @@ -55,6 +56,15 @@ class Champ < ActiveRecord::Base private + def format_date_to_iso + date = begin + Date.parse(value).strftime("%F") + rescue + nil + end + self.value = date + end + def internal_notification unless dossier.state == 'draft' NotificationService.new('champs', self.dossier.id, self.libelle).notify diff --git a/app/views/users/description/champs/_date.html.haml b/app/views/users/description/champs/_date.html.haml new file mode 100644 index 000000000..f04b581a6 --- /dev/null +++ b/app/views/users/description/champs/_date.html.haml @@ -0,0 +1,5 @@ +%input.form-control{ name: "champs['#{champ.id}']", + placeholder: "JJ/MM/AAAA", + id: "champs_#{champ.id}", + value: champ.value, + type: "date" } diff --git a/app/views/users/description/champs/_render_list_champs.html.haml b/app/views/users/description/champs/_render_list_champs.html.haml index 01c395d33..6bb776379 100644 --- a/app/views/users/description/champs/_render_list_champs.html.haml +++ b/app/views/users/description/champs/_render_list_champs.html.haml @@ -47,14 +47,16 @@ - when 'explication' + - when 'date' + = render partial: 'users/description/champs/date', locals: { champ: champ } + - else %input.form-control{ name: "champs['#{champ.id}']", placeholder: champ.libelle, id: "champs_#{champ.id}", value: champ.value, - type: champ.type_champ, - 'data-provide' => champ.data_provide, - 'data-date-format' => champ.data_date_format } + type: champ.type_champ } + - unless champ.description.empty? %div{ id: "description_champs_#{champ.id}", class: ('help-block' unless champ.type_champ == 'engagement') } 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 diff --git a/spec/factories/champ.rb b/spec/factories/champ.rb index 96620cb80..3a2302f4c 100644 --- a/spec/factories/champ.rb +++ b/spec/factories/champ.rb @@ -1,4 +1,6 @@ FactoryGirl.define do factory :champ do + + type_de_champ { FactoryGirl.create(:type_de_champ_public) } end end diff --git a/spec/models/champ_shared_example.rb b/spec/models/champ_shared_example.rb index b404154df..7dfe6bb1b 100644 --- a/spec/models/champ_shared_example.rb +++ b/spec/models/champ_shared_example.rb @@ -68,4 +68,30 @@ shared_examples 'champ_spec' do it { expect(subject).to include '99 - Étranger' } end + + context "when type_champ=date" do + let(:type_de_champ) { create(:type_de_champ_public, type_champ: "date")} + let(:champ) { create(:champ, type_de_champ: type_de_champ) } + + it "should convert %d/%m/%Y format to ISO" do + champ.value = "31/12/2017" + champ.save + champ.reload + expect(champ.value).to eq("2017-12-31") + end + + it "should convert to nil if date parse failed" do + champ.value = "bla" + champ.save + champ.reload + expect(champ.value).to be(nil) + end + + it "should convert empty string to nil" do + champ.value = "" + champ.save + champ.reload + expect(champ.value).to be(nil) + end + end end