Skip to content

Commit 84b861f

Browse files
committed
Update documentation on AC::Parameters
1 parent 14a3bd5 commit 84b861f

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

actionpack/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
`permitted?` status or even getting back a pure `Hash` object instead of
77
a `Parameters` object with proper sanitization.
88

9-
By stop inheriting from `HashWithIndifferentAccess`, we are able to make
9+
By not inheriting from `HashWithIndifferentAccess`, we are able to make
1010
sure that all methods that are defined in `Parameters` object will return
1111
a proper `Parameters` object with a correct `permitted?` flag.
1212

actionpack/lib/action_controller/metal/strong_parameters.rb

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ def initialize(parameters = {})
151151
@permitted = self.class.permit_all_parameters
152152
end
153153

154+
# Returns true if another +Parameters+ object contains the same content and
155+
# permitted flag, or other Hash-like object contains the same content. This
156+
# override is in place so you can perform a comparison with `Hash`.
154157
def ==(other_hash)
155158
if other_hash.respond_to?(:permitted?)
156159
super
@@ -357,6 +360,8 @@ def [](key)
357360
convert_hashes_to_parameters(key, @parameters[key])
358361
end
359362

363+
# Assigns a value to a given +key+. The given key may still get filtered out
364+
# when +permit+ is called.
360365
def []=(key, value)
361366
@parameters[key] = value
362367
end
@@ -393,11 +398,19 @@ def slice(*keys)
393398
new_instance_with_inherited_permitted_status(@parameters.slice(*keys))
394399
end
395400

401+
# Returns current <tt>ActionController::Parameters</tt> instance which
402+
# contains only the given +keys+.
396403
def slice!(*keys)
397404
@parameters.slice!(*keys)
398405
self
399406
end
400407

408+
# Returns a new <tt>ActionController::Parameters</tt> instance that
409+
# filters out the given +keys+.
410+
#
411+
# params = ActionController::Parameters.new(a: 1, b: 2, c: 3)
412+
# params.except(:a, :b) # => {"c"=>3}
413+
# params.except(:d) # => {"a"=>1,"b"=>2,"c"=>3}
401414
def except(*keys)
402415
new_instance_with_inherited_permitted_status(@parameters.except(*keys))
403416
end
@@ -427,15 +440,16 @@ def transform_values(&block)
427440
end
428441
end
429442

443+
# Performs values transformation and returns the altered
444+
# <tt>ActionController::Parameters</tt> instance.
430445
def transform_values!(&block)
431446
@parameters.transform_values!(&block)
432447
self
433448
end
434449

435-
# This method is here only to make sure that the returned object has the
436-
# correct +permitted+ status. It should not matter since the parent of
437-
# this object is +HashWithIndifferentAccess+
438-
def transform_keys(&block) # :nodoc:
450+
# Returns a new <tt>ActionController::Parameters</tt> instance with the
451+
# results of running +block+ once for every key. The values are unchanged.
452+
def transform_keys(&block)
439453
if block
440454
new_instance_with_inherited_permitted_status(
441455
@parameters.transform_keys(&block)
@@ -445,6 +459,8 @@ def transform_keys(&block) # :nodoc:
445459
end
446460
end
447461

462+
# Performs keys transfomration and returns the altered
463+
# <tt>ActionController::Parameters</tt> instance.
448464
def transform_keys!(&block)
449465
@parameters.transform_keys!(&block)
450466
self
@@ -458,6 +474,8 @@ def delete(key, &block)
458474
convert_hashes_to_parameters(key, @parameters.delete(key), false)
459475
end
460476

477+
# Returns a new instance of <tt>ActionController::Parameters</tt> with only
478+
# items that the block evaluates to true.
461479
def select(&block)
462480
new_instance_with_inherited_permitted_status(@parameters.select(&block))
463481
end
@@ -469,16 +487,21 @@ def select!(&block)
469487
end
470488
alias_method :keep_if, :select!
471489

490+
# Returns a new instance of <tt>ActionController::Parameters</tt> with items
491+
# that the block evaluates to true removed.
472492
def reject(&block)
473493
new_instance_with_inherited_permitted_status(@parameters.reject(&block))
474494
end
475495

496+
# Removes items that the block evaluates to true and returns self.
476497
def reject!(&block)
477498
@parameters.reject!(&block)
478499
self
479500
end
480501
alias_method :delete_if, :reject!
481502

503+
# Return values that were assigned to the given +keys+. Note that all the
504+
# +Hash+ objects will be converted to <tt>ActionController::Parameters</tt>.
482505
def values_at(*keys)
483506
convert_value_to_parameters(@parameters.values_at(*keys))
484507
end
@@ -497,15 +520,18 @@ def dup
497520
end
498521
end
499522

523+
# Returns a new <tt>ActionController::Parameters</tt> with all keys from
524+
# +other_hash+ merges into current hash.
500525
def merge(other_hash)
501526
new_instance_with_inherited_permitted_status(
502527
@parameters.merge(other_hash)
503528
)
504529
end
505530

506531
# This is required by ActiveModel attribute assignment, so that user can
507-
# pass +Parameters+ to a mass assignment methods in a model.
508-
def stringify_keys
532+
# pass +Parameters+ to a mass assignment methods in a model. It should not
533+
# matter as we are using +HashWithIndifferentAccess+ internally.
534+
def stringify_keys # :nodoc:
509535
dup
510536
end
511537

0 commit comments

Comments
 (0)