Skip to content

Commit ac32e38

Browse files
committed
Update Blacklight::SearchState to play well with Rails 5 AC::Parameters class
1 parent 0d429e7 commit ac32e38

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

lib/blacklight/search_state.rb

+10-7
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,25 @@ class SearchState
1010
# @param [ActionController::Parameters] params
1111
# @param [Blacklight::Config] blacklight_config
1212
def initialize(params, blacklight_config)
13-
if params.instance_of? Hash
14-
# This is an ActionView::TestCase workaround. Will be resolved by
15-
# https://github.com/rails/rails/pull/22913 (Rails > 4.2.5)
16-
@params = params.with_indifferent_access
17-
else
13+
if params.respond_to?(:to_unsafe_h)
1814
# This is the typical (not-ActionView::TestCase) code path.
1915
@params = params.to_unsafe_h
2016
# In Rails 5 to_unsafe_h returns a HashWithIndifferentAccess, in Rails 4 it returns Hash
2117
@params = @params.with_indifferent_access if @params.instance_of? Hash
18+
elsif params.is_a? Hash
19+
# This is an ActionView::TestCase workaround for Rails 4.2.
20+
@params = params.dup.with_indifferent_access
21+
else
22+
@params = params.dup.to_h.with_indifferent_access
2223
end
24+
2325
@blacklight_config = blacklight_config
2426
end
2527

26-
def to_h
28+
def to_hash
2729
@params
2830
end
31+
alias to_h to_hash
2932

3033
def reset
3134
Blacklight::SearchState.new(ActionController::Parameters.new, blacklight_config)
@@ -112,7 +115,7 @@ def remove_facet_params(field, item)
112115
# @yield [params] The merged parameters hash before being sanitized
113116
def params_for_search(params_to_merge={}, &block)
114117
# params hash we'll return
115-
my_params = params.dup.merge(params_to_merge.dup)
118+
my_params = params.dup.merge(Blacklight::SearchState.new(params_to_merge, blacklight_config))
116119

117120
if block_given?
118121
yield my_params

spec/lib/blacklight/search_state_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,40 @@
1212
let(:search_state) { described_class.new(params, blacklight_config) }
1313
let(:params) { parameter_class.new }
1414

15+
describe '#to_h' do
16+
let(:data) { { a: '1'} }
17+
let(:params) { parameter_class.new data }
18+
19+
it 'returns a copy of the original parameters' do
20+
expect(search_state.to_h).to eq data.with_indifferent_access
21+
expect(search_state.to_h.object_id).not_to eq params.object_id
22+
end
23+
24+
context 'with AC::Parameters' do
25+
let(:parameter_class) { ActionController::Parameters }
26+
27+
it 'returns the hash data' do
28+
expect(search_state.to_h).to eq data.with_indifferent_access
29+
end
30+
end
31+
32+
context 'with HashWithIndifferentAccess' do
33+
let(:parameter_class) { HashWithIndifferentAccess }
34+
35+
it 'returns the hash data' do
36+
expect(search_state.to_h).to eq data.with_indifferent_access
37+
end
38+
end
39+
40+
context 'with Hash' do
41+
let(:params) { data }
42+
43+
it 'returns the hash data' do
44+
expect(search_state.to_h).to eq data.with_indifferent_access
45+
end
46+
end
47+
end
48+
1549
describe "params_for_search" do
1650
let(:params) { parameter_class.new 'default' => 'params' }
1751

0 commit comments

Comments
 (0)