From 9fdc70c70b227f0006813d1c1701cf5cec07bc48 Mon Sep 17 00:00:00 2001 From: Chris Beer Date: Fri, 20 Feb 2015 20:00:06 -0800 Subject: [PATCH] The user's view preference must be active to be used --- .../blacklight/blacklight_helper_behavior.rb | 6 ++- lib/blacklight/catalog.rb | 8 ++-- spec/controllers/catalog_controller_spec.rb | 20 --------- spec/helpers/blacklight_helper_spec.rb | 44 +++++++++++++++++++ 4 files changed, 53 insertions(+), 25 deletions(-) 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..dfa0bcaff1 100644 --- a/lib/blacklight/catalog.rb +++ b/lib/blacklight/catalog.rb @@ -36,7 +36,7 @@ def index (@response, @document_list) = search_results(params, search_params_logic) respond_to do |format| - format.html { preferred_view } + format.html { store_preferred_view } format.rss { render :layout => false } format.atom { render :layout => false } format.json do @@ -130,11 +130,13 @@ def has_search_parameters? # 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 + def store_preferred_view session[:preferred_view] = params[:view] if params[:view] - params[:view] ||= session[:preferred_view] end + alias_method :preferred_view, :store_preferred_view + deprecation_deprecate :preferred_view + ## # Render additional response formats for the index action, as provided by the # blacklight configuration 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)