Merge pull request #412 from sgmap/normalize_api_date_format

[Fix #376] Normalize api date format
This commit is contained in:
Mathieu Magnin 2017-06-12 14:10:08 +02:00 committed by GitHub
commit b0c0824341
7 changed files with 73 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

@ -5,6 +5,7 @@ class Champ < ActiveRecord::Base
delegate :libelle, :type_champ, :order_place, :mandatory, :description, :drop_down_list, to: :type_de_champ 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? } after_save :internal_notification, if: Proc.new { !dossier.nil? }
def mandatory? def mandatory?
@ -12,12 +13,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
@ -55,6 +56,15 @@ class Champ < ActiveRecord::Base
private private
def format_date_to_iso
date = begin
Date.parse(value).strftime("%F")
rescue
nil
end
self.value = date
end
def internal_notification def internal_notification
unless dossier.state == 'draft' unless dossier.state == 'draft'
NotificationService.new('champs', self.dossier.id, self.libelle).notify NotificationService.new('champs', self.dossier.id, self.libelle).notify

View file

@ -0,0 +1,5 @@
%input.form-control{ name: "champs['#{champ.id}']",
placeholder: "JJ/MM/AAAA",
id: "champs_#{champ.id}",
value: champ.value,
type: "date" }

View file

@ -47,14 +47,16 @@
- when 'explication' - when 'explication'
- when 'date'
= render partial: 'users/description/champs/date', locals: { champ: champ }
- else - else
%input.form-control{ name: "champs['#{champ.id}']", %input.form-control{ name: "champs['#{champ.id}']",
placeholder: champ.libelle, placeholder: champ.libelle,
id: "champs_#{champ.id}", id: "champs_#{champ.id}",
value: champ.value, value: champ.value,
type: champ.type_champ, type: champ.type_champ }
'data-provide' => champ.data_provide,
'data-date-format' => champ.data_date_format }
- unless champ.description.empty? - unless champ.description.empty?
%div{ id: "description_champs_#{champ.id}", class: ('help-block' unless champ.type_champ == 'engagement') } %div{ id: "description_champs_#{champ.id}", class: ('help-block' unless champ.type_champ == 'engagement') }

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

View file

@ -1,4 +1,6 @@
FactoryGirl.define do FactoryGirl.define do
factory :champ do factory :champ do
type_de_champ { FactoryGirl.create(:type_de_champ_public) }
end end
end end

View file

@ -68,4 +68,30 @@ shared_examples 'champ_spec' do
it { expect(subject).to include '99 - Étranger' } it { expect(subject).to include '99 - Étranger' }
end 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 end