From ae590d73561169a340d90b7c175b404849475663 Mon Sep 17 00:00:00 2001 From: Taylor Fausak Date: Thu, 14 Aug 2014 17:07:11 -0500 Subject: [PATCH 1/2] Fix #164; convert hashes to indifferent access --- lib/active_interaction/filters/hash_filter.rb | 2 +- spec/active_interaction/filters/hash_filter_spec.rb | 2 +- spec/active_interaction/integration/hash_interaction_spec.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/active_interaction/filters/hash_filter.rb b/lib/active_interaction/filters/hash_filter.rb index b3135ddb..4c30fd93 100644 --- a/lib/active_interaction/filters/hash_filter.rb +++ b/lib/active_interaction/filters/hash_filter.rb @@ -36,7 +36,7 @@ def cast(value) filters.each_with_object(strip? ? {} : value) do |(name, filter), h| clean_value(h, name.to_s, filter, value) - end.symbolize_keys + end.with_indifferent_access else super 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 From 7f798041271ebac7b286190c87ddffa938b5da0e Mon Sep 17 00:00:00 2001 From: Taylor Fausak Date: Thu, 14 Aug 2014 17:47:28 -0500 Subject: [PATCH 2/2] Remove stringify_the_symbol_keys and transform_keys --- lib/active_interaction/backports.rb | 12 ------------ lib/active_interaction/filters/hash_filter.rb | 11 ++++------- 2 files changed, 4 insertions(+), 19 deletions(-) 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 4c30fd93..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.with_indifferent_access + 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