Skip to content
This repository was archived by the owner on Aug 17, 2017. It is now read-only.

Allow parameter filters to match multi-parameter attributes (DHH compatible) #39

Merged
merged 2 commits into from
Oct 5, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/action_controller/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def permit(*filters)
case filter
when Symbol, String then
params[filter] = self[filter] if has_key?(filter)
keys.grep(/\A#{Regexp.escape(filter)}\(\di\)\z/).each { |key| params[key] = self[key] }
when Hash then
self.slice(*filter.keys).each do |key, value|
return unless value
Expand Down
34 changes: 34 additions & 0 deletions test/multi_parameter_attributes_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'test_helper'
require 'action_controller/parameters'

class MultiParameterAttributesTest < ActiveSupport::TestCase
test "permitted multi-parameter attribute keys" do
params = ActionController::Parameters.new({
book: {
"shipped_at(1i)" => "2012",
"shipped_at(2i)" => "3",
"shipped_at(3i)" => "25",
"shipped_at(4i)" => "10",
"shipped_at(5i)" => "15",
"published_at(1i)" => "1999",
"published_at(2i)" => "2",
"published_at(3i)" => "5"
}
})

permitted = params.permit book: [ :shipped_at ]

assert permitted.permitted?

assert_equal "2012", permitted[:book]["shipped_at(1i)"]
assert_equal "3", permitted[:book]["shipped_at(2i)"]
assert_equal "25", permitted[:book]["shipped_at(3i)"]
assert_equal "10", permitted[:book]["shipped_at(4i)"]
assert_equal "15", permitted[:book]["shipped_at(5i)"]

assert_nil permitted[:book]["published_at(1i)"]
assert_nil permitted[:book]["published_at(2i)"]
assert_nil permitted[:book]["published_at(3i)"]
end
end