[Fix #1309] Format datetime before save
This commit is contained in:
parent
dcb38d79a3
commit
50abb36c81
3 changed files with 13 additions and 12 deletions
|
@ -6,7 +6,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' }
|
||||
before_save :serialize_datetime_if_needed, if: Proc.new { type_champ == 'datetime' }
|
||||
before_save :format_datetime, if: Proc.new { type_champ == 'datetime' }
|
||||
before_save :multiple_select_to_string, if: Proc.new { type_champ == 'multiple_drop_down_list' }
|
||||
|
||||
after_save :internal_notification, if: Proc.new { dossier.present? }
|
||||
|
@ -93,7 +93,7 @@ class Champ < ActiveRecord::Base
|
|||
self.value = date
|
||||
end
|
||||
|
||||
def serialize_datetime_if_needed
|
||||
def format_datetime
|
||||
if (value =~ /=>/).present?
|
||||
date = begin
|
||||
hash_date = YAML.safe_load(value.gsub('=>', ': '))
|
||||
|
@ -102,8 +102,11 @@ class Champ < ActiveRecord::Base
|
|||
rescue
|
||||
nil
|
||||
end
|
||||
|
||||
self.value = date
|
||||
elsif /^\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}$/ =~ value # old browsers can send with dd/mm/yyyy hh:mm format
|
||||
self.value = DateTime.parse(value, "%d/%m/%Y %H:%M").strftime("%Y-%m-%d %H:%M")
|
||||
elsif !(/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}$/ =~ value) # a datetime not correctly formatted should not be stored
|
||||
self.value = nil
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -5,24 +5,22 @@ describe Champ do
|
|||
|
||||
it_should_behave_like "champ_spec"
|
||||
|
||||
describe '#serialize_datetime_if_needed' do
|
||||
describe '#format_datetime' do
|
||||
let(:type_de_champ) { TypeDeChamp.new(type_champ: 'datetime') }
|
||||
let(:champ) { Champ.new(type_de_champ: type_de_champ, value: value) }
|
||||
|
||||
before { champ.save }
|
||||
|
||||
# when using the old form, and the ChampsService Class
|
||||
# TODO: to remove
|
||||
context 'when the value is already serialized' do
|
||||
let(:value) { '12/01/2017 10:23' }
|
||||
context 'when the value is sent by a modern browser' do
|
||||
let(:value) { '2017-12-31 10:23' }
|
||||
|
||||
it { expect(champ.value).to eq(value) }
|
||||
end
|
||||
|
||||
context 'when the value is not already serialized' do
|
||||
let(:value) { '{ 1=>2017, 2=>01, 3=>12, 4=>10, 5=>23 }' }
|
||||
context 'when the value is sent by a old browser' do
|
||||
let(:value) { '31/12/2018 09:26' }
|
||||
|
||||
it { expect(champ.value).to eq('12/01/2017 10:23') }
|
||||
it { expect(champ.value).to eq('2018-12-31 09:26') }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ describe ChampsService do
|
|||
end
|
||||
|
||||
it 'parses and save the date' do
|
||||
expect(champ_datetime.value).to eq('d 12:24')
|
||||
expect(champ_datetime.value).to eq(nil)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue