From 780d26f79aa4fb5b87f8ccbcc58f8aea5ccc39e5 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 24 May 2024 13:13:58 -0300 Subject: [PATCH] feat: title es un atributo obligatorio MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit si no existe en el esquema, generar un título en base a sus valores --- app/models/metadata_array.rb | 6 +++++- app/models/metadata_belongs_to.rb | 4 ++++ app/models/metadata_predefined_value.rb | 4 ++++ app/models/metadata_related_posts.rb | 4 ++++ app/models/metadata_slug.rb | 2 +- app/models/metadata_string.rb | 4 ++++ app/models/metadata_template.rb | 5 +++++ app/models/metadata_text.rb | 6 +++++- app/models/metadata_title.rb | 18 ++++++++++++++++++ app/models/post.rb | 3 ++- app/views/posts/attribute_ro/_title.haml | 3 +++ app/views/posts/attributes/_title.haml | 1 + 12 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 app/models/metadata_title.rb create mode 100644 app/views/posts/attribute_ro/_title.haml create mode 100644 app/views/posts/attributes/_title.haml diff --git a/app/models/metadata_array.rb b/app/models/metadata_array.rb index 368aa546..a8a6e555 100644 --- a/app/models/metadata_array.rb +++ b/app/models/metadata_array.rb @@ -19,8 +19,12 @@ class MetadataArray < MetadataTemplate true && !private? end + def titleize? + true + end + def to_s - value.join(', ') + value.select(&:present?).join(', ') end # Obtiene el valor desde el documento, convirtiéndolo a Array si no lo diff --git a/app/models/metadata_belongs_to.rb b/app/models/metadata_belongs_to.rb index be1fa670..7dec4f94 100644 --- a/app/models/metadata_belongs_to.rb +++ b/app/models/metadata_belongs_to.rb @@ -13,6 +13,10 @@ class MetadataBelongsTo < MetadataRelatedPosts '' end + def to_s + belongs_to.try(:title).try(:value).to_s + end + # Obtiene el valor desde el documento. # # @return [String] diff --git a/app/models/metadata_predefined_value.rb b/app/models/metadata_predefined_value.rb index 9cf36382..dbdb1e48 100644 --- a/app/models/metadata_predefined_value.rb +++ b/app/models/metadata_predefined_value.rb @@ -10,6 +10,10 @@ class MetadataPredefinedValue < MetadataString @values ||= layout.dig(:metadata, name, 'values', I18n.locale.to_s)&.invert || {} end + def to_s + values.invert[value].to_s + end + private # Solo permite almacenar los valores predefinidos. diff --git a/app/models/metadata_related_posts.rb b/app/models/metadata_related_posts.rb index 2728020e..9e84c446 100644 --- a/app/models/metadata_related_posts.rb +++ b/app/models/metadata_related_posts.rb @@ -22,6 +22,10 @@ class MetadataRelatedPosts < MetadataArray false end + def titleize? + false + end + def indexable_values posts.where(uuid: value).map(&:title).map(&:value) end diff --git a/app/models/metadata_slug.rb b/app/models/metadata_slug.rb index b0fe8cec..417cfa0b 100644 --- a/app/models/metadata_slug.rb +++ b/app/models/metadata_slug.rb @@ -25,7 +25,7 @@ require 'jekyll/utils' class MetadataSlug < MetadataTemplate # Trae el slug desde el título si existe o una string al azar def default_value - title ? Jekyll::Utils.slugify(title, mode: site.slugify_mode) : SecureRandom.uuid + Jekyll::Utils.slugify(title || SecureRandom.uuid, mode: site.slugify_mode) end def value diff --git a/app/models/metadata_string.rb b/app/models/metadata_string.rb index c1d888b1..cb3ad264 100644 --- a/app/models/metadata_string.rb +++ b/app/models/metadata_string.rb @@ -11,6 +11,10 @@ class MetadataString < MetadataTemplate true && !private? end + def titleize? + true + end + private # No se permite HTML en las strings diff --git a/app/models/metadata_template.rb b/app/models/metadata_template.rb index c762220e..29391ac6 100644 --- a/app/models/metadata_template.rb +++ b/app/models/metadata_template.rb @@ -16,6 +16,11 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type, false end + # El valor puede ser parte de un título auto-generado + def titleize? + false + end + def inspect "#<#{self.class} site=#{site.name.inspect} post=#{post.id.inspect} value=#{value.inspect}>" end diff --git a/app/models/metadata_text.rb b/app/models/metadata_text.rb index 103bcd0a..3ac336bb 100644 --- a/app/models/metadata_text.rb +++ b/app/models/metadata_text.rb @@ -1,4 +1,8 @@ # frozen_string_literal: true # Un campo de texto largo -class MetadataText < MetadataString; end +class MetadataText < MetadataString + def titleize? + false + end +end diff --git a/app/models/metadata_title.rb b/app/models/metadata_title.rb new file mode 100644 index 00000000..c913878b --- /dev/null +++ b/app/models/metadata_title.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +# El título es obligatorio para todos los Post, si el esquema no lo +# incluye, tenemos que poder generar un valor legible por humanes. +class MetadataTitle < MetadataString + # Obtener todos los valores de texto del artículo y generar un título + # en base a eso. + # + # @return [String] + def default_value + @default_value ||= + post.attributes.select do |attr| + post[attr].titleize? + end.map do |attr| + post[attr].to_s + end.compact.join(' ').strip.squeeze(' ') + end +end diff --git a/app/models/post.rb b/app/models/post.rb index 70773023..64669524 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -12,11 +12,12 @@ class Post DEFAULT_ATTRIBUTES = %i[site document layout].freeze # Otros atributos que no vienen en los metadatos PRIVATE_ATTRIBUTES = %i[path slug attributes errors].freeze - PUBLIC_ATTRIBUTES = %i[lang date uuid created_at].freeze + PUBLIC_ATTRIBUTES = %i[title lang date uuid created_at].freeze ALIASED_ATTRIBUTES = %i[locale].freeze ATTR_SUFFIXES = %w[? =].freeze ATTRIBUTE_DEFINITIONS = { + 'title' => { 'type' => 'title', 'required' => true }, 'lang' => { 'type' => 'lang', 'required' => true }, 'date' => { 'type' => 'document_date', 'required' => true }, 'uuid' => { 'type' => 'uuid', 'required' => true }, diff --git a/app/views/posts/attribute_ro/_title.haml b/app/views/posts/attribute_ro/_title.haml new file mode 100644 index 00000000..67642e2c --- /dev/null +++ b/app/views/posts/attribute_ro/_title.haml @@ -0,0 +1,3 @@ +%tr{ id: attribute } + %th= post_label_t(attribute, post: post) + %td{ dir: dir, lang: locale }= metadata.value diff --git a/app/views/posts/attributes/_title.haml b/app/views/posts/attributes/_title.haml new file mode 100644 index 00000000..d94afdbb --- /dev/null +++ b/app/views/posts/attributes/_title.haml @@ -0,0 +1 @@ +-#