From f5834b5c1bbd647530b7ca160343832aeeedcb4e Mon Sep 17 00:00:00 2001 From: f Date: Thu, 13 May 2021 12:52:49 -0300 Subject: [PATCH] en lugar de asumir un valor, informar el error --- app/models/metadata_document_date.rb | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/models/metadata_document_date.rb b/app/models/metadata_document_date.rb index f6ad7d0..82126ba 100644 --- a/app/models/metadata_document_date.rb +++ b/app/models/metadata_document_date.rb @@ -11,6 +11,19 @@ class MetadataDocumentDate < MetadataTemplate document.date end + # Siempre es obligatorio + def required + true + end + + def validate + super + + errors << I18n.t('metadata.date.invalid_format') unless valid_format? + + errors.empty? + end + # El valor puede ser un Date, Time o una String en el formato # "yyyy-mm-dd" # @@ -24,8 +37,6 @@ class MetadataDocumentDate < MetadataTemplate case self[:value] when String begin - raise Date::Error unless /\A\d{2,4}-\d{1,2}-\d{1,2}\z/ ~= self[:value] - Date.iso8601(self[:value]).to_time rescue Date::Error value_from_document || default_value @@ -34,4 +45,13 @@ class MetadataDocumentDate < MetadataTemplate value_from_document || default_value end end + + private + + def valid_format? + return true if self[:value].is_a?(Time) + + @valid_format_re ||= /\A\d{2,4}-\d{1,2}-\d{1,2}\z/ + @valid_format_re =~ self[:value].to_s + end end