From b4467e4d569d515753b260e5909dbc93988ee385 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 24 May 2024 13:12:56 -0300 Subject: [PATCH] refactor: agregar definiciones por defecto al post --- app/models/post.rb | 59 ++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 39 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index e6910e18..70773023 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -13,8 +13,19 @@ class Post # 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 + ALIASED_ATTRIBUTES = %i[locale].freeze ATTR_SUFFIXES = %w[? =].freeze + ATTRIBUTE_DEFINITIONS = { + 'lang' => { 'type' => 'lang', 'required' => true }, + 'date' => { 'type' => 'document_date', 'required' => true }, + 'uuid' => { 'type' => 'uuid', 'required' => true }, + 'created_at' => { 'type' => 'created_at', 'required' => true }, + 'slug' => { 'type' => 'slug', 'required' => true }, + 'path' => { 'type' => 'path', 'required' => true }, + 'locale' => { 'alias' => 'lang' }, + } + class PostError < StandardError; end class UnknownAttributeError < PostError; end @@ -49,10 +60,12 @@ class Post @layout = args[:layout] @site = args[:site] @document = args[:document] - @attributes = layout.attributes + PUBLIC_ATTRIBUTES + @attributes = (layout.attributes + PUBLIC_ATTRIBUTES).uniq @errors = {} @metadata = {} + layout.metadata = ATTRIBUTE_DEFINITIONS.merge(layout.metadata).with_indifferent_access + # Leer el documento si existe # @todo Asignar todos los valores a self[:value] luego de leer document&.read! unless new? @@ -195,6 +208,10 @@ class Post define_singleton_method(name) do template = layout.metadata[name.to_s] + if template.key?('alias') + return public_send(template['alias'].to_sym) + end + @metadata[name] ||= MetadataFactory.build(document: document, post: self, @@ -210,43 +227,6 @@ class Post public_send name end - # TODO: Mover a method_missing - def slug - @metadata[:slug] ||= MetadataSlug.new(document: document, site: site, layout: layout, name: :slug, type: :slug, - post: self, required: true) - end - - # TODO: Mover a method_missing - def date - @metadata[:date] ||= MetadataDocumentDate.new(document: document, site: site, layout: layout, name: :date, - type: :document_date, post: self, required: true) - end - - # TODO: Mover a method_missing - def path - @metadata[:path] ||= MetadataPath.new(document: document, site: site, layout: layout, name: :path, type: :path, - post: self, required: true) - end - - # TODO: Mover a method_missing - def lang - @metadata[:lang] ||= MetadataLang.new(document: document, site: site, layout: layout, name: :lang, type: :lang, - post: self, required: true) - end - - alias locale lang - - # TODO: Mover a method_missing - def uuid - @metadata[:uuid] ||= MetadataUuid.new(document: document, site: site, layout: layout, name: :uuid, type: :uuid, - post: self, required: true) - end - - # La fecha de creación inmodificable del post - def created_at - @metadata[:created_at] ||= MetadataCreatedAt.new(document: document, site: site, layout: layout, name: :created_at, type: :created_at, post: self, required: true) - end - # Devuelve los strong params para el layout. # # XXX: Nos gustaría no tener que instanciar Metadata acá, pero depende @@ -423,7 +403,8 @@ class Post def attribute?(mid) included = DEFAULT_ATTRIBUTES.include?(mid) || PRIVATE_ATTRIBUTES.include?(mid) || - PUBLIC_ATTRIBUTES.include?(mid) + PUBLIC_ATTRIBUTES.include?(mid) || + ALIASED_ATTRIBUTES.include?(mid) included = attributes.include? mid if !included && singleton_class.method_defined?(:attributes)