2020-08-06 16:35:45 +02:00
|
|
|
|
# == Schema Information
|
|
|
|
|
#
|
|
|
|
|
# Table name: champs
|
|
|
|
|
#
|
2021-02-04 19:23:40 +01:00
|
|
|
|
# id :integer not null, primary key
|
|
|
|
|
# data :jsonb
|
|
|
|
|
# fetch_external_data_exceptions :string is an Array
|
2022-12-01 12:00:21 +01:00
|
|
|
|
# prefilled :boolean default(FALSE)
|
2021-02-04 19:23:40 +01:00
|
|
|
|
# private :boolean default(FALSE), not null
|
2021-10-20 17:26:09 +02:00
|
|
|
|
# rebased_at :datetime
|
2021-02-04 19:23:40 +01:00
|
|
|
|
# type :string
|
|
|
|
|
# value :string
|
2021-10-05 15:37:13 +02:00
|
|
|
|
# value_json :jsonb
|
2021-02-04 19:23:40 +01:00
|
|
|
|
# created_at :datetime
|
|
|
|
|
# updated_at :datetime
|
2022-09-21 15:20:42 +02:00
|
|
|
|
# dossier_id :integer
|
2021-02-04 19:23:40 +01:00
|
|
|
|
# etablissement_id :integer
|
|
|
|
|
# external_id :string
|
|
|
|
|
# parent_id :bigint
|
2022-12-16 12:39:51 +01:00
|
|
|
|
# row_id :string
|
2022-09-21 15:20:42 +02:00
|
|
|
|
# type_de_champ_id :integer
|
2020-08-06 16:35:45 +02:00
|
|
|
|
#
|
2018-02-13 18:18:20 +01:00
|
|
|
|
class Champs::DatetimeChamp < Champ
|
2023-01-12 17:42:02 +01:00
|
|
|
|
before_validation :convert_to_iso8601, unless: -> { validation_context == :prefill }
|
|
|
|
|
validate :iso_8601
|
2018-06-14 17:41:31 +02:00
|
|
|
|
|
2018-07-25 19:34:06 +02:00
|
|
|
|
def search_terms
|
|
|
|
|
# Text search is pretty useless for datetimes so we’re not including these champs
|
|
|
|
|
end
|
|
|
|
|
|
2019-05-22 17:54:17 +02:00
|
|
|
|
def to_s
|
|
|
|
|
value.present? ? I18n.l(Time.zone.parse(value)) : ""
|
|
|
|
|
end
|
|
|
|
|
|
2019-09-11 16:04:42 +02:00
|
|
|
|
def for_tag
|
|
|
|
|
value.present? ? I18n.l(Time.zone.parse(value)) : ""
|
|
|
|
|
end
|
|
|
|
|
|
2020-03-23 16:50:59 +01:00
|
|
|
|
def html_label?
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
2018-06-14 17:41:31 +02:00
|
|
|
|
private
|
|
|
|
|
|
2023-01-12 17:42:02 +01:00
|
|
|
|
def convert_to_iso8601
|
2018-06-14 17:41:31 +02:00
|
|
|
|
if (value =~ /=>/).present?
|
|
|
|
|
self.value =
|
|
|
|
|
begin
|
|
|
|
|
hash_date = YAML.safe_load(value.gsub('=>', ': '))
|
2018-12-24 17:37:57 +01:00
|
|
|
|
year, month, day, hour, minute = hash_date.values_at(1, 2, 3, 4, 5)
|
2023-01-12 17:42:02 +01:00
|
|
|
|
Time.zone.local(year, month, day, hour, minute).iso8601
|
2018-06-14 17:41:31 +02:00
|
|
|
|
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
|
2023-01-12 17:42:02 +01:00
|
|
|
|
self.value = Time.zone.strptime(value, "%d/%m/%Y %H:%M").iso8601
|
|
|
|
|
elsif /^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}$/.match?(value)
|
|
|
|
|
self.value = Time.zone.strptime(value, "%Y-%m-%d %H:%M").iso8601
|
|
|
|
|
elsif valid_iso8601? # a correct iso8601 datetime
|
|
|
|
|
self.value = Time.zone.strptime(value, "%Y-%m-%dT%H:%M").iso8601
|
|
|
|
|
else
|
2018-06-14 17:41:31 +02:00
|
|
|
|
self.value = nil
|
|
|
|
|
end
|
|
|
|
|
end
|
2023-01-12 17:42:02 +01:00
|
|
|
|
|
|
|
|
|
def iso_8601
|
|
|
|
|
return if valid_iso8601? || value.blank?
|
|
|
|
|
# i18n-tasks-use t('errors.messages.not_a_datetime')
|
|
|
|
|
errors.add :datetime, errors.generate_message(:value, :not_a_datetime)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def valid_iso8601?
|
|
|
|
|
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(:\d{2}\+\d{2}:\d{2})?$/.match?(value)
|
|
|
|
|
end
|
2018-02-13 18:18:20 +01:00
|
|
|
|
end
|