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::DateChamp < 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 dates so we’re not including these champs
|
|
|
|
|
end
|
|
|
|
|
|
2018-12-28 15:37:20 +01:00
|
|
|
|
def to_s
|
2019-11-25 14:39:42 +01:00
|
|
|
|
value.present? ? I18n.l(Time.zone.parse(value), format: '%d %B %Y') : ""
|
2018-12-28 15:37:20 +01:00
|
|
|
|
end
|
|
|
|
|
|
2019-09-11 16:04:42 +02:00
|
|
|
|
def for_tag
|
2019-11-25 14:39:42 +01:00
|
|
|
|
value.present? ? I18n.l(Time.zone.parse(value), format: '%d %B %Y') : ""
|
2019-09-11 16:04:42 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-06-14 17:41:31 +02:00
|
|
|
|
private
|
|
|
|
|
|
2023-01-12 17:42:02 +01:00
|
|
|
|
def convert_to_iso8601
|
2023-01-31 16:39:00 +01:00
|
|
|
|
return if likely_iso8601_format? && parsable_iso8601?
|
2023-01-12 17:42:02 +01:00
|
|
|
|
|
|
|
|
|
self.value = if /^\d{2}\/\d{2}\/\d{4}$/.match?(value)
|
|
|
|
|
Date.parse(value).iso8601
|
|
|
|
|
else
|
|
|
|
|
nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def iso_8601
|
2023-01-31 16:39:00 +01:00
|
|
|
|
return if parsable_iso8601? || value.blank?
|
2023-01-12 17:42:02 +01:00
|
|
|
|
# i18n-tasks-use t('errors.messages.not_a_date')
|
|
|
|
|
errors.add :date, errors.generate_message(:value, :not_a_date)
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-31 16:39:00 +01:00
|
|
|
|
def likely_iso8601_format?
|
2023-01-12 17:42:02 +01:00
|
|
|
|
/^\d{4}-\d{2}-\d{2}$/.match?(value)
|
2018-06-14 17:41:31 +02:00
|
|
|
|
end
|
2023-01-31 16:39:00 +01:00
|
|
|
|
|
|
|
|
|
def parsable_iso8601?
|
|
|
|
|
Date.parse(value)
|
|
|
|
|
true
|
|
|
|
|
rescue ArgumentError, # case 2023-27-02, out of range
|
|
|
|
|
TypeError # nil
|
|
|
|
|
false
|
|
|
|
|
end
|
2018-02-13 18:18:20 +01:00
|
|
|
|
end
|