Class: ActionController::Parameters

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/action_controller/parameters.rb

Constant Summary collapse

NEVER_UNPERMITTED_PARAMS =

Never raise an UnpermittedParameters exception because of these params are present. They are added by Rails and it's of no concern.

%w( controller action )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil, klass = String) ⇒ Parameters

Returns a new instance of Parameters.


40
41
42
43
44
# File 'lib/action_controller/parameters.rb', line 40

def initialize(attributes = nil, klass = String)
  super(attributes)
  @permitted = false
  @klass = klass
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass


32
33
34
# File 'lib/action_controller/parameters.rb', line 32

def klass
  @klass
end

#permittedObject Also known as: permitted?

Returns the value of attribute permitted


30
31
32
# File 'lib/action_controller/parameters.rb', line 30

def permitted
  @permitted
end

Instance Method Details

#[](key) ⇒ Object


78
79
80
# File 'lib/action_controller/parameters.rb', line 78

def [](key)
  convert_hashes_to_parameters(key, super)
end

#dupObject


94
95
96
97
98
99
# File 'lib/action_controller/parameters.rb', line 94

def dup
  self.class.new(self).tap do |duplicate|
    duplicate.default = default
    duplicate.instance_variable_set :@permitted, @permitted
  end
end

#fetch(key, *args) ⇒ Object


82
83
84
85
86
# File 'lib/action_controller/parameters.rb', line 82

def fetch(key, *args)
  convert_hashes_to_parameters(key, super)
rescue KeyError, IndexError
  raise ActionController::ParameterMissing.new(key)
end

#permit(*filters) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/action_controller/parameters.rb', line 62

def permit(*filters)
  params = self.class.new
  filters.each do |filter|
    rule = filter.is_a?(Hash) ? filter : default_rule(filter)
    if rule.values.one? && rule.values.first.is_a?(Class)
      permitted_scalar_filter(params, rule.keys.first, rule.values.first)
    else
      apply_filter(params, rule)
    end
  end

  unpermitted_parameters!(params) if self.class.action_on_unpermitted_parameters

  params.permit!
end

#permit!Object


46
47
48
49
50
51
52
53
54
# File 'lib/action_controller/parameters.rb', line 46

def permit!
  each_pair do |key, value|
    convert_hashes_to_parameters(key, value)
    self[key].permit! if self[key].respond_to? :permit!
  end

  @permitted = true
  self
end

#require(key) ⇒ Object Also known as: required


56
57
58
# File 'lib/action_controller/parameters.rb', line 56

def require(key)
  self[key].presence || raise(ActionController::ParameterMissing.new(key))
end

#slice(*keys) ⇒ Object


88
89
90
91
92
# File 'lib/action_controller/parameters.rb', line 88

def slice(*keys)
  self.class.new(super).tap do |new_instance|
    new_instance.instance_variable_set :@permitted, @permitted
  end
end