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-06-16 14:35:37 +00:00
|
|
|
# @return [Time]
|
|
|
|
def document_value
|
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-06 15:33:28 +00:00
|
|
|
def indexable?
|
2021-05-09 15:25:16 +00:00
|
|
|
true && !private?
|
2021-05-06 15:33:28 +00:00
|
|
|
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
|
2021-06-16 14:35:37 +00:00
|
|
|
document_value || default_value
|
2021-05-13 14:44:09 +00:00
|
|
|
end
|
|
|
|
else
|
2021-06-16 14:35:37 +00:00
|
|
|
self[:value] || document_value || 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
|
2019-08-13 23:33:57 +00:00
|
|
|
|
2021-05-13 15:52:49 +00:00
|
|
|
def valid_format?
|
|
|
|
return true if self[:value].is_a?(Time)
|
2020-05-11 21:55:30 +00:00
|
|
|
|
2021-05-13 15:52:49 +00:00
|
|
|
@valid_format_re ||= /\A\d{2,4}-\d{1,2}-\d{1,2}\z/
|
|
|
|
@valid_format_re =~ self[:value].to_s
|
2019-08-13 23:33:57 +00:00
|
|
|
end
|
2019-08-07 21:35:37 +00:00
|
|
|
end
|