5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-01 22:26:06 +00:00

actualizar el índice cuando se agrega o modifica un post

además, activa callbacks de activerecord de forma que Post se va
pareciendo cada vez más a un modelo de rails :D
This commit is contained in:
f 2021-05-06 19:46:36 -03:00
parent d61d1cad56
commit fd7ab8d7ef
2 changed files with 16 additions and 3 deletions

View file

@ -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

View file

@ -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.