Skip to content

Commit

Permalink
Merge pull request rails#22582 from sikachu/hwia-to_h
Browse files Browse the repository at this point in the history
Make Parameters#to_h and #to_unsafe_h return HWIA
  • Loading branch information
kaspth committed Dec 14, 2015
2 parents 05bb068 + 6d4aef9 commit 757a566
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 7 additions & 5 deletions actionpack/lib/action_controller/metal/strong_parameters.rb
Expand Up @@ -162,8 +162,8 @@ def ==(other_hash)
end
end

# Returns a safe +Hash+ representation of this parameter with all
# unpermitted keys removed.
# Returns a safe <tt>ActiveSupport::HashWithIndifferentAccess</tt>
# representation of this parameter with all unpermitted keys removed.
#
# params = ActionController::Parameters.new({
# name: 'Senjougahara Hitagi',
Expand All @@ -175,15 +175,17 @@ def ==(other_hash)
# safe_params.to_h # => {"name"=>"Senjougahara Hitagi"}
def to_h
if permitted?
@parameters.to_h
@parameters.deep_dup
else
slice(*self.class.always_permitted_parameters).permit!.to_h
end
end

# Returns an unsafe, unfiltered +Hash+ representation of this parameter.
# Returns an unsafe, unfiltered
# <tt>ActiveSupport::HashWithIndifferentAccess</tt> representation of this
# parameter.
def to_unsafe_h
@parameters.to_h
@parameters.deep_dup
end
alias_method :to_unsafe_hash, :to_unsafe_h

Expand Down
Expand Up @@ -256,15 +256,15 @@ def assert_filtered_out(params, key)
end

test "to_h returns empty hash on unpermitted params" do
assert @params.to_h.is_a? Hash
assert @params.to_h.is_a? ActiveSupport::HashWithIndifferentAccess
assert_not @params.to_h.is_a? ActionController::Parameters
assert @params.to_h.empty?
end

test "to_h returns converted hash on permitted params" do
@params.permit!

assert @params.to_h.is_a? Hash
assert @params.to_h.is_a? ActiveSupport::HashWithIndifferentAccess
assert_not @params.to_h.is_a? ActionController::Parameters
end

Expand All @@ -273,7 +273,7 @@ def assert_filtered_out(params, key)
ActionController::Parameters.permit_all_parameters = true
params = ActionController::Parameters.new(crab: "Senjougahara Hitagi")

assert params.to_h.is_a? Hash
assert params.to_h.is_a? ActiveSupport::HashWithIndifferentAccess
assert_not @params.to_h.is_a? ActionController::Parameters
assert_equal({ "crab" => "Senjougahara Hitagi" }, params.to_h)
ensure
Expand All @@ -294,7 +294,7 @@ def assert_filtered_out(params, key)
end

test "to_unsafe_h returns unfiltered params" do
assert @params.to_h.is_a? Hash
assert @params.to_h.is_a? ActiveSupport::HashWithIndifferentAccess
assert_not @params.to_h.is_a? ActionController::Parameters
end
end

0 comments on commit 757a566

Please sign in to comment.