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