Skip to content

Commit

Permalink
Merge pull request #1310 from projectblacklight/catalog-track
Browse files Browse the repository at this point in the history
Update catalog#track to fall back on the model's route, and fallback …
  • Loading branch information
jcoyne committed Nov 8, 2015
2 parents 012c0c2 + 9d4062c commit 31ee994
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
8 changes: 4 additions & 4 deletions app/controllers/concerns/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ def track
search_session['id'] = params[:search_id]
search_session['per_page'] = params[:per_page]

path = if params[:redirect] and (params[:redirect].starts_with?("/") or params[:redirect] =~ URI::regexp)
URI.parse(params[:redirect]).path
if params[:redirect] and (params[:redirect].starts_with?('/') or params[:redirect] =~ URI::regexp)
path = URI.parse(params[:redirect]).path
redirect_to path, status: 303
else
{ action: 'show' }
redirect_to blacklight_config.document_model.new(id: params[:id]), status: 303
end
redirect_to path, :status => 303
end

# displays values and pagination links for a single facet field
Expand Down
20 changes: 13 additions & 7 deletions app/helpers/blacklight/url_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,32 @@ def link_to_next_document(next_document)
# session_tracking_params(SolrDocument.new(id: 123), 7)
# => { data: { :'tracker-href' => '/catalog/123/track?counter=7&search_id=999' } }
def session_tracking_params document, counter
if document.nil?
path = session_tracking_path(document, per_page: params.fetch(:per_page, search_session['per_page']), counter: counter, search_id: current_search_session.try(:id))

if path.nil?
return {}
end
{ :data => {:'context-href' => session_tracking_path(document, per_page: params.fetch(:per_page, search_session['per_page']), counter: counter, search_id: current_search_session.try(:id))}}

{ data: {:'context-href' => path } }
end
protected :session_tracking_params

##
# Get the URL for tracking search sessions across pages using polymorphic routing
def session_tracking_path document, params = {}
controller_tracking_method = "track_#{controller_name}_path"
return if document.nil?

if respond_to? controller_tracking_method
if respond_to?(controller_tracking_method)
send(controller_tracking_method, params.merge(id: document.id))
else
polymorphic_path(document, params)
blacklight.track_search_context_path(params.merge(id: document.id))
end
end

def controller_tracking_method
"track_#{controller_name}_path"
end

#
# link based helpers ->
#
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
put "saved_searches/save/:id", :to => "saved_searches#save", :as => "save_search"
delete "saved_searches/forget/:id", :to => "saved_searches#forget", :as => "forget_search"
post "saved_searches/forget/:id", :to => "saved_searches#forget"
post "/catalog/:id/track", to: 'catalog#track', as: 'track_search_context'

resources :suggest, only: :index, defaults: { format: 'json' }
end
21 changes: 19 additions & 2 deletions spec/helpers/url_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,29 @@
expect(helper.link_to_document(@document, :title_display, counter: 5)).to include 'data-context-href="tracking url"'
end

it "should merge the data- attributes from the options with the counter params" do
it "includes the data- attributes from the options" do
data = {'id'=>'123456','title_display'=>['654321']}
@document = SolrDocument.new(data)
link = helper.link_to_document @document, { data: { x: 1 } }
expect(link).to have_selector '[data-x]'
expect(link).to have_selector '[data-context-href]'
end

it 'adds a controller-specific tracking attribute' do
data = { 'id'=>'123456', 'title_display'=>['654321'] }
@document = SolrDocument.new(data)

expect(helper).to receive(:track_test_path).and_return('/asdf')
link = helper.link_to_document @document, { data: { x: 1 } }

expect(link).to have_selector '[data-context-href="/asdf"]'
end

it 'adds a global tracking attribute' do
data = { 'id'=>'123456', 'title_display'=>['654321'] }
@document = SolrDocument.new(data)

link = helper.link_to_document @document, { data: { x: 1 } }
expect(link).to have_selector '[data-context-href="/catalog/123456/track"]'
end

it "passes on the title attribute to the link_to_with_data method" do
Expand Down

0 comments on commit 31ee994

Please sign in to comment.