diff --git a/lib/blacklight/search_state.rb b/lib/blacklight/search_state.rb index a6ce863ccb..55ed1297b2 100644 --- a/lib/blacklight/search_state.rb +++ b/lib/blacklight/search_state.rb @@ -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 :[] diff --git a/spec/lib/blacklight/search_state_spec.rb b/spec/lib/blacklight/search_state_spec.rb index 8e054b1f1b..014a946e86 100644 --- a/spec/lib/blacklight/search_state_spec.rb +++ b/spec/lib/blacklight/search_state_spec.rb @@ -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' }