aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack
diff options
context:
space:
mode:
authorLukas Zapletal <lzap+git@redhat.com>2018-09-25 14:47:39 +0200
committerLukas Zapletal <lzap+git@redhat.com>2018-09-27 16:16:44 +0200
commit58e87c3f54d519fc72103ab1bac6d60d26b11577 (patch)
treeb56c078ec48d2bfd839d5606210f9da72d811772 /actionpack
parent4f3eb86ffec8ebc4faea3143dda8ebb5aad74d08 (diff)
downloadrails-58e87c3f54d519fc72103ab1bac6d60d26b11577.tar.gz
rails-58e87c3f54d519fc72103ab1bac6d60d26b11577.tar.bz2
rails-58e87c3f54d519fc72103ab1bac6d60d26b11577.zip
Added ActionController::Parameters.each_value method
Diffstat (limited to 'actionpack')
-rw-r--r--actionpack/lib/action_controller/metal/strong_parameters.rb8
-rw-r--r--actionpack/test/controller/parameters/accessors_test.rb18
2 files changed, 26 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/metal/strong_parameters.rb b/actionpack/lib/action_controller/metal/strong_parameters.rb
index a37f08d944..c1272ce667 100644
--- a/actionpack/lib/action_controller/metal/strong_parameters.rb
+++ b/actionpack/lib/action_controller/metal/strong_parameters.rb
@@ -348,6 +348,14 @@ module ActionController
end
alias_method :each, :each_pair
+ # Convert all hashes in values into parameters, then yield each value in
+ # the same way as <tt>Hash#each_value</tt>.
+ def each_value(&block)
+ @parameters.each_pair do |key, value|
+ yield [convert_hashes_to_parameters(key, value)]
+ end
+ end
+
# Attribute that keeps track of converted arrays, if any, to avoid double
# looping in the common use case permit + mass-assignment. Defined in a
# method to instantiate it only if needed.
diff --git a/actionpack/test/controller/parameters/accessors_test.rb b/actionpack/test/controller/parameters/accessors_test.rb
index 68c7f2d9ea..9f1fb3d042 100644
--- a/actionpack/test/controller/parameters/accessors_test.rb
+++ b/actionpack/test/controller/parameters/accessors_test.rb
@@ -75,6 +75,24 @@ class ParametersAccessorsTest < ActiveSupport::TestCase
end
end
+ test "each_value carries permitted status" do
+ @params.permit!
+ @params["person"].each_value { |value| assert(value.permitted?) if value == 32 }
+ end
+
+ test "each_value carries unpermitted status" do
+ @params["person"].each_value { |value| assert_not(value.permitted?) if value == 32 }
+ end
+
+ test "each_key converts to hash for permitted" do
+ @params.permit!
+ @params.each_key { |key| assert_kind_of(String, key) if key == "person" }
+ end
+
+ test "each_key converts to hash for unpermitted" do
+ @params.each_key { |key| assert_kind_of(String, key) if key == "person" }
+ end
+
test "empty? returns true when params contains no key/value pairs" do
params = ActionController::Parameters.new
assert_empty params