mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-15 04:11:41 +00:00
en lugar de asumir un valor, informar el error
This commit is contained in:
parent
8a5e965e61
commit
f5834b5c1b
1 changed files with 22 additions and 2 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue