This repository has been archived by the owner. It is now read-only.

Modify strong_parameters to be more compliant with accepts_nested_attributes_for #87

Open
wants to merge 2 commits into
from
Jump to file or symbol
Failed to load files and symbols.
+46 −4
Split
@@ -167,7 +167,7 @@ def hash_filter(params, filter)
array_of_permitted_scalars_filter(params, key)
else
# Declaration {:user => :name} or {:user => [:name, :age, {:adress => ...}]}.
- params[key] = each_element(value) do |element|
+ params[key] = each_element(key, value) do |element|
if element.is_a?(Hash)
element = self.class.new(element) unless element.respond_to?(:permit)
element.permit(*Array.wrap(filter[key]))
@@ -177,11 +177,11 @@ def hash_filter(params, filter)
end
end
- def each_element(value)
+ def each_element(key, value)
if value.is_a?(Array)
value.map { |el| yield el }.compact
- # fields_for on an array of records uses numeric hash keys.
- elsif value.is_a?(Hash) && value.keys.all? { |k| k =~ /\A-?\d+\z/ }
+ # fields_for on an array of records will label the key as fields_attributes
+ elsif value.is_a?(Hash) && key =~ /_attributes$/
hash = value.class.new
value.each { |k,v| hash[k] = yield v }
hash
@@ -290,4 +290,46 @@ def assert_filtered_out(params, key)
assert_filtered_out permitted[:book][:authors_attributes]['-1'], :age_of_death
end
+
+ test "acceptes_nested_attributes_for_style_nested_params random keys" do
+ params = ActionController::Parameters.new({
+ :entry_form => {
+ :user_responses_attributes => {
+ :'first_name' => { :response => 'William', :entry_field_id => '52', :stip_me => 'Dali' },
+ :'last_name' => { :response => 'Shakespeare', :entry_field_id => '27' },
+ :'phone' => { :response => %w(this bad phone number should be stripped), :entry_field_id => '16' }
+ }
+ }
+ })
+ permitted = params.permit :entry_form => { :user_responses_attributes => [ :response, :entry_field_id ] }
+
+ assert_not_nil permitted[:entry_form][:user_responses_attributes]['first_name']
+ assert_not_nil permitted[:entry_form][:user_responses_attributes]['last_name']
+ assert_equal 'William', permitted[:entry_form][:user_responses_attributes]['first_name'][:response]
+ assert_equal 'Shakespeare', permitted[:entry_form][:user_responses_attributes]['last_name'][:response]
+
+ assert_filtered_out permitted[:entry_form][:user_responses_attributes]['phone'], :response
+ assert_filtered_out permitted[:entry_form][:user_responses_attributes]['first_name'], :strip_me
+ end
+
+ test "acceptes_nested_attributes_for_style_nested_params no keys" do
+ params = ActionController::Parameters.new({
+ :entry_form => {
+ :user_responses_attributes => [
+ { :response => 'William', :entry_field_id => '52', :stip_me => 'Dali' },
+ { :response => 'Shakespeare', :entry_field_id => '27' },
+ { :response => %w(this bad phone number should be stripped), :entry_field_id => '16' }
+ ]
+ }
+ })
+ permitted = params.permit :entry_form => { :user_responses_attributes => [ :response, :entry_field_id ] }
+
+ assert_not_nil permitted[:entry_form][:user_responses_attributes][0]
+ assert_not_nil permitted[:entry_form][:user_responses_attributes][1]
+ assert_equal 'William', permitted[:entry_form][:user_responses_attributes][0][:response]
+ assert_equal 'Shakespeare', permitted[:entry_form][:user_responses_attributes][1][:response]
+
+ assert_filtered_out permitted[:entry_form][:user_responses_attributes][2], :response
+ assert_filtered_out permitted[:entry_form][:user_responses_attributes][0], :strip_me
+ end
end