Skip to content

Commit

Permalink
Merge 80bdf0f into 8191c4e
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Mar 10, 2014
2 parents 8191c4e + 80bdf0f commit f93a0e8
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 45 deletions.
56 changes: 34 additions & 22 deletions app/assets/javascripts/blacklight/search_context.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
//= require blacklight/core
(function($) {
Blacklight.do_search_context_behavior = function() {
$('a[data-counter]').click(function(event) {
var f = document.createElement('form'); f.style.display = 'none';
this.parentNode.appendChild(f);
f.method = 'POST';
f.action = $(this).attr('href');
if(event.metaKey || event.ctrlKey){f.target = '_blank';};
var d = document.createElement('input'); d.setAttribute('type', 'hidden');
d.setAttribute('name', 'counter'); d.setAttribute('value', $(this).data('counter')); f.appendChild(d);
var id = document.createElement('input'); id.setAttribute('type', 'hidden');
id.setAttribute('name', 'search_id'); id.setAttribute('value', $(this).data('search_id')); f.appendChild(id);
var m = document.createElement('input'); m.setAttribute('type', 'hidden');
m.setAttribute('name', '_method'); m.setAttribute('value', 'put'); f.appendChild(m);
var m = document.createElement('input'); m.setAttribute('type', 'hidden');
m.setAttribute('name', $('meta[name="csrf-param"]').attr('content')); m.setAttribute('value', $('meta[name="csrf-token"]').attr('content')); f.appendChild(m);
$('a[data-context-href]').on('click.search-context', handleSearchContextMethod);
};

f.submit();
return false;
});
// this is the $.rails.handleMethod with a couple adjustments, described inline:
// first, we're attaching this directly to the event handler, so we can check for meta-keys
Blacklight.handleSearchContextMethod = function(event) {
var link = $(this);

};
Blacklight.onLoad(function() {
Blacklight.do_search_context_behavior();
});
// instead of using the normal href, we need to use the context href instead
var href = link.data('context-href'),
method = 'post',
target = link.attr('target'),
csrfToken = $('meta[name=csrf-token]').attr('content'),
csrfParam = $('meta[name=csrf-param]').attr('content'),
form = $('<form method="post" action="' + href + '"></form>'),
metadataInput = '<input name="_method" value="' + method + '" type="hidden" />',
redirectHref = '<input name="redirect" value="' + link.attr('href') + '" type="hidden" />';

// check for meta keys.. if set, we should open in a new table
if(event.metaKey || event.ctrlKey) {
target = '_blank';
}

if (csrfParam !== undefined && csrfToken !== undefined) {
metadataInput += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />';
}

if (target) { form.attr('target', target); }

form.hide().append(metadataInput).appendTo('body');
form.submit();
};

Blacklight.onLoad(function() {
Blacklight.do_search_context_behavior();
});
})(jQuery);
36 changes: 32 additions & 4 deletions app/helpers/blacklight/url_helper_behavior.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'deprecation'
module Blacklight::UrlHelperBehavior
extend Deprecation
self.deprecation_horizon = 'blacklight 6.0'

##
# Extension point for downstream applications
Expand All @@ -15,30 +18,55 @@ def url_for_document doc
def link_to_document(doc, opts={:label=>nil, :counter => nil})
opts[:label] ||= document_show_link_field(doc)
label = render_document_index_label doc, opts
link_to label, url_for_document(doc), search_session_params(opts[:counter]).merge(opts.reject { |k,v| [:label, :counter].include? k })
link_to label, url_for_document(doc), document_link_params(doc, opts)
end

def document_link_params(doc, opts)
session_tracking_params(doc, opts[:counter]).merge(opts.reject { |k,v| [:label, :counter].include? k })
end
protected :document_link_params

##
# Link to the previous document in the current search context
def link_to_previous_document(previous_document)
link_to_unless previous_document.nil?, raw(t('views.pagination.previous')), url_for_document(previous_document), search_session_params(search_session['counter'].to_i - 1).merge(:class => "previous", :rel => 'prev') do
link_opts = session_tracking_params(previous_document, search_session['counter'].to_i - 1).merge(:class => "previous", :rel => 'prev')
link_to_unless previous_document.nil?, raw(t('views.pagination.previous')), url_for_document(previous_document), link_opts do
content_tag :span, raw(t('views.pagination.previous')), :class => 'previous'
end
end

##
# Link to the next document in the current search context
def link_to_next_document(next_document)
link_to_unless next_document.nil?, raw(t('views.pagination.next')), url_for_document(next_document), search_session_params(search_session['counter'].to_i + 1).merge(:class => "next", :rel => 'next') do
link_opts = session_tracking_params(next_document, search_session['counter'].to_i + 1).merge(:class => "next", :rel => 'next')
link_to_unless next_document.nil?, raw(t('views.pagination.next')), url_for_document(next_document), link_opts do
content_tag :span, raw(t('views.pagination.next')), :class => 'next'
end
end

##
# Current search context parameters
def search_session_params counter
def search_session_params counter
{ :'data-counter' => counter, :'data-search_id' => current_search_session.try(:id) }
end
deprecation_deprecate :search_session_params

##
# Attributes for a link that gives a URL we can use to track clicks for the current search session
# @param [SolrDocument] document
# @param [Integer] counter
# @example
# 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?
return {}
end

{ :data => {:'context-href' => track_solr_document_path(document, counter: counter, search_id: current_search_session.try(:id))}}
end
protected :session_tracking_params


#
# link based helpers ->
Expand Down
4 changes: 2 additions & 2 deletions lib/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ def show
end

# updates the search counter (allows the show view to paginate)
def update
def track
search_session['counter'] = params[:counter]
redirect_to :action => "show", :status => 303
redirect_to URI.parse(params[:redirect]).path , :status => 303
end

# displays values and pagination links for a single facet field
Expand Down
2 changes: 1 addition & 1 deletion lib/blacklight/catalog/search_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def blacklisted_search_session_params
def setup_next_and_previous_documents
if search_session['counter'] and current_search_session
index = search_session['counter'].to_i - 1
response, documents = get_previous_and_next_documents_for_search index, current_search_session.query_params
response, documents = get_previous_and_next_documents_for_search index, current_search_session.query_params.with_indifferent_access

search_session['total'] = response.total
@search_context_response = response
Expand Down
8 changes: 6 additions & 2 deletions lib/blacklight/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,14 @@ def export(primary_resource)
def solr_document(primary_resource)
add_routes do |options|

args = {only: [:show, :update]}
args = {only: [:show]}
args[:constraints] = options[:constraints] if options[:constraints]

resources :solr_document, args.merge(path: primary_resource, controller: primary_resource)
resources :solr_document, args.merge(path: primary_resource, controller: primary_resource) do
member do
post "track"
end
end

# :show and :update are for backwards-compatibility with catalog_url named routes
resources primary_resource, args
Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,17 @@ def assigns_response
doc_id = '2007020969'

it "should set counter value into session[:search]" do
put :update, :id => doc_id, :counter => 3
put :track, :id => doc_id, :counter => 3
expect(session[:search]['counter']).to eq "3"
end

it "should redirect to show action for doc id" do
put :update, :id => doc_id, :counter => 3
put :track, :id => doc_id, :counter => 3
assert_redirected_to(catalog_path(doc_id))
end

it "HTTP status code for redirect should be 303" do
put :update, :id => doc_id, :counter => 3
put :track, :id => doc_id, :counter => 3
expect(response.status).to eq 303
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/features/alternate_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
fill_in "q", :with=>"history"
click_button 'search'
expect(page).to have_selector ".document-thumbnail"
expect(page).to have_selector ".document-thumbnail a[data-counter]"
expect(page).to have_selector ".document-thumbnail a[data-context-href]"
puts page.body
expect(page).to have_selector ".document-thumbnail a img"

end
Expand Down
13 changes: 9 additions & 4 deletions spec/features/search_results_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,19 @@
click_on 'Pluvial nectar of blessings'
expect(page).to have_content "« Previous | 10 of 30 | Next »"
prev = page.find("#previousNextDocument .previous")
expect(prev['data-counter']).to eq "9"
expect(prev['data-search_id']).to eq search_id
expect(prev['data-context-href']).to eq "/catalog/2003546302/track?counter=9&search_id=#{search_id}"

click_on "« Previous"

prev = page.find("#previousNextDocument .previous")
expect(prev['data-counter']).to eq "8"
expect(prev['data-search_id']).to eq search_id
expect(prev['data-context-href']).to eq "/catalog/2004310986/track?counter=8&search_id=#{search_id}"
end

it "should redirect context urls to the original url", :js => true do
search_for ''
first('.index_title a').click
expect(page).to have_content "« Previous | 1 of 30 | Next »"
expect(page.current_url).to_not have_content "/track"
end

end
Expand Down
4 changes: 2 additions & 2 deletions spec/helpers/url_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@
it "should convert the counter parameter into a data- attribute" do
data = {'id'=>'123456','title_display'=>['654321']}
@document = SolrDocument.new(data)
expect(helper.link_to_document(@document, { :label => :title_display, :counter => 5 })).to match /data-counter="5"/
expect(helper.link_to_document(@document, { :label => :title_display, :counter => 5 })).to match /\/catalog\/123456\/track\?counter=5/
end

it "passes on the title attribute to the link_to_with_data method" do
Expand Down Expand Up @@ -352,4 +352,4 @@
expect(added_facet_params_from_facet_action).to eq added_facet_params.except(Blacklight::Solr::FacetPaginator.request_keys[:page], Blacklight::Solr::FacetPaginator.request_keys[:sort])
end
end
end
end
8 changes: 4 additions & 4 deletions spec/lib/blacklight/routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
describe "without constraints" do
let(:options) { Hash.new }
it "should define the resources" do
router.should_receive(:resources).with(:solr_document, {:path=>:records, :controller=>:records, :only=>[:show, :update]})
router.should_receive(:resources).with(:records, :only=>[:show, :update])
router.should_receive(:resources).with(:solr_document, {:path=>:records, :controller=>:records, :only=>[:show]})
router.should_receive(:resources).with(:records, :only=>[:show])
subject.solr_document(:records)
end
end

describe "with constraints" do
let(:options) { { :constraints => {id: /[a-z]+/, format: false } } }
it "should define the resources" do
router.should_receive(:resources).with(:solr_document, {:path=>:records, :controller=>:records, :only=>[:show, :update], :constraints=>{:id=>/[a-z]+/, :format=>false} })
router.should_receive(:resources).with(:records, :only=>[:show, :update], :constraints=>{:id=>/[a-z]+/, :format=>false})
router.should_receive(:resources).with(:solr_document, {:path=>:records, :controller=>:records, :only=>[:show], :constraints=>{:id=>/[a-z]+/, :format=>false} })
router.should_receive(:resources).with(:records, :only=>[:show], :constraints=>{:id=>/[a-z]+/, :format=>false})
subject.solr_document(:records)
end
end
Expand Down

0 comments on commit f93a0e8

Please sign in to comment.