2019-08-08 18:28:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-07 21:35:37 +00:00
|
|
|
# Maneja la fecha del document
|
|
|
|
class MetadataDocumentDate < MetadataTemplate
|
|
|
|
# La fecha por defecto es ahora!
|
|
|
|
def default_value
|
|
|
|
Date.today.to_time
|
|
|
|
end
|
|
|
|
|
2021-02-17 21:40:07 +00:00
|
|
|
def value_from_document
|
2021-05-17 15:43:23 +00:00
|
|
|
return nil if post.new?
|
2021-05-14 20:41:44 +00:00
|
|
|
|
2021-02-17 21:40:07 +00:00
|
|
|
document.date
|
|
|
|
end
|
|
|
|
|
2021-05-13 15:52:49 +00:00
|
|
|
# Siempre es obligatorio
|
|
|
|
def required
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
|
|
|
super
|
|
|
|
|
|
|
|
errors << I18n.t('metadata.date.invalid_format') unless valid_format?
|
|
|
|
|
|
|
|
errors.empty?
|
|
|
|
end
|
|
|
|
|
2020-02-12 21:24:54 +00:00
|
|
|
# El valor puede ser un Date, Time o una String en el formato
|
|
|
|
# "yyyy-mm-dd"
|
2021-05-13 14:44:09 +00:00
|
|
|
#
|
|
|
|
# XXX: Date.iso8601 acepta fechas en el futuro lejano, como 20000,
|
|
|
|
# pero Jekyll las limita a cuatro cifras, así que vamos a mantener
|
|
|
|
# eso.
|
|
|
|
#
|
|
|
|
# @see {https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb#L15}
|
2019-08-07 21:35:37 +00:00
|
|
|
def value
|
2021-05-13 14:44:09 +00:00
|
|
|
self[:value] =
|
|
|
|
case self[:value]
|
|
|
|
when String
|
|
|
|
begin
|
|
|
|
Date.iso8601(self[:value]).to_time
|
|
|
|
rescue Date::Error
|
|
|
|
value_from_document || default_value
|
|
|
|
end
|
|
|
|
else
|
2021-05-14 20:41:44 +00:00
|
|
|
self[:value] || value_from_document || default_value
|
2021-05-13 14:44:09 +00:00
|
|
|
end
|
2019-08-13 23:33:57 +00:00
|
|
|
end
|
2021-05-13 15:52:49 +00:00
|
|
|
|
|
|
|
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
|
2019-08-07 21:35:37 +00:00
|
|
|
end
|