mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-16 12:41:41 +00:00
evitar confusiones entre diccionario y locale
el diccionario es lo que usa internamente pg para indexar, el locale es el idioma que asignamos en sutty.
This commit is contained in:
parent
d2ae406023
commit
6396841b2c
5 changed files with 43 additions and 21 deletions
|
@ -17,13 +17,12 @@ class PostsController < ApplicationController
|
|||
|
||||
@site = find_site
|
||||
@locale = locale
|
||||
dictionary = IndexedPost.to_dictionary(locale: locale)
|
||||
|
||||
# XXX: Cada vez que cambiamos un Post tocamos el sitio con lo que es
|
||||
# más simple saber si hubo cambios.
|
||||
if filter_params.present? || stale?(@site)
|
||||
# Todos los artículos de este sitio para el idioma actual
|
||||
@posts = @site.indexed_posts.where(locale: dictionary)
|
||||
@posts = @site.indexed_posts.where(locale: locale)
|
||||
# De este tipo
|
||||
@posts = @posts.where(layout: filter_params[:layout]) if filter_params[:layout]
|
||||
# Que estén dentro de la categoría
|
||||
|
|
|
@ -19,7 +19,7 @@ class IndexedPost < ApplicationRecord
|
|||
query: query,
|
||||
using: {
|
||||
tsearch: {
|
||||
dictionary: IndexedPost.to_dictionary(locale: locale),
|
||||
dictionary: dictionary,
|
||||
tsvector_column: 'indexed_content'
|
||||
},
|
||||
trigram: {
|
||||
|
|
|
@ -18,7 +18,8 @@ class Post
|
|||
indexed_post.layout = layout.name
|
||||
indexed_post.site_id = site.id
|
||||
indexed_post.path = path.basename
|
||||
indexed_post.locale = IndexedPost.to_dictionary(locale: locale.value)
|
||||
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
|
||||
|
|
29
db/migrate/20210507221120_add_dictionary_to_indexed_posts.rb
Normal file
29
db/migrate/20210507221120_add_dictionary_to_indexed_posts.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Para no estar calculando todo el tiempo el diccionario del idioma,
|
||||
# agregamos una columna más.
|
||||
class AddDictionaryToIndexedPosts < ActiveRecord::Migration[6.1]
|
||||
LOCALES = {
|
||||
'english' => 'en',
|
||||
'spanish' => 'es'
|
||||
}
|
||||
|
||||
def up
|
||||
add_column :indexed_posts, :dictionary, :string
|
||||
|
||||
create_trigger(compatibility: 1).on(:indexed_posts).before(:insert, :update) do
|
||||
"new.indexed_content := to_tsvector(('pg_catalog.' || new.dictionary)::regconfig, coalesce(new.title, '') || '\n' || coalesce(new.content,''));"
|
||||
end
|
||||
|
||||
IndexedPost.find_each do |post|
|
||||
locale = post.locale
|
||||
|
||||
post.update dictionary: locale, locale: LOCALES[locale]
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
remove_column :indexed_posts, :locale
|
||||
rename_column :indexed_posts, :dictionary, :locale
|
||||
end
|
||||
end
|
27
db/schema.rb
27
db/schema.rb
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2021_05_06_212356) do
|
||||
ActiveRecord::Schema.define(version: 2021_05_07_221120) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "pg_trgm"
|
||||
|
@ -230,6 +230,7 @@ ActiveRecord::Schema.define(version: 2021_05_06_212356) do
|
|||
t.string "content", default: ""
|
||||
t.tsvector "indexed_content"
|
||||
t.integer "order", default: 0
|
||||
t.string "dictionary"
|
||||
t.index ["front_matter"], name: "index_indexed_posts_on_front_matter", using: :gin
|
||||
t.index ["indexed_content"], name: "index_indexed_posts_on_indexed_content", using: :gin
|
||||
t.index ["layout"], name: "index_indexed_posts_on_layout"
|
||||
|
@ -360,21 +361,13 @@ ActiveRecord::Schema.define(version: 2021_05_06_212356) do
|
|||
|
||||
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
|
||||
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
|
||||
# no candidate create_trigger statement could be found, creating an adapter-specific one
|
||||
execute(<<-SQL)
|
||||
CREATE OR REPLACE FUNCTION public.indexed_posts_before_insert_update_row_tr()
|
||||
RETURNS trigger
|
||||
LANGUAGE plpgsql
|
||||
AS $function$
|
||||
BEGIN
|
||||
new.indexed_content := to_tsvector(('pg_catalog.' || new.locale)::regconfig, coalesce(new.title, '') || '
|
||||
' || coalesce(new.content,''));
|
||||
RETURN NEW;
|
||||
END;
|
||||
$function$
|
||||
SQL
|
||||
|
||||
# no candidate create_trigger statement could be found, creating an adapter-specific one
|
||||
execute("CREATE TRIGGER indexed_posts_before_insert_update_row_tr BEFORE INSERT OR UPDATE ON \"indexed_posts\" FOR EACH ROW EXECUTE PROCEDURE indexed_posts_before_insert_update_row_tr()")
|
||||
create_trigger("indexed_posts_before_insert_update_row_tr", :compatibility => 1).
|
||||
on("indexed_posts").
|
||||
before(:insert, :update) do
|
||||
<<-SQL_ACTIONS
|
||||
new.indexed_content := to_tsvector(('pg_catalog.' || new.dictionary)::regconfig, coalesce(new.title, '') || '
|
||||
' || coalesce(new.content,''));
|
||||
SQL_ACTIONS
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue