From b4027e66869ef62e3e489655a4e4a82e5eb82c56 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 9 Sep 2024 11:02:27 -0300 Subject: [PATCH 1/4] feat: poder configurar la fecha por defecto #17333 --- app/models/metadata_date.rb | 19 ++++++++++++++++++- app/views/posts/attribute_ro/_date.haml | 2 +- app/views/posts/attributes/_date.haml | 2 +- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/models/metadata_date.rb b/app/models/metadata_date.rb index 9e655d4c..cdde77ce 100644 --- a/app/models/metadata_date.rb +++ b/app/models/metadata_date.rb @@ -1,8 +1,25 @@ # frozen_string_literal: true class MetadataDate < MetadataTemplate + # La fecha de hoy si no hay nada + # + # @return [Date,nil] def default_value - Date.today + case (dv = super) + when String + if dv.present? + begin + Date.parse(dv) + rescue Date::Error => e + ExceptionNotifier.notify_exception(e, data: { site: site.name, post: post.id, name:, type: }) + nil + end + else + nil + end + else + Date.today + end end # Devuelve una fecha, si no hay ninguna es la fecha de hoy. diff --git a/app/views/posts/attribute_ro/_date.haml b/app/views/posts/attribute_ro/_date.haml index cf8ea8ac..d7e275c7 100644 --- a/app/views/posts/attribute_ro/_date.haml +++ b/app/views/posts/attribute_ro/_date.haml @@ -1,3 +1,3 @@ %tr{ id: attribute } %th= post_label_t(attribute, post: post) - %td{ dir: dir, lang: locale }= l metadata.value.to_date + %td{ dir: dir, lang: locale }= l metadata.value diff --git a/app/views/posts/attributes/_date.haml b/app/views/posts/attributes/_date.haml index 1347c59c..f875c53e 100644 --- a/app/views/posts/attributes/_date.haml +++ b/app/views/posts/attributes/_date.haml @@ -1,6 +1,6 @@ .form-group = label_tag "#{base}_#{attribute}", post_label_t(attribute, post: post) - = date_field base, attribute, value: metadata.value.to_date.strftime('%F'), + = date_field base, attribute, value: metadata.value&.strftime('%F'), **field_options(attribute, metadata), pattern: '\d{4}-\d{2}-\d{2}', data: { 'pattern-mismatch': t('metadata.date.invalid_format') } = render 'posts/attribute_feedback', From 7a874f5a27b1a2d5714c97e4d6da99052c271ff3 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 2 Oct 2024 13:27:07 -0300 Subject: [PATCH 2/4] BREAKING CHANGE: la fecha por defecto viene del esquema o es nula --- app/models/metadata_date.rb | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/app/models/metadata_date.rb b/app/models/metadata_date.rb index cdde77ce..ecc5d06a 100644 --- a/app/models/metadata_date.rb +++ b/app/models/metadata_date.rb @@ -1,24 +1,21 @@ # frozen_string_literal: true class MetadataDate < MetadataTemplate - # La fecha de hoy si no hay nada + # La fecha de hoy si no hay nada. Podemos traer un valor por defecto + # desde el esquema, siempre y cuando pueda considerarse una fecha + # válida. # # @return [Date,nil] def default_value - case (dv = super) - when String - if dv.present? - begin - Date.parse(dv) - rescue Date::Error => e - ExceptionNotifier.notify_exception(e, data: { site: site.name, post: post.id, name:, type: }) - nil - end - else + if (dv = super.presence) + begin + Date.parse(dv) + # XXX: Notificar para que sepamos que el esquema no es válido. + # TODO: Validar el valor por defecto en sutty-schema-validator. + rescue Date::Error => e + ExceptionNotifier.notify_exception(e, data: { site: site.name, post: post.id, name:, type: }) nil end - else - Date.today end end From b9ec8a6756aa716c7693263afb02125c26c27655 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 14 Oct 2024 13:16:30 -0300 Subject: [PATCH 3/4] fix: date_field necesita strftime en el atributo closes #17530 closes #17575 closes #17576 closes #17577 closes #17580 closes #17581 closes #17582 closes #17584 closes #17586 closes #17588 closes #17589 closes #17590 --- app/models/metadata_date.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/models/metadata_date.rb b/app/models/metadata_date.rb index ecc5d06a..5a96871a 100644 --- a/app/models/metadata_date.rb +++ b/app/models/metadata_date.rb @@ -19,6 +19,14 @@ class MetadataDate < MetadataTemplate end end + # Delegar el formato al valor, para uso dentro de date_field() + # + # @param format [String] + # @return [String,nil] + def strftime(format) + value&.strftime(format) + end + # Devuelve una fecha, si no hay ninguna es la fecha de hoy. # # @return [Date] From 1c84e57c7e3f6802479e7847c041c66a7f6414f3 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 14 Oct 2024 15:03:31 -0300 Subject: [PATCH 4/4] fix: no localizar sin fecha closes #17593 --- app/views/posts/attribute_ro/_date.haml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/views/posts/attribute_ro/_date.haml b/app/views/posts/attribute_ro/_date.haml index d7e275c7..25ba9430 100644 --- a/app/views/posts/attribute_ro/_date.haml +++ b/app/views/posts/attribute_ro/_date.haml @@ -1,3 +1,5 @@ %tr{ id: attribute } %th= post_label_t(attribute, post: post) - %td{ dir: dir, lang: locale }= l metadata.value + %td{ dir: dir, lang: locale } + - if metadata.value + = l metadata.value