Skip to content

Commit

Permalink
Search results default to the users last used view type
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Aug 29, 2014
1 parent cb2d0b1 commit b3c35cc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 26 deletions.
54 changes: 32 additions & 22 deletions lib/blacklight/catalog.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- encoding : utf-8 -*-
module Blacklight::Catalog
module Blacklight::Catalog
extend ActiveSupport::Concern

include Blacklight::Base

SearchHistoryWindow = 100 # how many searches to save in session history
Expand All @@ -18,13 +18,13 @@ module Blacklight::Catalog

record_search_parameters
end

# get search results from the solr index
def index
(@response, @document_list) = get_search_results

respond_to do |format|
format.html { }
format.html { preferred_view }
format.rss { render :layout => false }
format.atom { render :layout => false }
format.json do
Expand All @@ -35,10 +35,10 @@ def index
document_export_formats(format)
end
end

# get single document from the solr index
def show
@response, @document = get_solr_response_for_doc_id
@response, @document = get_solr_response_for_doc_id

respond_to do |format|
format.html {setup_next_and_previous_documents}
Expand All @@ -49,10 +49,10 @@ def show
# export formats.
@document.export_formats.each_key do | format_name |
# It's important that the argument to send be a symbol;
# if it's a string, it makes Rails unhappy for unclear reasons.
# if it's a string, it makes Rails unhappy for unclear reasons.
format.send(format_name.to_sym) { render :text => @document.export_as(format_name), :layout => false }
end

end
end

Expand Down Expand Up @@ -80,14 +80,14 @@ def facet

respond_to do |format|
# Draw the facet selector for users who have javascript disabled:
format.html
format.html
format.json { render json: render_facet_list_as_json }

# Draw the partial for the "more" facet modal window:
format.js { render :layout => false }
end
end

# method to serve up XML OpenSearch description and JSON autocomplete response
def opensearch
respond_to do |format|
Expand All @@ -99,7 +99,7 @@ def opensearch
end
end
end

# citation action
def citation
@response, @documents = get_solr_response_for_document_ids(params[:id])
Expand All @@ -109,14 +109,14 @@ def citation
end
end


# Email Action (this will render the appropriate view on GET requests and process the form and send the email on POST requests)
def email
@response, @documents = get_solr_response_for_document_ids(params[:id])

if request.post? and validate_email_params
email = RecordMailer.email_record(@documents, {:to => params[:to], :message => params[:message]}, url_options)
email.deliver
email.deliver

flash[:success] = I18n.t("blacklight.email.success")

Expand All @@ -132,11 +132,11 @@ def email
end
end


# SMS action (this will render the appropriate view on GET requests and process the form and send the email on POST requests)
def sms
def sms
@response, @documents = get_solr_response_for_document_ids(params[:id])

if request.post? and validate_sms_params
to = "#{params[:to].gsub(/[^\d]/, '')}@#{params[:carrier]}"

Expand All @@ -150,7 +150,7 @@ def sms
format.js { render 'sms_sent' }
end and return
end

respond_to do |format|
format.js { render :layout => false }
format.html
Expand All @@ -159,16 +159,26 @@ def sms

##
# Check if any search parameters have been set
# @return [Boolean]
# @return [Boolean]
def has_search_parameters?
!params[:q].blank? or !params[:f].blank? or !params[:search_field].blank?
end
protected

protected
#
# non-routable methods ->
#

##
# If the params specify a view, then store it in the session. If the params
# do not specifiy the view, set the view parameter to the value stored in the
# session. This enables a user with a session to do subsequent searches and have
# them default to the last used view.
def preferred_view
session[:preferred_view] = params[:view] if params[:view]
params[:view] ||= session[:preferred_view]
end

##
# Render additional response formats, as provided by the blacklight configuration
def additional_response_formats format
Expand Down
35 changes: 31 additions & 4 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
describe CatalogController do

describe "index action" do
# In Rails 3 ActionDispatch::TestProcess#assigns() converts anything that

# In Rails 3 ActionDispatch::TestProcess#assigns() converts anything that
# descends from Hash to a HashWithIndifferentAccess. Therefore our Solr
# response object gets replaced if we call assigns(:response)
# Fixed by https://github.com/rails/rails/commit/185c3dbc6ab845edfc94e8d38ef5be11c417dd81
Expand All @@ -17,17 +17,44 @@ def assigns_response
assigns(:response)
end
end

describe "with format :html" do
let(:user_query) { 'history' } # query that will get results

it "should have no search history if no search criteria" do
allow(controller).to receive(:get_search_results)
allow(controller).to receive(:get_search_results)
session[:history] = []
get :index
expect(session[:history]).to be_empty
end

describe "preferred view" do
it "should save the view choice" do
get :index, q: 'foo', view: 'gallery'
expect(session[:preferred_view]).to eq 'gallery'
end

context "when they have a preferred view" do
before do
session[:preferred_view] = 'gallery'
end

context "and no view is specified" do
it "should use the saved preference" do
get :index, q: 'foo'
expect(controller.params[:view]).to eq 'gallery'
end
end

context "and a view is specified" do
it "should use the saved preference" do
get :index, q: 'foo', view: 'list'
expect(controller.params[:view]).to eq 'list'
end
end
end
end

# check each user manipulated parameter
it "should have docs and facets for query with results", :integration => true do
get :index, q: user_query
Expand Down

0 comments on commit b3c35cc

Please sign in to comment.