Skip to content

Commit b952acc

Browse files
committed
unsubscribe from search results directly from browse page
closes #43
1 parent 00010ff commit b952acc

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

app/helpers/opportunities_helper.rb

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ def filtered?
66
@opportunities.filterer.params[:department_id].present?
77
end
88

9+
def existing_saved_search
10+
current_user &&
11+
filtered? &&
12+
current_user.saved_searches.detect do |saved_search|
13+
normalize_search_params(saved_search.search_params) ==
14+
normalize_search_params(current_filter_params)
15+
end
16+
end
17+
18+
def normalize_search_params(search_params)
19+
search_params.stringify_keys.select do |_, v|
20+
v.present?
21+
end
22+
end
23+
924
def current_filter_params
1025
pick(@opportunities.filterer.params, *SavedSearch::PERMITTED_SEARCH_PARAMS)
1126
end

app/views/opportunities/_filter_form.html.erb

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
<ul class='sidebar_sub_actions'>
2525
<li>
2626
<% if signed_in? %>
27-
<a href='<%= saved_searches_path(current_filter_params) %>' data-method='post'><%= t('email_me') %></a>
27+
<% if existing_saved_search %>
28+
<a href='<%= saved_search_path(existing_saved_search) %>' data-method='delete'><%= t('unsubscribe_from_search') %></a>
29+
<% else %>
30+
<a href='<%= saved_searches_path(current_filter_params) %>' data-method='post'><%= t('email_me') %></a>
31+
<% end %>
2832
<% else %>
2933
<a href='<%= new_user_session_path %>'><%= t('email_me') %></a>
3034
<% end %>

config/locales/en.yml

+1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ en:
192192
other: Answer %{count} questions
193193
unapprove: Un-approve
194194
unsubscribe: Unsubscribe
195+
unsubscribe_from_search: Unsubscribe from these search results
195196
users:
196197
registrations:
197198
confirm:

spec/features/opportunities/index_spec.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,20 @@
2727
context 'when signed in' do
2828
before { login_as vendor }
2929

30-
it 'saved a search' do
30+
it 'saves a search' do
3131
visit opportunities_path
32+
33+
# No links without filter
34+
expect(page).to_not have_link t('unsubscribe_from_search')
35+
expect(page).to_not have_link t('email_me')
36+
3237
fill_in :opportunity_filters_text, with: 'nothingmatchesthis'
3338
find('.opportunity_filters button').click
3439
expect { click_link t('email_me') }.
3540
to change { vendor.saved_searches.count }.by(1)
41+
42+
expect { click_link t('unsubscribe_from_search') }.
43+
to change { vendor.saved_searches.count }.by(-1)
3644
end
3745
end
3846
end

0 commit comments

Comments
 (0)