diff --git a/app/helpers/blacklight/blacklight_helper_behavior.rb b/app/helpers/blacklight/blacklight_helper_behavior.rb index 7afa15ee89..64bd6e6fc6 100644 --- a/app/helpers/blacklight/blacklight_helper_behavior.rb +++ b/app/helpers/blacklight/blacklight_helper_behavior.rb @@ -337,8 +337,10 @@ def field_value_separator # @param [Hash] the query parameters to check # @return [Symbol] def document_index_view_type query_params=params - if query_params[:view] and blacklight_config.view.keys.include? query_params[:view].to_sym - query_params[:view].to_sym + view_param = query_params[:view] + view_param ||= session[:preferred_view] + if view_param and document_index_views.keys.include? view_param.to_sym + view_param.to_sym else default_document_index_view_type end diff --git a/lib/blacklight/catalog.rb b/lib/blacklight/catalog.rb index 11d2638f77..e519a211ae 100644 --- a/lib/blacklight/catalog.rb +++ b/lib/blacklight/catalog.rb @@ -132,7 +132,6 @@ def has_search_parameters? # them default to the last used view. def preferred_view session[:preferred_view] = params[:view] if params[:view] - params[:view] ||= session[:preferred_view] end ## diff --git a/spec/controllers/catalog_controller_spec.rb b/spec/controllers/catalog_controller_spec.rb index 757ccf8274..7046a907e2 100644 --- a/spec/controllers/catalog_controller_spec.rb +++ b/spec/controllers/catalog_controller_spec.rb @@ -33,26 +33,6 @@ def assigns_response 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 diff --git a/spec/helpers/blacklight_helper_spec.rb b/spec/helpers/blacklight_helper_spec.rb index 974c8ad2c2..ec184e30e0 100644 --- a/spec/helpers/blacklight_helper_spec.rb +++ b/spec/helpers/blacklight_helper_spec.rb @@ -612,6 +612,50 @@ def mock_document_app_helper_url *args end end + describe "#document_index_view_type" do + it "should default to the default view" do + allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2) + allow(helper).to receive(:default_document_index_view_type).and_return(:xyz) + expect(helper.document_index_view_type).to eq :xyz + end + + it "should use the query parameter" do + allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2) + expect(helper.document_index_view_type(view: :a)).to eq :a + end + + it "should use the default view if the requested view is not available" do + allow(helper).to receive(:default_document_index_view_type).and_return(:xyz) + allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2) + expect(helper.document_index_view_type(view: :c)).to eq :xyz + end + + context "when they have a preferred view" do + before do + session[:preferred_view] = :b + end + + context "and no view is specified" do + it "should use the saved preference" do + allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2, c: 3) + expect(helper.document_index_view_type).to eq :b + end + + it "should use the default view if the preference is not available" do + allow(helper).to receive(:document_index_views).and_return(a: 1) + expect(helper.document_index_view_type).to eq :a + end + end + + context "and a view is specified" do + it "should use the query parameter" do + allow(helper).to receive(:document_index_views).and_return(a: 1, b: 2, c: 3) + expect(helper.document_index_view_type(view: :c)).to eq :c + end + end + end + end + describe "#presenter_class" do before do allow(helper).to receive(:blacklight_config).and_return(blacklight_config)