Skip to content

Commit

Permalink
Fix failing test on several methods on Parameter
Browse files Browse the repository at this point in the history
* `each`
* `each_pair`
* `delete`
* `select!`
  • Loading branch information
sikachu committed Aug 19, 2014
1 parent 0663e8f commit 3591dd5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
26 changes: 25 additions & 1 deletion actionpack/lib/action_controller/metal/strong_parameters.rb
Expand Up @@ -160,6 +160,18 @@ def to_h
end
end

# Convert all hashes in values into parameters, then yield each pair like
# the same way as <tt>Hash#each_pair</tt>
def each_pair(&block)
super do |key, value|
convert_hashes_to_parameters(key, value)
end

super
end

alias_method :each, :each_pair

# 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.
Expand Down Expand Up @@ -195,7 +207,6 @@ def permitted?
# Person.new(params) # => #<Person id: nil, name: "Francesco">
def permit!
each_pair do |key, value|
value = convert_hashes_to_parameters(key, value)
Array.wrap(value).each do |v|
v.permit! if v.respond_to? :permit!
end
Expand Down Expand Up @@ -387,6 +398,19 @@ def transform_keys # :nodoc:
end
end

# Deletes and returns a key-value pair from +Parameters+ whose key is equal
# to key. If the key is not found, returns the default value. If the
# optional code block is given and the key is not found, pass in the key
# and return the result of block.
def delete(key, &block)
convert_hashes_to_parameters(key, super, false)
end

# Equivalent to Hash#keep_if, but returns nil if no changes were made.
def select!(&block)
convert_value_to_parameters(super)
end

# Returns an exact copy of the <tt>ActionController::Parameters</tt>
# instance. +permitted+ state is kept on the duped object.
#
Expand Down
9 changes: 9 additions & 0 deletions actionpack/test/controller/parameters/accessors_test.rb
Expand Up @@ -36,6 +36,15 @@ class ParametersAccessorsTest < ActiveSupport::TestCase
@params.each { |key, value| assert_not(value.permitted?) if key == "person" }
end

test "each_pair carries permitted status" do
@params.permit!
@params.each_pair { |key, value| assert(value.permitted?) if key == "person" }
end

test "each_pair carries unpermitted status" do
@params.each_pair { |key, value| assert_not(value.permitted?) if key == "person" }
end

test "except retains permitted status" do
@params.permit!
assert @params.except(:person).permitted?
Expand Down
4 changes: 2 additions & 2 deletions actionpack/test/controller/parameters/mutators_test.rb
Expand Up @@ -63,11 +63,11 @@ class ParametersMutatorsTest < ActiveSupport::TestCase

test "select! retains permitted status" do
@params.permit!
assert @params.select! { |k| k == "person" }.permitted?
assert @params.select! { |k| k != "person" }.permitted?
end

test "select! retains unpermitted status" do
assert_not @params.select! { |k| k == "person" }.permitted?
assert_not @params.select! { |k| k != "person" }.permitted?
end

test "slice! retains permitted status" do
Expand Down

0 comments on commit 3591dd5

Please sign in to comment.