5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-22 23:26:21 +00:00
panel/app/models/post/indexable.rb

80 lines
2.1 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
class Post
# Vuelve indexables a los Posts
module Indexable
extend ActiveSupport::Concern
included do
# @return [IndexedPost,nil]
def indexed_post
site.indexed_posts.find_by_post_id(uuid.value)
end
# Devuelve una versión indexable del Post
#
# @return [IndexedPost]
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|
indexed_post.layout = layout.name
indexed_post.path = path.basename
indexed_post.locale = locale.value
indexed_post.dictionary = IndexedPost.to_dictionary(locale: locale.value)
indexed_post.title = title.value
indexed_post.front_matter = indexable_front_matter
indexed_post.content = indexable_content
indexed_post.created_at = date.value
indexed_post.order = attribute?(:order) ? order.value : 0
end
end
# Indexa o reindexa el Post
#
# @return [Boolean]
def index!
to_index.save
end
def remove_from_index!
to_index.destroy.destroyed?
end
2022-08-03 16:11:06 +00:00
private
2023-10-06 13:12:18 +00:00
# Los metadatos que se almacenan como objetos JSON.
#
# @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
2023-10-06 13:12:18 +00:00
indexable_attributes.select do |attr|
self[attr].front_matter?
end.each do |attr|
ifm[attr] = self[attr].try(:indexable_values)
2023-10-06 13:12:18 +00:00
end
end
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|
self[attr].to_s.tr("\n", ' ')
end.join("\n").squeeze("\n")
end
def indexable_attributes
@indexable_attributes ||= attributes.select do |attr|
2021-05-14 19:59:47 +00:00
self[attr].indexable?
end
end
end
end
end