Skip to content

Commit af9eb9a

Browse files
committed
Fix sorting Topics and add system tests
1 parent 344ac10 commit af9eb9a

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

app/models/concerns/searcheable.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ def sort_order(order_from_params)
3333

3434
order_from_params
3535
end
36+
end
3637

37-
def search_with_params(params)
38+
included do
39+
scope :search_with_params, ->(params) do
3840
self
3941
.then { |scope| params[:state].present? ? scope.by_state(params[:state]) : scope }
4042
.then { |scope| params[:provider_id].present? ? scope.by_provider(params[:provider_id]) : scope }
4143
.then { |scope| params[:language_id].present? ? scope.by_language(params[:language_id]): scope }
4244
.then { |scope| params[:year].present? ? scope.by_year(params[:year]) : scope }
4345
.then { |scope| params[:month].present? ? scope.by_month(params[:month]) : scope }
4446
.then { |scope| params[:query].present? ? scope.search(params[:query]) : scope }
45-
.then { |scope| scope.order(created_at: sort_order(params[:order])) }
47+
.then { |scope| params[:order].present? ? scope.order(created_at: sort_order(params[:order].to_sym)) : scope }
4648
end
4749
end
4850
end

spec/system/topic_search_spec.rb

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Topics search", type: :system do
4+
let(:admin) { create(:user, :admin, email: "[email protected]") }
5+
let(:english) { create(:language, name: "English") }
6+
let(:spanish) { create(:language, name: "Spanish") }
7+
let!(:spanish_active_topic) { create(:topic, language: spanish, title: "Tratamiento del resfriado", created_at: Date.new(2025, 02, 03)) }
8+
let!(:english_active_topic) { create(:topic, language: english, title: "How to treat colds", description: "All the latest information about nasopharyngitis", created_at: Date.new(2025, 03, 04)) }
9+
let!(:english_archived_topic) { create(:topic, :archived, language: english, title: "Obsolete", created_at: Date.new(2023, 02, 01)) }
10+
11+
before do
12+
login_as(admin)
13+
click_link("Topics")
14+
end
15+
16+
it "shows all topics" do
17+
expect(page).to have_text(english_active_topic.title)
18+
expect(page).to have_text(spanish_active_topic.title)
19+
expect(page).to have_text(english_archived_topic.title)
20+
end
21+
22+
context "when searching by title" do
23+
it "only displays topics matching the search" do
24+
fill_in "search_query", with: "tratamiento"
25+
26+
expect(page).to have_text(spanish_active_topic.title)
27+
expect(page).not_to have_text(english_active_topic.title)
28+
expect(page).not_to have_text(english_archived_topic.title)
29+
end
30+
end
31+
32+
context "when searching by description" do
33+
it "only displays topics matching the search" do
34+
fill_in "search_query", with: "pharyn"
35+
36+
expect(page).to have_text(english_active_topic.title)
37+
expect(page).not_to have_text(spanish_active_topic.title)
38+
expect(page).not_to have_text(english_archived_topic.title)
39+
end
40+
end
41+
42+
context "when searching by language" do
43+
it "only displays topics matching the search" do
44+
select "Spanish", from: "search_language_id"
45+
46+
expect(page).to have_text(spanish_active_topic.title)
47+
expect(page).not_to have_text(english_active_topic.title)
48+
expect(page).not_to have_text(english_archived_topic.title)
49+
50+
select "English", from: "search_language_id"
51+
52+
expect(page).to have_text(english_active_topic.title)
53+
expect(page).to have_text(english_archived_topic.title)
54+
expect(page).not_to have_text(spanish_active_topic.title)
55+
end
56+
end
57+
58+
context "when searching by year" do
59+
it "only displays topics matching the search" do
60+
select "2025", from: "search_year"
61+
62+
expect(page).to have_text(spanish_active_topic.title)
63+
expect(page).to have_text(english_active_topic.title)
64+
expect(page).not_to have_text(english_archived_topic.title)
65+
66+
select "2023", from: "search_year"
67+
68+
expect(page).to have_text(english_archived_topic.title)
69+
expect(page).not_to have_text(spanish_active_topic.title)
70+
expect(page).not_to have_text(english_active_topic.title)
71+
end
72+
end
73+
74+
context "when searching by month" do
75+
it "only displays topics matching the search" do
76+
select "2", from: "search_month"
77+
78+
expect(page).to have_text(spanish_active_topic.title)
79+
expect(page).to have_text(english_archived_topic.title)
80+
expect(page).not_to have_text(english_active_topic.title)
81+
82+
select "3", from: "search_month"
83+
84+
expect(page).to have_text(english_active_topic.title)
85+
expect(page).not_to have_text(english_archived_topic.title)
86+
expect(page).not_to have_text(spanish_active_topic.title)
87+
end
88+
end
89+
90+
context "when searching by state" do
91+
it "only displays topics matching the search" do
92+
select "active", from: "search_state"
93+
94+
expect(page).to have_text(spanish_active_topic.title)
95+
expect(page).to have_text(english_active_topic.title)
96+
expect(page).not_to have_text(english_archived_topic.title)
97+
98+
select "archived", from: "search_state"
99+
100+
expect(page).to have_text(english_archived_topic.title)
101+
expect(page).not_to have_text(spanish_active_topic.title)
102+
expect(page).not_to have_text(english_active_topic.title)
103+
end
104+
end
105+
106+
context "when sorting" do
107+
it "displays users in the selected order" do
108+
select "asc", from: "search_order"
109+
expect(page).to have_text(/#{english_archived_topic.title}.+#{spanish_active_topic.title}.+#{english_active_topic.title}/m)
110+
end
111+
end
112+
end

0 commit comments

Comments
 (0)