Skip to content

Commit 0220177

Browse files
mejackreedbarmintor
authored andcommitted
Enable a configuration for skipping a saved search that uses the request and params object
1 parent ed767db commit 0220177

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

app/controllers/concerns/blacklight/search_context.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def set_current_search_session
3939
end
4040

4141
def find_search_session
42-
if agent_is_crawler?
42+
if agent_is_crawler? || skip_session_tracking?
4343
nil
4444
elsif params[:search_context].present?
4545
find_or_initialize_search_session_from_params JSON.parse(params[:search_context])
@@ -79,6 +79,13 @@ def agent_is_crawler?
7979
crawler_proc.call(request)
8080
end
8181

82+
def skip_session_tracking?
83+
skip_session_proc = blacklight_config.skip_session_tracking
84+
return false if skip_session_proc.nil?
85+
86+
skip_session_proc.call(request, params)
87+
end
88+
8289
def find_or_initialize_search_session_from_params params
8390
params_copy = params.reject { |k, v| blacklisted_search_session_params.include?(k.to_sym) || v.blank? }
8491

lib/blacklight/configuration.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ def default_values
243243
# @return [Boolean]
244244
property :track_search_session, default: true
245245

246+
# @!attribute skip_session_tracking
247+
# @since v8.0.0
248+
# @return [Boolean]
249+
property :skip_session_tracking, default: nil
250+
246251
# @!attribute advanced_search
247252
# @since v7.15.0
248253
# @return [#enabled]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe 'Search session skipping' do
4+
describe 'crawler search' do
5+
let(:original_proc) { ::CatalogController.blacklight_config.skip_session_tracking }
6+
7+
before do
8+
::CatalogController.blacklight_config.skip_session_tracking = ->(_req, params) { params.fetch('view', nil) == 'weird_json_view' }
9+
end
10+
11+
after do
12+
::CatalogController.blacklight_config.skip_session_tracking = original_proc
13+
end
14+
15+
it 'remembers most searches' do
16+
visit root_path
17+
fill_in 'q', with: 'chicken'
18+
expect { click_button 'search' }.to change(Search, :count).by(1)
19+
end
20+
21+
it 'does not remember weird json search' do
22+
visit root_path
23+
expect { visit search_catalog_path(q: 'chicken', view: 'weird_json_view') }.not_to change(Search, :count)
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)