diff --git a/lib/devise/rails/warden_compat.rb b/lib/devise/rails/warden_compat.rb index b6e2340b..ba62fdda 100644 --- a/lib/devise/rails/warden_compat.rb +++ b/lib/devise/rails/warden_compat.rb @@ -47,6 +47,10 @@ unless Devise.rack_session? alias_method :regular_writer, :[]= unless method_defined?(:regular_writer) alias_method :regular_update, :update unless method_defined?(:regular_update) + def [](key) + super(convert_key(key)) + end + def []=(key, value) regular_writer(convert_key(key), value) end @@ -91,6 +95,7 @@ unless Devise.rack_session? def symbolize_keys; to_hash.symbolize_keys end def to_options!; self end + def to_hash; Hash.new.update(self) end protected diff --git a/test/indifferent_hash.rb b/test/indifferent_hash.rb new file mode 100644 index 00000000..43dd7529 --- /dev/null +++ b/test/indifferent_hash.rb @@ -0,0 +1,33 @@ +require 'test_helper' + +class IndifferentHashTest < ActiveSupport::TestCase + setup do + @hash = Devise::IndifferentHash.new + end + + test "it overwrites getter and setter" do + @hash[:foo] = "bar" + assert_equal "bar", @hash["foo"] + assert_equal "bar", @hash[:foo] + + @hash["foo"] = "baz" + assert_equal "baz", @hash["foo"] + assert_equal "baz", @hash[:foo] + end + + test "it overwrites update" do + @hash.update :foo => "bar" + assert_equal "bar", @hash["foo"] + assert_equal "bar", @hash[:foo] + + @hash.update "foo" => "baz" + assert_equal "baz", @hash["foo"] + assert_equal "baz", @hash[:foo] + end + + test "it returns a Hash on to_hash" do + @hash[:foo] = "bar" + assert_equal Hash["foo", "bar"], @hash.to_hash + assert_kind_of Hash, @hash.to_hash + end +end if defined?(Devise::IndifferentHash) \ No newline at end of file