[#1421] Move type-specific before_save to typed champs
This commit is contained in:
parent
75bdb8c4bc
commit
c690d3819e
4 changed files with 47 additions and 44 deletions
|
@ -7,10 +7,6 @@ class Champ < ApplicationRecord
|
|||
|
||||
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 :format_datetime, if: Proc.new { type_champ == 'datetime' }
|
||||
before_save :multiple_select_to_string, if: Proc.new { type_champ == 'multiple_drop_down_list' }
|
||||
|
||||
scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) }
|
||||
scope :public_only, -> { where(private: false) }
|
||||
scope :private_only, -> { where(private: true) }
|
||||
|
@ -126,44 +122,4 @@ class Champ < ApplicationRecord
|
|||
|
||||
errors
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def format_date_to_iso
|
||||
date = begin
|
||||
Date.parse(value).iso8601
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
self.value = date
|
||||
end
|
||||
|
||||
def format_datetime
|
||||
if (value =~ /=>/).present?
|
||||
date = begin
|
||||
hash_date = YAML.safe_load(value.gsub('=>', ': '))
|
||||
year, month, day, hour, minute = hash_date.values_at(1,2,3,4,5)
|
||||
DateTime.new(year, month, day, hour, minute).strftime("%d/%m/%Y %H:%M")
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
self.value = date
|
||||
elsif /^\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}$/.match?(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}$/.match?(value)) # a datetime not correctly formatted should not be stored
|
||||
self.value = nil
|
||||
end
|
||||
end
|
||||
|
||||
def multiple_select_to_string
|
||||
if value.present?
|
||||
json = JSON.parse(value)
|
||||
if json == ['']
|
||||
self.value = nil
|
||||
else
|
||||
json = json - ['']
|
||||
self.value = json.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,2 +1,14 @@
|
|||
class Champs::DateChamp < Champ
|
||||
before_save :format_before_save
|
||||
|
||||
private
|
||||
|
||||
def format_before_save
|
||||
self.value =
|
||||
begin
|
||||
Date.parse(value).iso8601
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,2 +1,22 @@
|
|||
class Champs::DatetimeChamp < Champ
|
||||
before_save :format_before_save
|
||||
|
||||
private
|
||||
|
||||
def format_before_save
|
||||
if (value =~ /=>/).present?
|
||||
self.value =
|
||||
begin
|
||||
hash_date = YAML.safe_load(value.gsub('=>', ': '))
|
||||
year, month, day, hour, minute = hash_date.values_at(1,2,3,4,5)
|
||||
DateTime.new(year, month, day, hour, minute).strftime("%d/%m/%Y %H:%M")
|
||||
rescue
|
||||
nil
|
||||
end
|
||||
elsif /^\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}$/.match?(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}$/.match?(value)) # a datetime not correctly formatted should not be stored
|
||||
self.value = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,2 +1,17 @@
|
|||
class Champs::MultipleDropDownListChamp < Champ
|
||||
before_save :format_before_save
|
||||
|
||||
private
|
||||
|
||||
def format_before_save
|
||||
if value.present?
|
||||
json = JSON.parse(value)
|
||||
if json == ['']
|
||||
self.value = nil
|
||||
else
|
||||
json = json - ['']
|
||||
self.value = json.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue