Skip to content

Commit 8a63f03

Browse files
authored
Merge pull request #695 from slovensko-digital/fix/679-find-journey-by-category-attrs
Fix/679 find journey by category attrs
2 parents 703e9c9 + bdf4d66 commit 8a63f03

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

app/models/journey.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Journey < ApplicationRecord
2323

2424
has_many :search_documents, :class_name => 'Document', as: :searchable
2525
has_one :categorization, :as => :categorizable, dependent: :destroy
26+
has_many :categories, through: :categorization
2627
accepts_nested_attributes_for :categorization
2728

2829
validates :title, presence: true
@@ -56,7 +57,7 @@ def to_param
5657
end
5758

5859
def description_search
59-
join_search([html_to_search_str(description), steps_search(:content)])
60+
join_search([html_to_search_str(description), steps_search(:content), categorization_search(:name, :description)])
6061
end
6162

6263
def title_search
@@ -111,4 +112,8 @@ def join_search(arr)
111112
def steps_search(column)
112113
Document.where(searchable: steps).pluck(column).join(' ')
113114
end
115+
116+
def categorization_search(*columns)
117+
html_to_search_str(categories.pluck(*columns).flatten.join(' '))
118+
end
114119
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class ReindexJourneys < ActiveRecord::Migration[7.0]
2+
disable_ddl_transaction!
3+
4+
def up
5+
PgSearch::Multisearch.rebuild(Journey)
6+
end
7+
8+
def down
9+
end
10+
end

db/structure.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2458,6 +2458,7 @@ INSERT INTO "schema_migrations" (version) VALUES
24582458
('20250426123370'),
24592459
('20250426123635'),
24602460
('20250426123636'),
2461-
('20250426170312');
2461+
('20250426170312'),
2462+
('20251011094046');
24622463

24632464

spec/features/search_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@
2929
expect(page).not_to have_text(unmatching_faq.title)
3030
end
3131

32+
scenario 'Visitor searches for category of interest' do
33+
title_matching = 'Batman'
34+
description_matching = 'Superman'
35+
36+
category = create(:category, name: "#{title_matching} category", description: "#{description_matching} description")
37+
matching_journey = create(:journey, title: "matching journey", published_status: 'PUBLISHED')
38+
create(:categorization, categorizable: matching_journey, categories: [category])
39+
40+
matching_journey.title = 'journey with category'
41+
matching_journey.save!
42+
43+
visit root_path
44+
fill_in 'q', with: title_matching
45+
click_button 'Hľadať'
46+
47+
expect(page).to have_text('Výsledky vyhľadávania')
48+
expect(page).to have_text(matching_journey.title)
49+
50+
visit root_path
51+
fill_in 'q', with: description_matching
52+
click_button 'Hľadať'
53+
54+
expect(page).to have_text('Výsledky vyhľadávania')
55+
expect(page).to have_text(matching_journey.title)
56+
end
57+
3258
scenario 'Not found results' do
3359
visit root_path
3460
fill_in 'q', with: 'Neexistujuci vyraz'

0 commit comments

Comments
 (0)