demarches-normaliennes/app/models/champs/datetime_champ.rb
Damien Le Thiec 4b0e6ea382
Convert datetime field to datetime_local (#8572)
* Convert datetime field to datetime_local

* Fix tests

* Add value formatting view datetime component

* Add helper datetime component value
2023-02-09 15:18:38 +01:00

71 lines
2.4 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# == Schema Information
#
# Table name: champs
#
# id :integer not null, primary key
# data :jsonb
# fetch_external_data_exceptions :string is an Array
# prefilled :boolean default(FALSE)
# private :boolean default(FALSE), not null
# rebased_at :datetime
# type :string
# value :string
# value_json :jsonb
# created_at :datetime
# updated_at :datetime
# dossier_id :integer
# etablissement_id :integer
# external_id :string
# parent_id :bigint
# row_id :string
# type_de_champ_id :integer
#
class Champs::DatetimeChamp < Champ
before_validation :convert_to_iso8601, unless: -> { validation_context == :prefill }
validate :iso_8601
def search_terms
# Text search is pretty useless for datetimes so were not including these champs
end
def to_s
value.present? ? I18n.l(Time.zone.parse(value)) : ""
end
def for_tag
value.present? ? I18n.l(Time.zone.parse(value)) : ""
end
private
def convert_to_iso8601
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)
Time.zone.local(year, month, day, hour, minute).iso8601
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 = 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
self.value = nil
end
end
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
end