Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up the main module and add a search_class attribute to Alchemy #53

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/controller/alchemy/pg_search/controller_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def perform_search
# @return [Array]
#
def search_results
Alchemy::PgSearch.search params[:query], ability: current_ability
Alchemy.search_class.search params[:query], ability: current_ability
end

# A view helper that loads the search result page.
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
49 changes: 42 additions & 7 deletions lib/alchemy-pg_search.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
require "alchemy/pg_search/engine"
require "alchemy/pg_search/config"
require "alchemy/pg_search/search"

module Alchemy
mattr_accessor :search_class
@@search_class = PgSearch

module PgSearch
SEARCHABLE_INGREDIENTS = %w[Text Richtext Picture]

Expand All @@ -16,20 +18,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
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
Loading