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 #17

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 11 additions & 1 deletion lib/action_controller/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def permit!
def require(key)
self[key].presence || raise(ActionController::ParameterMissing.new(key))
end

alias :required :require

def permit(*filters)
Expand All @@ -39,6 +39,10 @@ def permit(*filters)
case filter
when Symbol then
params[filter] = self[filter] if has_key?(filter)

multi_param_keys_for(filter).each do |multi_filter|
params[multi_filter] = self[multi_filter]
end
when Hash then
self.slice(*filter.keys).each do |key, value|
return unless value
Expand Down Expand Up @@ -97,6 +101,12 @@ def each_element(object)
yield object
end
end

def multi_param_keys_for(filter)
keys.select { |key|
key.index("#{filter}(") == 0 && key.index(")") == key.length - 1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use regexp for this?

keys.select do |key|
  key.match /#{filter}\(\d+i\)/
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#index was the fastest way to figure it out when I measured different approaches. I agree it's cleaner with a Regexp though.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i found regexp are almost always faster than anything when it comes to strings. maybe with other regexp it will be faster:

key.match /\A#{filter}\(\d{1}i\)\z/i

}
end
end

module StrongParameters
Expand Down
33 changes: 33 additions & 0 deletions test/multi_parameter_attributes_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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