From 59a6b94f5e3bfd6e4bf82210e0f9b397037a4546 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 11 Nov 2020 17:00:09 -0300 Subject: [PATCH] cachear metadatos #177 --- app/models/metadata_template.rb | 18 ++++++++++++++++-- app/views/posts/_form.haml | 13 +++++++------ app/views/posts/show.haml | 23 +++++++++++++---------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/app/models/metadata_template.rb b/app/models/metadata_template.rb index 0e1f593e..0e2e9c70 100644 --- a/app/models/metadata_template.rb +++ b/app/models/metadata_template.rb @@ -4,7 +4,6 @@ # # TODO: Validar el tipo de valor pasado a value= segĂșn el :type # -# rubocop:disable Metrics/BlockLength MetadataTemplate = Struct.new(:site, :document, :name, :label, :type, :value, :help, :required, :errors, :post, :layout, keyword_init: true) do @@ -12,6 +11,22 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type, attr_reader :value_was + # Queremos que los artĂ­culos nuevos siempre cacheen, si usamos el UUID + # siempre vamos a obtener un item nuevo. + def cache_key + return name.to_s if post.new? + + @cache_key ||= 'post/' + post.uuid.value + '/' + name.to_s + end + + def cache_version + value.hash.to_s + end + + def cache_key_with_version + cache_key + '-' + cache_version + end + def value=(new_value) @value_was = value self[:value] = new_value @@ -162,4 +177,3 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type, @box ||= Lockbox.new key: site.private_key, padding: true, encode: true end end -# rubocop:enable Metrics/BlockLength diff --git a/app/views/posts/_form.haml b/app/views/posts/_form.haml index ef9f51a4..ef16484b 100644 --- a/app/views/posts/_form.haml +++ b/app/views/posts/_form.haml @@ -40,14 +40,15 @@ -# Dibuja cada atributo - post.attributes.each do |attribute| - - metadata = post.send(attribute) + - metadata = post[attribute] - type = metadata.type - = render("posts/attributes/#{type}", - post: post, attribute: attribute, - metadata: metadata, site: site, - dir: dir, locale: @locale, - autofocus: (post.attributes.first == attribute)) + - cache metadata do + = render("posts/attributes/#{type}", + post: post, attribute: attribute, + metadata: metadata, site: site, + dir: dir, locale: @locale, + autofocus: (post.attributes.first == attribute)) -# Botones de guardado = render 'posts/submit', site: site, post: post diff --git a/app/views/posts/show.haml b/app/views/posts/show.haml index 52069a0d..20397b3d 100644 --- a/app/views/posts/show.haml +++ b/app/views/posts/show.haml @@ -26,17 +26,20 @@ - metadata = @post[attr] - next unless metadata.front_matter? - = render("posts/attribute_ro/#{metadata.type}", - post: @post, attribute: attr, - metadata: metadata, - site: @site, - tags: all_html_tags, - locale: @locale, - dir: dir) + - cache metadata do + = render("posts/attribute_ro/#{metadata.type}", + post: @post, attribute: attr, + metadata: metadata, + site: @site, + tags: all_html_tags, + locale: @locale, + dir: dir) -# Mostrar todo lo que no va en el front_matter (el contenido) - @post.attributes.each do |attr| - - next if @post.send(attr).front_matter? + - metadata - @post[attr] + - next if metadata.front_matter? - %section{ id: attr, dir: dir } - = @post.send(attr).to_s.html_safe + - cache metadata do + %section{ id: attr, dir: dir } + = @post.public_send(attr).to_s.html_safe