Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed an exception when using Ajax based requests from Safari because…

… Safari appends a \000 to the post body. Symbols can't have \000 in them so indifferent access would throw an exception in the constructor. Indifferent hashes now use strings internally instead. #746 [Tobias Luetke]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@827 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit e4106a580ec5008f7a84be6f412e5d88d7a160cb 1 parent e834be7
David Heinemeier Hansson dhh authored
2  activesupport/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* Fixed an exception when using Ajax based requests from Safari because Safari appends a \000 to the post body. Symbols can't have \000 in them so indifferent access would throw an exception in the constructor. Indifferent hashes now use strings internally instead. #746 [Tobias Luetke]
+
* Added String#to_time and String#to_date for wrapping ParseDate
8 activesupport/lib/active_support/core_ext/hash/indifferent_access.rb
View
@@ -1,8 +1,8 @@
class HashWithIndifferentAccess < Hash
- def initialize(constructor)
+ def initialize(constructor = {})
if constructor.is_a?(Hash)
super()
- update(constructor.symbolize_keys)
+ update(constructor.stringify_keys)
else
super(constructor)
end
@@ -12,7 +12,7 @@ def initialize(constructor)
def [](key)
case key
- when Symbol: regular_reader(key) || regular_reader(key.to_s)
+ when Symbol: regular_reader(key.to_s) || regular_reader(key)
when String: regular_reader(key) || regular_reader(key.to_sym)
else regular_reader(key)
end
@@ -21,7 +21,7 @@ def [](key)
alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
def []=(key, value)
- regular_writer(key.is_a?(String) ? key.to_sym : key, value)
+ regular_writer(key.is_a?(Symbol) ? key.to_s : key, value)
end
end
26 activesupport/test/core_ext/hash_ext_test.rb
View
@@ -3,6 +3,7 @@
class HashExtTest < Test::Unit::TestCase
def setup
+
@strings = { 'a' => 1, 'b' => 2 }
@symbols = { :a => 1, :b => 2 }
@mixed = { :a => 1, 'b' => 2 }
@@ -31,7 +32,7 @@ def test_symbolize_keys!
assert_equal @symbols, @strings.dup.symbolize_keys!
assert_equal @symbols, @mixed.dup.symbolize_keys!
- assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys! }
+ assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys }
end
def test_stringify_keys
@@ -50,11 +51,24 @@ def test_indifferent_access
@strings = @strings.with_indifferent_access
@symbols = @symbols.with_indifferent_access
@mixed = @mixed.with_indifferent_access
-
- assert_equal @strings[:a], @strings["a"]
- assert_equal @symbols[:a], @symbols["a"]
- assert_equal @strings["b"], @mixed["b"]
- assert_equal @strings[:b], @mixed["b"]
+
+ assert_equal @strings[:a], @strings['a']
+ assert_equal @symbols[:a], @symbols['a']
+ assert_equal @strings['b'], @mixed['b']
+ assert_equal @strings[:b], @mixed['b']
+ end
+
+ def test_indifferent_writing
+ hash = HashWithIndifferentAccess.new
+ hash[:a] = 1
+ hash['b'] = 2
+ hash[3] = 3
+
+ assert_equal hash['a'], 1
+ assert_equal hash['b'], 2
+ assert_equal hash[:a], 1
+ assert_equal hash[:b], 2
+ assert_equal hash[3], 3
end
def test_assert_valid_keys
Please sign in to comment.
Something went wrong with that request. Please try again.