Skip to content

Commit

Permalink
The user's view preference must be active to be used
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 23, 2015
1 parent 58dd4e2 commit 9fdc70c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
6 changes: 4 additions & 2 deletions app/helpers/blacklight/blacklight_helper_behavior.rb
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions lib/blacklight/catalog.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 0 additions & 20 deletions spec/controllers/catalog_controller_spec.rb
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions spec/helpers/blacklight_helper_spec.rb
Expand Up @@ -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)
Expand Down

0 comments on commit 9fdc70c

Please sign in to comment.