Skip to content

Commit

Permalink
Update Blacklight::SearchState to play well with Rails 5 AC::Paramete…
Browse files Browse the repository at this point in the history
…rs class
  • Loading branch information
cbeer committed Aug 9, 2016
1 parent 0d429e7 commit ac32e38
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/blacklight/search_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ class SearchState
# @param [ActionController::Parameters] params
# @param [Blacklight::Config] blacklight_config
def initialize(params, blacklight_config)
if params.instance_of? Hash
# This is an ActionView::TestCase workaround. Will be resolved by
# https://github.com/rails/rails/pull/22913 (Rails > 4.2.5)
@params = params.with_indifferent_access
else
if params.respond_to?(:to_unsafe_h)
# This is the typical (not-ActionView::TestCase) code path.
@params = params.to_unsafe_h
# In Rails 5 to_unsafe_h returns a HashWithIndifferentAccess, in Rails 4 it returns Hash
@params = @params.with_indifferent_access if @params.instance_of? Hash
elsif params.is_a? Hash
# This is an ActionView::TestCase workaround for Rails 4.2.
@params = params.dup.with_indifferent_access
else
@params = params.dup.to_h.with_indifferent_access
end

@blacklight_config = blacklight_config
end

def to_h
def to_hash
@params
end
alias to_h to_hash

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

if block_given?
yield my_params
Expand Down
34 changes: 34 additions & 0 deletions spec/lib/blacklight/search_state_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,40 @@
let(:search_state) { described_class.new(params, blacklight_config) }
let(:params) { parameter_class.new }

describe '#to_h' do
let(:data) { { a: '1'} }
let(:params) { parameter_class.new data }

it 'returns a copy of the original parameters' do
expect(search_state.to_h).to eq data.with_indifferent_access
expect(search_state.to_h.object_id).not_to eq params.object_id
end

context 'with AC::Parameters' do
let(:parameter_class) { ActionController::Parameters }

it 'returns the hash data' do
expect(search_state.to_h).to eq data.with_indifferent_access
end
end

context 'with HashWithIndifferentAccess' do
let(:parameter_class) { HashWithIndifferentAccess }

it 'returns the hash data' do
expect(search_state.to_h).to eq data.with_indifferent_access
end
end

context 'with Hash' do
let(:params) { data }

it 'returns the hash data' do
expect(search_state.to_h).to eq data.with_indifferent_access
end
end
end

describe "params_for_search" do
let(:params) { parameter_class.new 'default' => 'params' }

Expand Down

0 comments on commit ac32e38

Please sign in to comment.