2021-05-06 15:52:30 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# La representación indexable de un artículo
|
|
|
|
class IndexedPost < ApplicationRecord
|
|
|
|
include PgSearch::Model
|
|
|
|
|
|
|
|
# La traducción del locale según Sutty al locale según PostgreSQL
|
|
|
|
DICTIONARIES = {
|
|
|
|
es: 'spanish',
|
|
|
|
en: 'english'
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
# TODO: Los indexed posts tienen que estar scopeados al idioma actual,
|
|
|
|
# no buscar sobre todos
|
|
|
|
pg_search_scope :search,
|
|
|
|
lambda { |locale, query|
|
|
|
|
{
|
|
|
|
against: :content,
|
|
|
|
query: query,
|
|
|
|
using: {
|
|
|
|
tsearch: {
|
2021-05-07 22:25:09 +00:00
|
|
|
dictionary: dictionary,
|
2021-05-06 15:52:30 +00:00
|
|
|
tsvector_column: 'indexed_content'
|
2021-05-07 19:21:44 +00:00
|
|
|
},
|
|
|
|
trigram: {
|
|
|
|
word_similarity: true
|
2021-05-06 15:52:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 22:07:11 +00:00
|
|
|
# Trae los IndexedPost en el orden en que van a terminar en el sitio.
|
|
|
|
default_scope lambda { order(order: :desc, created_at: :desc) }
|
2021-05-07 19:17:25 +00:00
|
|
|
scope :in_category, lambda { |category| where("front_matter->'categories' ? :category", category: category.to_s) }
|
|
|
|
scope :by_usuarie, lambda { |usuarie| where("front_matter->'usuaries' @> :usuarie::jsonb", usuarie: usuarie.to_s) }
|
2021-05-06 22:07:11 +00:00
|
|
|
|
2021-05-06 15:52:30 +00:00
|
|
|
belongs_to :site
|
|
|
|
|
|
|
|
# Convertir locale a direccionario de PG
|
|
|
|
#
|
|
|
|
# @param [String,Symbol]
|
|
|
|
# @return [String]
|
|
|
|
def self.to_dictionary(locale:)
|
|
|
|
DICTIONARIES[locale.to_sym] || 'simple'
|
|
|
|
end
|
|
|
|
end
|