Skip to content

Commit

Permalink
Merge pull request #1717 from projectblacklight/release-6.x-backports
Browse files Browse the repository at this point in the history
Deserialize search history query parameters using search_state
  • Loading branch information
Jessie Keck committed Jun 21, 2017
2 parents c57e0b6 + dae97be commit 1ce5032
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/controllers/concerns/blacklight/search_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def blacklisted_search_session_params
def setup_next_and_previous_documents
if search_session['counter'] and current_search_session
index = search_session['counter'].to_i - 1
response, documents = get_previous_and_next_documents_for_search index, ActiveSupport::HashWithIndifferentAccess.new(current_search_session.query_params)
response, documents = get_previous_and_next_documents_for_search index, search_state.reset(current_search_session.query_params).to_hash

search_session['total'] = response.total
@search_context_response = response
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/blacklight/url_helper_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def start_over_path query_params = params
# link_back_to_catalog(label: 'Back to Search', route_set: my_engine)
def link_back_to_catalog(opts={:label=>nil})
scope = opts.delete(:route_set) || self
query_params = current_search_session.try(:query_params) || ActionController::Parameters.new
query_params = search_state.reset(current_search_session.try(:query_params)).to_hash

if search_session['counter']
per_page = (search_session['per_page'] || default_per_page).to_i
Expand Down
2 changes: 1 addition & 1 deletion app/views/search_history/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<table class="table table-striped search_history">
<%- @searches.each_with_index do |search,index| -%>
<%= content_tag :tr, :id => "document_#{index + 1}" do %>
<td class="query"><%= link_to_previous_search(search.query_params) %></td>
<td class="query"><%= link_to_previous_search(search_state.reset(search.query_params).to_hash) %></td>
<%- if has_user_authentication_provider? -%>
<td class="actions">
<%- if current_or_guest_user && search.saved? -%>
Expand Down
4 changes: 2 additions & 2 deletions lib/blacklight/search_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def to_hash
end
alias to_h to_hash

def reset
self.class.new(ActionController::Parameters.new, blacklight_config, controller)
def reset(params = nil)
self.class.new(params || ActionController::Parameters.new, blacklight_config, controller)
end

##
Expand Down
7 changes: 7 additions & 0 deletions lib/blacklight/solr/response/group_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ def start
params[:start].to_s.to_i
end

##
# Relying on a fallback (method missing) to @response is problematic as it
# will not evaluate the correct `total` method.
def empty?
total.zero?
end

def method_missing meth, *args, &block
if response.respond_to? meth
response.send(meth, *args, &block)
Expand Down
10 changes: 9 additions & 1 deletion spec/lib/blacklight/search_state_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
end
end

subject(:search_state) { described_class.new(params, blacklight_config, controller) }
let(:parameter_class) { ActionController::Parameters }
let(:controller) { double }
let(:search_state) { described_class.new(params, blacklight_config, controller) }
let(:params) { parameter_class.new }

describe '#to_h' do
Expand Down Expand Up @@ -297,4 +297,12 @@
expect(params).not_to have_key :f
end
end

describe '#reset' do
it 'returns a search state with the given parameters' do
new_state = search_state.reset('a' => 1)

expect(new_state.to_hash).to eq({ 'a' => 1 })
end
end
end
6 changes: 6 additions & 0 deletions spec/models/blacklight/solr/response/group_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
expect(group.group_limit).to eq 5
end
end

describe "empty?" do
it "uses the total from this object" do
expect(group.empty?).to be false
end
end
end

def create_response(response, params = {})
Expand Down

0 comments on commit 1ce5032

Please sign in to comment.