From 8a5e965e617142250debe2a0ae3bf04a223e54e8 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 13 May 2021 11:44:09 -0300 Subject: [PATCH 1/4] validar la fecha MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit una fecha en el futuro como 20213-01-01 es válida para ruby, pero jekyll valida las fechas con cuatro digitos nada mas (issue pendiente :P), así que tomaba el archivo con fecha en el futurísimo como un archivo sin fecha. fixes #1573 fixes #1572 --- app/models/metadata_document_date.rb | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/app/models/metadata_document_date.rb b/app/models/metadata_document_date.rb index 39e6873..f6ad7d0 100644 --- a/app/models/metadata_document_date.rb +++ b/app/models/metadata_document_date.rb @@ -13,11 +13,25 @@ class MetadataDocumentDate < MetadataTemplate # 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 + raise Date::Error unless /\A\d{2,4}-\d{1,2}-\d{1,2}\z/ ~= self[:value] - self[:value] = Date.iso8601(self[:value]).to_time if self[:value].is_a? String - - self[:value] + Date.iso8601(self[:value]).to_time + rescue Date::Error + value_from_document || default_value + end + else + value_from_document || default_value + end end end From f5834b5c1bbd647530b7ca160343832aeeedcb4e Mon Sep 17 00:00:00 2001 From: f Date: Thu, 13 May 2021 12:52:49 -0300 Subject: [PATCH 2/4] en lugar de asumir un valor, informar el error --- app/models/metadata_document_date.rb | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) 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 From 64b59bfc4dc996f9a0e2391dc328cba9a13a9631 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 14 May 2021 17:41:44 -0300 Subject: [PATCH 3/4] si el post ya tiene fecha, no cambiarla --- app/models/metadata_document_date.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/metadata_document_date.rb b/app/models/metadata_document_date.rb index 82126ba..312bb3a 100644 --- a/app/models/metadata_document_date.rb +++ b/app/models/metadata_document_date.rb @@ -8,6 +8,8 @@ class MetadataDocumentDate < MetadataTemplate end def value_from_document + return nil unless document.path + document.date end @@ -42,7 +44,7 @@ class MetadataDocumentDate < MetadataTemplate value_from_document || default_value end else - value_from_document || default_value + self[:value] || value_from_document || default_value end end From e19713f5df355150a99b07c2d20f3a5be2693815 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 17 May 2021 12:43:23 -0300 Subject: [PATCH 4/4] =?UTF-8?q?usar=20Post#new=3F=20que=20ya=20sabe=20cu?= =?UTF-8?q?=C3=A1ndo=20el=20documento=20no=20existe=20aun?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/metadata_document_date.rb | 2 +- app/models/post.rb | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/models/metadata_document_date.rb b/app/models/metadata_document_date.rb index 312bb3a..7512cbf 100644 --- a/app/models/metadata_document_date.rb +++ b/app/models/metadata_document_date.rb @@ -8,7 +8,7 @@ class MetadataDocumentDate < MetadataTemplate end def value_from_document - return nil unless document.path + return nil if post.new? document.date end diff --git a/app/models/post.rb b/app/models/post.rb index 461733f..13e8a91 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