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 authored and cbeer committed Nov 10, 2014
1 parent 9207d38 commit 56d6792
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/blacklight/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions spec/controllers/catalog_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 56d6792

Please sign in to comment.