Skip to content

Commit

Permalink
Merge pull request #54 from sascha-karnatz/extract-search-module
Browse files Browse the repository at this point in the history
Remove search module
  • Loading branch information
tvdeyen authored Oct 8, 2024
2 parents fccf6d5 + fb81d36 commit adfa755
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 79 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ and reindex your database in your Rails console

```rb
# rails console
$ Alchemy::PgSearch::Search.rebuild
$ Alchemy::PgSearch.rebuild
```

## Contributing
Expand Down
2 changes: 1 addition & 1 deletion app/extensions/alchemy/pg_search/page_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def searchable?
private

def remove_unpublished_page
Alchemy::PgSearch::Search.remove_page(self) unless searchable?
Alchemy::PgSearch.remove_page(self) unless searchable?
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/jobs/alchemy/pg_search/index_page_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Alchemy
module PgSearch
class IndexPageJob < BaseJob
def perform(page)
Search.index_page(page)
PgSearch.index_page(page)
end
end
end
Expand Down
46 changes: 39 additions & 7 deletions lib/alchemy-pg_search.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "alchemy/pg_search/engine"
require "alchemy/pg_search/config"
require "alchemy/pg_search/search"

module Alchemy
module PgSearch
Expand All @@ -16,20 +15,53 @@ def self.is_searchable?(ingredient_type)
SEARCHABLE_INGREDIENTS.include?(ingredient_type.gsub(/Alchemy::Ingredients::/, ""))
end

##
# index all supported Alchemy models
def self.rebuild
[Alchemy::Page, Alchemy::Ingredient].each do |model|
::PgSearch::Multisearch.rebuild(model)
end
end

##
# remove the whole index for the page
#
# @param page [Alchemy::Page]
def self.remove_page(page)
::PgSearch::Document.delete_by(page_id: page.id)
end

##
# index a single page and indexable ingredients
#
# @param page [Alchemy::Page]
def self.index_page(page)
remove_page page

page.update_pg_search_document
page.all_elements.includes(:ingredients).find_each do |element|
element.ingredients.select { |i| Alchemy::PgSearch.is_searchable?(i.type) }.each do |ingredient|
ingredient.update_pg_search_document
end
end
end

##
# search for page results
#
# @param query [string]
# @param ability [nil|CanCan::Ability]
# @return [ActiveRecord::Relation]
def self.search(query, ability: nil)
Search.search(query, ability: ability)
end
query = ::PgSearch.multisearch(query)
.select("JSON_AGG(content) as content", :page_id)
.reorder("")
.group(:page_id)
.joins(:page)

##
# index all supported Alchemy models
def self.rebuild
Search.rebuild
query = query.merge(Alchemy::Page.accessible_by(ability, :read)) if ability

query
end
end
end
55 changes: 0 additions & 55 deletions lib/alchemy/pg_search/search.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/features/fulltext_search_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
it "does not display results placed on global pages" do
# A layout page is configured and the page is indexed after publish
public_page.update!(layoutpage: true)
Alchemy::PgSearch::Search.index_page public_page
Alchemy::PgSearch.index_page public_page

visit("/suche?query=search")
expect(page).to have_css("h2.no_search_results")
Expand Down
14 changes: 14 additions & 0 deletions spec/jobs/index_page_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Alchemy::PgSearch::IndexPageJob, type: :job do
include ActiveJob::TestHelper

let(:page) { create(:alchemy_page, :public) }

it "calls the index_page - method" do
expect(Alchemy::PgSearch).to receive(:index_page).with(page)
described_class.perform_now(page)
end
end
26 changes: 13 additions & 13 deletions spec/lib/search_spec.rb → spec/lib/alchemy-pg_search_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe Alchemy::PgSearch::Search do
describe Alchemy::PgSearch do
let(:page_version) { create(:alchemy_page_version, :published) }
let(:ingredient_element) { create(:alchemy_element, :with_ingredients, name: "ingredient_test", public: true, page_version: page_version) }

Expand All @@ -22,7 +22,7 @@
context 'after rebuild' do
before do
prepared_ingredients
Alchemy::PgSearch::Search.rebuild
Alchemy::PgSearch.rebuild
end

it 'should have entries (2 Pages + 3 Ingredients)' do
Expand All @@ -39,11 +39,11 @@
context 'remove_page' do
before do
prepared_ingredients
Alchemy::PgSearch::Search.rebuild
Alchemy::PgSearch.rebuild
end

context 'remove first page' do
before { Alchemy::PgSearch::Search.remove_page first_page }
before { Alchemy::PgSearch.remove_page first_page }

it 'should have only one page and relative ingredients (1 Page + 3 Ingredients)' do
expect(PgSearch::Document.count).to eq(4)
Expand All @@ -55,7 +55,7 @@
end

context 'remove second page' do
before { Alchemy::PgSearch::Search.remove_page second_page }
before { Alchemy::PgSearch.remove_page second_page }

it 'should have only one page (1 Page)' do
expect(PgSearch::Document.count).to eq(1)
Expand All @@ -80,7 +80,7 @@

context 'first_page' do
before do
Alchemy::PgSearch::Search.index_page first_page
Alchemy::PgSearch.index_page first_page
end

it 'should have only one entry' do
Expand All @@ -94,7 +94,7 @@

context 'second_page' do
before do
Alchemy::PgSearch::Search.index_page second_page
Alchemy::PgSearch.index_page second_page
end

it 'should have four entries (1 Page + 3 Ingredients)' do
Expand All @@ -112,7 +112,7 @@
let!(:nested_element) { create(:alchemy_element, :with_ingredients, name: "article", public: true, page_version: page_version, parent_element: ingredient_element) }

before do
Alchemy::PgSearch::Search.index_page second_page
Alchemy::PgSearch.index_page second_page
end

it 'should have 6 documents' do
Expand All @@ -130,10 +130,10 @@
context 'page searchable' do
let(:searchable) { true }
let!(:page) { create(:alchemy_page, :public, name: "Searchable Page", searchable: searchable) }
let(:result) { Alchemy::PgSearch::Search.search "searchable" }
let(:result) { Alchemy::PgSearch.search "searchable" }

before do
Alchemy::PgSearch::Search.rebuild
Alchemy::PgSearch.rebuild
end

it 'should find one page' do
Expand All @@ -151,12 +151,12 @@
end

context 'search' do
let(:result) { Alchemy::PgSearch::Search.search "foo" }
let(:result) { Alchemy::PgSearch.search "foo" }

before do
create(:alchemy_page, :restricted, :public, name: "foo")
prepared_ingredients
Alchemy::PgSearch::Search.rebuild
Alchemy::PgSearch.rebuild
end

it 'should find two pages' do
Expand All @@ -165,7 +165,7 @@

context 'ability' do
let(:user) { User.create(alchemy_roles: ["member"]) }
let(:result) { Alchemy::PgSearch::Search.search "foo", ability: Alchemy::Permissions.new(user) }
let(:result) { Alchemy::PgSearch.search "foo", ability: Alchemy::Permissions.new(user) }

context 'with a logged in user' do
it 'should find two pages' do
Expand Down

0 comments on commit adfa755

Please sign in to comment.