From 6396841b2c4401d37a345dc351a73c2c3abe4582 Mon Sep 17 00:00:00 2001 From: f Date: Fri, 7 May 2021 19:25:09 -0300 Subject: [PATCH] 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. --- app/controllers/posts_controller.rb | 3 +- app/models/indexed_post.rb | 2 +- app/models/post/indexable.rb | 3 +- ...7221120_add_dictionary_to_indexed_posts.rb | 29 +++++++++++++++++++ db/schema.rb | 27 +++++++---------- 5 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 db/migrate/20210507221120_add_dictionary_to_indexed_posts.rb diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 4acf765..f865bd5 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -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 diff --git a/app/models/indexed_post.rb b/app/models/indexed_post.rb index ede5b53..16858a6 100644 --- a/app/models/indexed_post.rb +++ b/app/models/indexed_post.rb @@ -19,7 +19,7 @@ class IndexedPost < ApplicationRecord query: query, using: { tsearch: { - dictionary: IndexedPost.to_dictionary(locale: locale), + dictionary: dictionary, tsvector_column: 'indexed_content' }, trigram: { diff --git a/app/models/post/indexable.rb b/app/models/post/indexable.rb index 0baa801..b1d6250 100644 --- a/app/models/post/indexable.rb +++ b/app/models/post/indexable.rb @@ -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 diff --git a/db/migrate/20210507221120_add_dictionary_to_indexed_posts.rb b/db/migrate/20210507221120_add_dictionary_to_indexed_posts.rb new file mode 100644 index 0000000..f79309f --- /dev/null +++ b/db/migrate/20210507221120_add_dictionary_to_indexed_posts.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index ed62940..0a8dd08 100644 --- a/db/schema.rb +++ b/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