Permalink
Browse files

Merge pull request #64 from frodsan/fix_issue

hash filters should be accessed with symbols or strings
  • Loading branch information...
2 parents 3996cf8 + d0062e7 commit 2df13a050efb6087960ec5978b5d84f291307b0e @rafaelfranca rafaelfranca committed Nov 30, 2012
Showing with 30 additions and 2 deletions.
  1. +2 −1 Gemfile.lock
  2. +2 −0 lib/action_controller/parameters.rb
  3. +26 −1 test/nested_parameters_test.rb
View
3 Gemfile.lock
@@ -63,6 +63,7 @@ PLATFORMS
ruby
DEPENDENCIES
- mocha
+ mocha (~> 0.12.0)
rake
+ rdoc
strong_parameters!
View
2 lib/action_controller/parameters.rb
@@ -46,6 +46,8 @@ def permit(*filters)
params[filter] = self[filter] if has_key?(filter)
keys.grep(/\A#{Regexp.escape(filter.to_s)}\(\d+[if]?\)\z/).each { |key| params[key] = self[key] }
when Hash then
+ filter = filter.with_indifferent_access
+
self.slice(*filter.keys).each do |key, value|
return unless value
View
27 test/nested_parameters_test.rb
@@ -32,6 +32,31 @@ class NestedParametersTest < ActiveSupport::TestCase
assert_nil permitted[:magazine]
end
+ test "permitted nested parameters with a string or a symbol as a key" do
+ params = ActionController::Parameters.new({
+ :book => {
+ 'authors' => [
+ { :name => "William Shakespeare", :born => "1564-04-26" },
+ { :name => "Christopher Marlowe" }
+ ]
+ }
+ })
+
+ permitted = params.permit :book => [ { 'authors' => [ :name ] } ]
+
+ assert_equal "William Shakespeare", permitted[:book]['authors'][0][:name]
+ assert_equal "William Shakespeare", permitted[:book][:authors][0][:name]
+ assert_equal "Christopher Marlowe", permitted[:book]['authors'][1][:name]
+ assert_equal "Christopher Marlowe", permitted[:book][:authors][1][:name]
+
+ permitted = params.permit :book => [ { :authors => [ :name ] } ]
+
+ assert_equal "William Shakespeare", permitted[:book]['authors'][0][:name]
+ assert_equal "William Shakespeare", permitted[:book][:authors][0][:name]
+ assert_equal "Christopher Marlowe", permitted[:book]['authors'][1][:name]
+ assert_equal "Christopher Marlowe", permitted[:book][:authors][1][:name]
+ end
+
test "nested arrays with strings" do
params = ActionController::Parameters.new({
:book => {
@@ -112,7 +137,7 @@ class NestedParametersTest < ActiveSupport::TestCase
end
test "fields_for_style_nested_params with negative numbers" do
- params = ActionController::Parameters.new({
+ params = ActionController::Parameters.new({
:book => {
:authors_attributes => {
:'-1' => { :name => 'William Shakespeare', :age_of_death => '52' },

0 comments on commit 2df13a0

Please sign in to comment.