From 56d67925d962834d70a493d920593a0250e2041e Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Fri, 29 Aug 2014 15:50:27 -0500 Subject: [PATCH] Search results default to the users last used view type --- lib/blacklight/catalog.rb | 12 ++++++++- spec/controllers/catalog_controller_spec.rb | 27 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/blacklight/catalog.rb b/lib/blacklight/catalog.rb index bac197d98c..e84552aa54 100644 --- a/lib/blacklight/catalog.rb +++ b/lib/blacklight/catalog.rb @@ -24,7 +24,7 @@ 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 @@ -179,6 +179,16 @@ def has_search_parameters? # 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 diff --git a/spec/controllers/catalog_controller_spec.rb b/spec/controllers/catalog_controller_spec.rb index f21382ed03..7de0e93296 100644 --- a/spec/controllers/catalog_controller_spec.rb +++ b/spec/controllers/catalog_controller_spec.rb @@ -28,6 +28,33 @@ def assigns_response 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