Skip to content

Commit 040c517

Browse files
barmintorcbeer
authored andcommitted
implement a configuration object for Blacklight::Configuration.track_search_session
- allow configuration of components for applied parameters and item pagination - only build server-side search tracking data if using server-side tracking - extract applied_params html into a component
1 parent f40b1cf commit 040c517

File tree

13 files changed

+187
-27
lines changed

13 files changed

+187
-27
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div id="appliedParams" class="clearfix constraints-container">
2+
<%= render 'start_over' %>
3+
<%= link_back_to_catalog class: 'btn btn-outline-secondary' %>
4+
</div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module Blacklight
4+
module SearchContext
5+
class ServerAppliedParamsComponent < Blacklight::Component
6+
delegate :current_search_session, :link_back_to_catalog, to: :helpers
7+
8+
def render?
9+
current_search_session
10+
end
11+
end
12+
end
13+
end

app/controllers/concerns/blacklight/search_context.rb

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ def agent_is_crawler?
101101
end
102102

103103
def find_or_initialize_search_session_from_params params
104+
return unless blacklight_config.track_search_session_config.storage == 'server'
105+
104106
params_copy = params.reject { |k, v| blacklisted_search_session_params.include?(k.to_sym) || v.blank? }
105107

106108
return if params_copy.reject { |k, _v| [:action, :controller].include? k.to_sym }.blank?
@@ -131,25 +133,31 @@ def blacklisted_search_session_params
131133
# calls setup_previous_document then setup_next_document.
132134
# used in the show action for single view pagination.
133135
def setup_next_and_previous_documents
134-
if search_session['counter'] && current_search_session
135-
index = search_session['counter'].to_i - 1
136-
response, documents = search_service.previous_and_next_documents_for_search index, search_state.reset(current_search_session.query_params)
136+
return { counter: params[:counter] } if setup_next_and_previous_on_client?
137+
return nil unless setup_next_and_previous_on_server?
137138

138-
search_session['total'] = response.total
139-
{ prev: documents.first, next: documents.last }
140-
end
139+
index = search_session['counter'].to_i - 1
140+
response, documents = search_service.previous_and_next_documents_for_search index, search_state.reset(current_search_session.query_params)
141+
142+
search_session['total'] = response.total
143+
{ prev: documents.first, next: documents.last }
141144
rescue Blacklight::Exceptions::InvalidRequest => e
142145
logger&.warn "Unable to setup next and previous documents: #{e}"
143146
nil
144147
end
145148

149+
def setup_next_and_previous_on_server?
150+
search_session['counter'] && current_search_session && blacklight_config.track_search_session_config.storage == 'server'
151+
end
152+
153+
def setup_next_and_previous_on_client?
154+
params[:counter] && blacklight_config.track_search_session_config.storage == 'client'
155+
end
156+
146157
def page_links_document_path(document, counter)
147158
return nil unless document
159+
return search_state.url_for_document(document, counter: counter) if blacklight_config.view_config(:show).route
148160

149-
if blacklight_config.view_config(:show).route
150-
search_state.url_for_document(document, counter: counter)
151-
else
152-
solr_document_path(document, counter: counter)
153-
end
161+
solr_document_path(document, counter: counter)
154162
end
155163
end

app/helpers/blacklight/url_helper_behavior.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,21 +80,24 @@ def link_to_next_document(next_document, classes: 'next', **addl_link_opts)
8080
# @example
8181
# session_tracking_params(SolrDocument.new(id: 123), 7)
8282
# => { data: { context_href: '/catalog/123/track?counter=7&search_id=999' } }
83-
def session_tracking_params document, counter
84-
path = session_tracking_path(document, per_page: params.fetch(:per_page, search_session['per_page']), counter: counter, search_id: current_search_session.try(:id), document_id: document&.id)
85-
86-
if path.nil?
87-
return {}
83+
def session_tracking_params document, counter, per_page: search_session['per_page'], search_id: current_search_session&.id
84+
path_params = { per_page: params.fetch(:per_page, per_page), counter: counter, search_id: search_id }
85+
if blacklight_config.track_search_session_config.storage == 'server'
86+
path_params[:document_id] = document&.id
87+
path_params[:search_id] = search_id
8888
end
89+
path = session_tracking_path(document, path_params)
90+
return {} if path.nil?
8991

90-
{ data: { context_href: path, turbo_prefetch: false } }
92+
context_method = blacklight_config.track_search_session_config.storage == 'client' ? 'get' : 'post'
93+
{ data: { context_href: path, context_method: context_method, turbo_prefetch: false } }
9194
end
9295
private :session_tracking_params
9396

9497
##
9598
# Get the URL for tracking search sessions across pages using polymorphic routing
9699
def session_tracking_path document, params = {}
97-
return if document.nil? || !blacklight_config&.track_search_session
100+
return if document.nil? || !blacklight_config.track_search_session_config.storage
98101

99102
if main_app.respond_to?(controller_tracking_method)
100103
return main_app.public_send(controller_tracking_method, params.merge(id: document))
@@ -105,6 +108,8 @@ def session_tracking_path document, params = {}
105108
end
106109

107110
def controller_tracking_method
111+
return blacklight_config.track_search_session_config.url_helper if blacklight_config.track_search_session_config.url_helper
112+
108113
"track_#{controller_name}_path"
109114
end
110115

app/views/catalog/_show_main_content.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<%= render(Blacklight::SearchContextComponent.new(search_context: @search_context, search_session: search_session, current_document: @document)) %>
1+
<%= render blacklight_config.track_search_session_config.item_pagination_component.new(search_context: @search_context, search_session: search_session, current_document: @document) if blacklight_config.track_search_session_config.item_pagination_component %>
22

3-
<% @page_title = t('blacklight.search.show.title', document_title: Deprecation.silence(Blacklight::BlacklightHelperBehavior) { document_show_html_title }, application_name: application_name).html_safe %>
3+
<% @page_title = t('blacklight.search.show.title', document_title: document_presenter(@document).html_title, application_name: application_name).html_safe %>
44
<% content_for(:head) { render_link_rel_alternates } %>
55

66
<%= render (blacklight_config.view_config(:show).document_component || Blacklight::DocumentComponent).new(presenter: document_presenter(@document), component: :div, title_component: :h1, show: true) do |component| %>

app/views/catalog/index.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
<% content_for(:head) do %>
2+
<meta name="blacklight-search-storage" content="<%= blacklight_config.track_search_session_config.storage %>">
3+
<% end %>
14
<% content_for(:sidebar) do %>
25
<%= render 'search_sidebar' %>
36
<% end %>

app/views/catalog/show.html.erb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
<% if current_search_session %>
2-
<div id="appliedParams" class="clearfix constraints-container">
3-
<%= render 'start_over' %>
4-
<%= link_back_to_catalog class: 'btn btn-outline-secondary' %>
5-
</div>
6-
<% end %>
1+
<%= render blacklight_config.track_search_session_config.applied_params_component.new if blacklight_config.track_search_session_config.applied_params_component %>
72

83
<%= render 'show_main_content' %>
94

lib/blacklight/configuration.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,11 @@ def default_per_page
306306
# @return [Boolean]
307307
property :track_search_session, default: true
308308

309+
# @!attribute track_search_session_config
310+
# @since v7.35.0
311+
# @return [Blacklight::Configuration::SessionTrackingConfig]
312+
property :track_search_session_config, default: nil
313+
309314
# @!attribute advanced_search
310315
# @since v7.15.0
311316
# @return [#enabled]
@@ -618,6 +623,21 @@ def show_fields_for(document_or_display_types)
618623
fields.merge(show_fields)
619624
end
620625

626+
def track_search_session=(val)
627+
self.track_search_session_config = Blacklight::Configuration::SessionTrackingConfig.new(storage: val ? 'server' : false)
628+
super
629+
end
630+
631+
def track_search_session_config
632+
return track_search_session if track_search_session.is_a?(Blacklight::Configuration::SessionTrackingConfig)
633+
634+
stored_config = super
635+
636+
return stored_config if stored_config
637+
638+
self.track_search_session_config = Blacklight::Configuration::SessionTrackingConfig.new(storage: track_search_session ? 'server' : false)
639+
end
640+
621641
# @!visibility private
622642
def freeze
623643
each { |_k, v| v.is_a?(OpenStruct) && v.freeze }
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
class Blacklight::Configuration
4+
class SessionTrackingConfig < Blacklight::OpenStructWithHashAccess
5+
# @!attribute storage
6+
# @return [String, FalseClass] 'server': use server-side tracking; 'client': delegate search tracking and prev/next navigation to client
7+
# @!attribute applied_params_component
8+
# @return [Class] component class used to render a facet group
9+
# @!attribute item_pagination_component
10+
# @return [Class] component class used to render the constraints
11+
12+
def initialize(property_hash = {})
13+
super({ storage: 'server' }.merge(property_hash))
14+
end
15+
16+
def applied_params_component
17+
super || default_applied_params_component(storage)
18+
end
19+
20+
def item_pagination_component
21+
super || default_item_pagination_component(storage)
22+
end
23+
24+
def url_helper
25+
super || default_url_helper(storage)
26+
end
27+
28+
def default_applied_params_component(storage)
29+
return Blacklight::SearchContext::ServerAppliedParamsComponent if storage == 'server'
30+
31+
nil
32+
end
33+
34+
def default_item_pagination_component(storage)
35+
return Blacklight::SearchContextComponent if storage == 'server'
36+
37+
nil
38+
end
39+
40+
# extension point for alternative storage types
41+
def default_url_helper(_storage)
42+
nil
43+
end
44+
end
45+
end

spec/components/blacklight/document_component_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
let(:blacklight_config) do
2828
CatalogController.blacklight_config.deep_copy.tap do |config|
29-
config.track_search_session = false
29+
config.track_search_session_config.storage = false
3030
config.index.thumbnail_field = 'thumbnail_path_ss'
3131
config.index.document_actions[:bookmark].partial = '/catalog/bookmark_control'
3232
end

0 commit comments

Comments
 (0)