Skip to content

Commit

Permalink
Enable a configuration for skipping a saved search that uses the requ…
Browse files Browse the repository at this point in the history
…est and params object
  • Loading branch information
mejackreed committed Sep 24, 2019
1 parent 13d3ca5 commit 8f8c025
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
9 changes: 8 additions & 1 deletion app/controllers/concerns/blacklight/search_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def set_current_search_session
end

def find_search_session
if agent_is_crawler?
if agent_is_crawler? || skip_session_tracking?
nil
elsif params[:search_context].present?
find_or_initialize_search_session_from_params JSON.parse(params[:search_context])
Expand Down Expand Up @@ -78,6 +78,13 @@ def agent_is_crawler?
crawler_proc.call(request)
end

def skip_session_tracking?
skip_session_proc = blacklight_config.skip_session_tracking
return false if skip_session_proc.nil?

skip_session_proc.call(request, params)
end

def find_or_initialize_search_session_from_params params
params_copy = params.reject { |k, v| blacklisted_search_session_params.include?(k.to_sym) || v.blank? }

Expand Down
3 changes: 2 additions & 1 deletion lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ def default_values
crawler_detector: nil,
autocomplete_suggester: 'mySuggester',
raw_endpoint: OpenStructWithHashAccess.new(enabled: false),
track_search_session: true
track_search_session: true,
skip_session_tracking: nil
}
end
# rubocop:enable Metrics/MethodLength
Expand Down
26 changes: 26 additions & 0 deletions spec/features/skip_session_tracking_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

RSpec.describe 'Search session skipping' do
describe 'crawler search' do
let(:original_proc) { ::CatalogController.blacklight_config.skip_session_tracking }

before do
::CatalogController.blacklight_config.skip_session_tracking = ->(req, params) { params.fetch('view', nil) == 'weird_json_view' }
end

after do
::CatalogController.blacklight_config.skip_session_tracking = original_proc
end

it 'remembers most searches' do
visit root_path
fill_in 'q', with: 'chicken'
expect { click_button 'search' }.to change(Search, :count).by(1)
end

it 'does not remember weird json search' do
visit root_path
expect { visit search_catalog_path(q: 'chicken', view: 'weird_json_view') }.not_to change(Search, :count)
end
end
end

0 comments on commit 8f8c025

Please sign in to comment.