Merge pull request #412 from sgmap/normalize_api_date_format
[Fix #376] Normalize api date format
This commit is contained in:
commit
b0c0824341
7 changed files with 73 additions and 8 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
5
app/views/users/description/champs/_date.html.haml
Normal file
5
app/views/users/description/champs/_date.html.haml
Normal 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" }
|
|
@ -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') }
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
FactoryGirl.define do
|
||||
factory :champ do
|
||||
|
||||
type_de_champ { FactoryGirl.create(:type_de_champ_public) }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue