2021-05-06 15:52:30 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Post
|
|
|
|
# Vuelve indexables a los Posts
|
|
|
|
module Indexable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2021-05-06 22:46:36 +00:00
|
|
|
# Indexa o reindexa el Post
|
|
|
|
after_save :index!
|
2021-05-07 21:06:49 +00:00
|
|
|
after_destroy :remove_from_index!
|
2021-05-06 22:46:36 +00:00
|
|
|
|
2021-05-06 15:52:30 +00:00
|
|
|
# Devuelve una versión indexable del Post
|
|
|
|
#
|
2021-05-07 19:17:25 +00:00
|
|
|
# @return [IndexedPost]
|
2021-05-06 15:52:30 +00:00
|
|
|
def to_index
|
2022-08-03 16:11:06 +00:00
|
|
|
IndexedPost.find_or_initialize_by(post_id: uuid.value, site_id: site.id).tap do |indexed_post|
|
2021-05-06 15:52:30 +00:00
|
|
|
indexed_post.layout = layout.name
|
|
|
|
indexed_post.path = path.basename
|
2021-05-07 22:25:09 +00:00
|
|
|
indexed_post.locale = locale.value
|
|
|
|
indexed_post.dictionary = IndexedPost.to_dictionary(locale: locale.value)
|
2021-05-06 15:52:30 +00:00
|
|
|
indexed_post.title = title.value
|
|
|
|
indexed_post.front_matter = indexable_front_matter
|
|
|
|
indexed_post.content = indexable_content
|
2021-05-06 22:07:11 +00:00
|
|
|
indexed_post.created_at = date.value
|
|
|
|
indexed_post.order = attribute?(:order) ? order.value : 0
|
2021-05-06 15:52:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-06 22:46:36 +00:00
|
|
|
# Indexa o reindexa el Post
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def index!
|
|
|
|
to_index.save
|
|
|
|
end
|
|
|
|
|
2021-05-07 21:06:49 +00:00
|
|
|
def remove_from_index!
|
|
|
|
to_index.destroy.destroyed?
|
|
|
|
end
|
|
|
|
|
2022-08-03 16:11:06 +00:00
|
|
|
private
|
|
|
|
|
2021-05-06 15:52:30 +00:00
|
|
|
# Los metadatos que se almacenan como objetos JSON. Empezamos con
|
|
|
|
# las categorías porque se usan para filtrar en el listado de
|
|
|
|
# artículos.
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
|
|
|
def indexable_front_matter
|
2021-05-14 19:59:47 +00:00
|
|
|
{}.tap do |ifm|
|
|
|
|
ifm[:usuaries] = usuaries.map(&:id)
|
|
|
|
ifm[:draft] = attribute?(:draft) ? draft.value : false
|
|
|
|
ifm[:categories] = categories.indexable_values if attribute? :categories
|
2021-05-07 19:17:25 +00:00
|
|
|
end
|
2021-05-06 15:52:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Devuelve un documento indexable en texto plano
|
|
|
|
#
|
|
|
|
# XXX: No memoizamos para permitir actualizaciones, aunque
|
|
|
|
# probablemente se indexe una sola vez.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def indexable_content
|
|
|
|
indexable_attributes.map do |attr|
|
2021-05-07 19:21:59 +00:00
|
|
|
self[attr].to_s.tr("\n", ' ')
|
2021-05-06 22:47:43 +00:00
|
|
|
end.join("\n").squeeze("\n")
|
2021-05-06 15:52:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def indexable_attributes
|
|
|
|
@indexable_attributes ||= attributes.select do |attr|
|
2021-05-14 19:59:47 +00:00
|
|
|
self[attr].indexable?
|
|
|
|
end
|
2021-05-06 15:52:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|