Skip to content

Commit

Permalink
Merge pull request #2290 from projectblacklight/params-compat
Browse files Browse the repository at this point in the history
Add compatibility shim between SearchState and ActionController::Para…
  • Loading branch information
jcoyne authored May 28, 2020
2 parents d4498f8 + 4c63215 commit 395edc3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/blacklight/search_state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ def to_hash
end
alias to_h to_hash

def to_unsafe_h
Deprecation.warn(self, 'Use SearchState#to_h instead of SearchState#to_unsafe_h')
to_hash
end

def method_missing(method_name, *arguments, &block)
if @params.respond_to?(method_name)
Deprecation.warn(self, "Calling `#{method_name}` on Blacklight::SearchState " \
'is deprecated and will be removed in Blacklight 8. Call #to_h first if you ' \
' need to use hash methods (or, preferably, use your own SearchState implementation)')
@params.public_send(method_name, *arguments, &block)
else
super
end
end

def respond_to_missing?(method_name, include_private = false)
@params.respond_to?(method_name, include_private) || super
end

# Tiny shim to make it easier to migrate raw params access to using this class
delegate :[], to: :params
deprecation_deprecate :[]
Expand Down
12 changes: 12 additions & 0 deletions spec/lib/blacklight/search_state_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@
end
end

describe 'interface compatibility with params' do
let(:params) { parameter_class.new f: { ff: ['xyz'] } }

it 'implements param methods' do
Deprecation.silence(described_class) do
expect(search_state.to_unsafe_h).to eq('f' => { 'ff' => ['xyz'] })
expect(search_state.fetch(:f)).to eq('ff' => ['xyz'])
expect(search_state.select { |k, _v,| k == 'f' }).to eq('f' => { 'ff' => ['xyz'] })
end
end
end

describe '#query_param' do
let(:params) { parameter_class.new q: 'xyz' }

Expand Down

0 comments on commit 395edc3

Please sign in to comment.