diff --git a/app/models/metadata_document_date.rb b/app/models/metadata_document_date.rb index 39e68735..7512cbfb 100644 --- a/app/models/metadata_document_date.rb +++ b/app/models/metadata_document_date.rb @@ -8,16 +8,52 @@ class MetadataDocumentDate < MetadataTemplate end def value_from_document + return nil if post.new? + 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" + # + # 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} def value - return (self[:value] = value_from_document || default_value) if self[:value].nil? + self[:value] = + case self[:value] + when String + begin + Date.iso8601(self[:value]).to_time + rescue Date::Error + value_from_document || default_value + end + else + self[:value] || value_from_document || default_value + end + end - self[:value] = Date.iso8601(self[:value]).to_time if self[:value].is_a? String + private - self[:value] + 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 diff --git a/app/models/post.rb b/app/models/post.rb index d5f77092..31a269e7 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -49,9 +49,7 @@ class Post public_send(attr)&.value = args[attr] if args.key?(attr) end - # XXX: No usamos Post#read porque a esta altura todavía no sabemos - # nada del Document - document.read! if File.exist? document.path + document.read! unless new? end def inspect