Skip to content

Commit 44f7164

Browse files
committed
Add Page model search specs
1 parent e53db82 commit 44f7164

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

app/models/concerns/searchable.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ module Searchable
3131
# part of the FTS5 extension.
3232
# https://www.sqlite.org/fts5.html#the_snippet_function
3333
#
34+
klass = self
35+
3436
scope :with_snippets, ->(*columns) do
35-
snippet_columns = search_index_mapping.keys.map(&:to_s) & columns.map(&:to_s)
37+
snippet_columns = klass.search_index_mapping.keys.map(&:to_s) & columns.map(&:to_s)
3638
scope = select("#{table_name}.*").join_search_index
3739
snippet_columns.each.with_index do |column, column_index|
3840
scope = scope.select("snippet(#{search_table_name}, #{column_index}, '<mark>', '</mark>', '…', 32) AS #{column}_snippet")
@@ -85,7 +87,6 @@ def search_index_mapping
8587

8688
@search_index_mapping ||= search_index_definition.flat_map { |item| Array(item) }.map { |a, b = a| [a, b] }.to_h
8789
end
88-
private :search_index_mapping
8990

9091
def refresh_search_index
9192
find_each(&:update_in_search_index)

spec/models/page_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,26 @@
3030
expect(Page.search("Joy of Rails")).to include page
3131
end
3232
end
33+
34+
describe ".rank" do
35+
it "orders search results by rank" do
36+
page_1 = Page.find_or_create_by!(request_path: "/articles/custom-color-schemes-with-ruby-on-rails")
37+
page_2 = Page.find_or_create_by!(request_path: "/articles/mastering-custom-configuration-in-rails")
38+
39+
search = Page.search("custom con*")
40+
41+
expect(search.ranked.to_a).to eq([page_2, page_1])
42+
end
43+
end
44+
45+
describe ".with_snippets" do
46+
it "orders search results by rank" do
47+
Page.find_or_create_by!(request_path: "/articles/custom-color-schemes-with-ruby-on-rails")
48+
49+
page = Page.search("Color*").with_snippets(:title, :body).first
50+
51+
expect(page.title_snippet).to match(%r{<mark>Color</mark>})
52+
expect(page.body_snippet).to match(%r{<mark>color</mark>})
53+
end
54+
end
3355
end

0 commit comments

Comments
 (0)