diff --git a/lib/active_interaction/backports.rb b/lib/active_interaction/backports.rb index 0793a584..56b6e391 100644 --- a/lib/active_interaction/backports.rb +++ b/lib/active_interaction/backports.rb @@ -19,15 +19,3 @@ class Errors protected :initialize_dup end end - -# @private -class Hash - # Required for Rails < 4.0.0. - def transform_keys - result = {} - each_key do |key| - result[yield(key)] = self[key] - end - result - end unless method_defined?(:transform_keys) -end diff --git a/lib/active_interaction/filters/hash_filter.rb b/lib/active_interaction/filters/hash_filter.rb index b3135ddb..94249468 100644 --- a/lib/active_interaction/filters/hash_filter.rb +++ b/lib/active_interaction/filters/hash_filter.rb @@ -32,11 +32,12 @@ class HashFilter < Filter def cast(value) case value when Hash - value = stringify_the_symbol_keys(value) + value = value.with_indifferent_access + initial = strip? ? ActiveSupport::HashWithIndifferentAccess.new : value - filters.each_with_object(strip? ? {} : value) do |(name, filter), h| + filters.each_with_object(initial) do |(name, filter), h| clean_value(h, name.to_s, filter, value) - end.symbolize_keys + end else super end @@ -74,9 +75,5 @@ def raw_default def strip? options.fetch(:strip, true) end - - def stringify_the_symbol_keys(hash) - hash.transform_keys { |key| key.is_a?(Symbol) ? key.to_s : key } - end end end diff --git a/spec/active_interaction/filters/hash_filter_spec.rb b/spec/active_interaction/filters/hash_filter_spec.rb index 492896e5..b5778120 100644 --- a/spec/active_interaction/filters/hash_filter_spec.rb +++ b/spec/active_interaction/filters/hash_filter_spec.rb @@ -37,7 +37,7 @@ let(:block) { proc { hash :a } } context 'with a Hash' do - let(:value) { { a: {} } } + let(:value) { { 'a' => {} } } it 'returns the Hash' do expect(result).to eql value diff --git a/spec/active_interaction/integration/hash_interaction_spec.rb b/spec/active_interaction/integration/hash_interaction_spec.rb index 988b070d..29561fed 100644 --- a/spec/active_interaction/integration/hash_interaction_spec.rb +++ b/spec/active_interaction/integration/hash_interaction_spec.rb @@ -21,11 +21,11 @@ before { inputs.merge!(a: a) } it 'returns the correct value for :a' do - expect(result[:a]).to eql a.symbolize_keys + expect(result[:a]).to eql a.with_indifferent_access end it 'returns the correct value for :b' do - expect(result[:b]).to eql(x: {}) + expect(result[:b]).to eql('x' => {}) end end