diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 2a9ced6dc002e8852493150d209743004db74dba..ce7e0a1e42903d3bc45e20a58744b154acfa3fad 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed double requiring of models with the same name as the controller + * Fixed that query params could be forced to nil on a POST due to the raw post fix #562 [moriq@moriq.com] * Fixed that cookies shouldn't be frozen in TestRequest #571 [Eric Hodel] diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 15c9b5b6ba0126b58f672a334a4bbfbfd321e9e9..dfe26395260a07794f03f9f550311c404309c281 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,3 +1,5 @@ +* Added IndifferentAccess as a way to wrap a hash by a symbol-based store that also can be accessed by string keys + * Added Inflector.humanize to turn attribute names like employee_salary into "Employee salary". Used by automated error reporting in AR. * Added availability of class inheritable attributes to the masses #477 [bitsweat] diff --git a/activesupport/lib/core_ext/hash.rb b/activesupport/lib/core_ext/hash.rb index a5c53db1e02bd57f368fbadda48d16c698e0cf43..e899d3e1e2a9af4963b90029618494e9f1abcb68 100644 --- a/activesupport/lib/core_ext/hash.rb +++ b/activesupport/lib/core_ext/hash.rb @@ -1,5 +1,7 @@ require File.dirname(__FILE__) + '/hash/keys' +require File.dirname(__FILE__) + '/hash/indifferent_access' class Hash #:nodoc: include ActiveSupport::CoreExtensions::Hash::Keys + include ActiveSupport::CoreExtensions::Hash::IndifferentAccess end diff --git a/activesupport/lib/core_ext/hash/indifferent_access.rb b/activesupport/lib/core_ext/hash/indifferent_access.rb new file mode 100644 index 0000000000000000000000000000000000000000..3fe099986650abf94c82fb4cc4db3d261a167433 --- /dev/null +++ b/activesupport/lib/core_ext/hash/indifferent_access.rb @@ -0,0 +1,38 @@ +class HashWithIndifferentAccess < Hash + def initialize(constructor) + if constructor.is_a?(Hash) + super() + update(constructor.symbolize_keys) + else + super(constructor) + end + end + + alias_method :regular_read, :[] + + def [](key) + case key + when Symbol: regular_read(key) || regular_read(key.to_s) + when String: regular_read(key) || regular_read(key.to_sym) + else regular_read(key) + end + end + + alias_method :regular_writer, :[]= + + def []=(key, value) + regular_writer(key.is_a?(String) ? key.to_sym : key, value) + end +end + +module ActiveSupport #:nodoc: + module CoreExtensions #:nodoc: + module Hash #:nodoc: + module IndifferentAccess + def with_indifferent_access + HashWithIndifferentAccess.new(self) + end + end + end + end +end diff --git a/activesupport/lib/core_ext/hash/keys.rb b/activesupport/lib/core_ext/hash/keys.rb index 536995dd71fbebd594578f0f35c43c49ac0a4580..4dd982337c85d4e2985fb23b18a9f3b256a3c734 100644 --- a/activesupport/lib/core_ext/hash/keys.rb +++ b/activesupport/lib/core_ext/hash/keys.rb @@ -2,7 +2,6 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module Hash #:nodoc: module Keys - # Return a new hash with all keys converted to symbols. def symbolize_keys inject({}) do |options, (key, value)| diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index 52a28c15534fe3751757c5c1f44db098fca132be..de85db2edf58c22f537137b2ef349d99289a47dc 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -2,7 +2,6 @@ require File.dirname(__FILE__) + '/../../lib/core_ext/hash' class HashExtTest < Test::Unit::TestCase - def setup @strings = { 'a' => 1, 'b' => 2 } @symbols = { :a => 1, :b => 2 } @@ -33,6 +32,17 @@ def test_symbolize_keys! assert_raises(NoMethodError) { { [] => 1 }.symbolize_keys! } end + 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"] + end + def test_assert_valid_keys assert_nothing_raised do { :failure => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])