From fd7ab8d7ef1190ce460eb967f922182992bdacdf Mon Sep 17 00:00:00 2001 From: f Date: Thu, 6 May 2021 19:46:36 -0300 Subject: [PATCH] =?UTF-8?q?actualizar=20el=20=C3=ADndice=20cuando=20se=20a?= =?UTF-8?q?grega=20o=20modifica=20un=20post?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit además, activa callbacks de activerecord de forma que Post se va pareciendo cada vez más a un modelo de rails :D --- app/models/post.rb | 7 +++++-- app/models/post/indexable.rb | 12 +++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 790ee1a2..7964d4ee 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -17,6 +17,7 @@ class Post attr_reader :attributes, :errors, :layout, :site, :document + include ActiveRecord::Callbacks include Post::Indexable class << self @@ -285,8 +286,10 @@ class Post end end - return false unless save_attributes! - return false unless write + run_callbacks :save do + return false unless save_attributes! + return false unless write + end # Vuelve a leer el post para tomar los cambios read diff --git a/app/models/post/indexable.rb b/app/models/post/indexable.rb index 9b92111f..b7b5a7fa 100644 --- a/app/models/post/indexable.rb +++ b/app/models/post/indexable.rb @@ -6,11 +6,14 @@ class Post extend ActiveSupport::Concern included do + # Indexa o reindexa el Post + after_save :index! + # Devuelve una versión indexable del Post # # @return [IndexedPosts] def to_index - @to_index ||= IndexedPost.find_or_create_by(id: uuid.value).tap do |indexed_post| + IndexedPost.find_or_create_by(id: uuid.value).tap do |indexed_post| indexed_post.layout = layout.name indexed_post.site_id = site.id indexed_post.path = path.basename @@ -25,6 +28,13 @@ class Post private + # Indexa o reindexa el Post + # + # @return [Boolean] + def index! + to_index.save + end + # Los metadatos que se almacenan como objetos JSON. Empezamos con # las categorías porque se usan para filtrar en el listado de # artículos.