diff --git a/app/models/indexed_post.rb b/app/models/indexed_post.rb index 6456a82..c76a5c8 100644 --- a/app/models/indexed_post.rb +++ b/app/models/indexed_post.rb @@ -26,6 +26,9 @@ class IndexedPost < ApplicationRecord } } + # Trae los IndexedPost en el orden en que van a terminar en el sitio. + default_scope lambda { order(order: :desc, created_at: :desc) } + belongs_to :site # Convertir locale a direccionario de PG diff --git a/app/models/post/indexable.rb b/app/models/post/indexable.rb index f25f28d..9b92111 100644 --- a/app/models/post/indexable.rb +++ b/app/models/post/indexable.rb @@ -18,6 +18,8 @@ class Post 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 diff --git a/config/initializers/core_extensions.rb b/config/initializers/core_extensions.rb index e37b2be..84dea42 100644 --- a/config/initializers/core_extensions.rb +++ b/config/initializers/core_extensions.rb @@ -66,3 +66,16 @@ module Jekyll end end end + +# No aplicar el orden por ranking para poder obtener los artículos en el +# orden que tendrían en el sitio final. +module PgSearch + ScopeOptions.class_eval do + def apply(scope) + scope = include_table_aliasing_for_rank(scope) + rank_table_alias = scope.pg_search_rank_table_alias(include_counter: true) + + scope.joins(rank_join(rank_table_alias)) + end + end +end diff --git a/db/migrate/20210506212356_add_order_to_indexed_posts.rb b/db/migrate/20210506212356_add_order_to_indexed_posts.rb new file mode 100644 index 0000000..4b1a9fc --- /dev/null +++ b/db/migrate/20210506212356_add_order_to_indexed_posts.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +# Agrega el orden y fecha del post en el post indexado, de forma que los +# resultados se puedan obtener en el mismo orden. +class AddOrderToIndexedPosts < ActiveRecord::Migration[6.1] + def change + add_column :indexed_posts, :order, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index fed6934..ed62940 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_04_224343) do +ActiveRecord::Schema.define(version: 2021_05_06_212356) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" @@ -229,6 +229,7 @@ ActiveRecord::Schema.define(version: 2021_05_04_224343) do t.jsonb "front_matter", default: "{}" t.string "content", default: "" t.tsvector "indexed_content" + t.integer "order", default: 0 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"