Class: ActionController::Parameters

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ Parameters

Returns a new instance of Parameters.



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

def initialize(attributes = nil)
  super(attributes)
  @permitted = false
  @strict = false
end

Instance Attribute Details

#permittedObject Also known as: permitted?

Returns the value of attribute permitted.



27
28
29
# File 'lib/action_controller/parameters.rb', line 27

def permitted
  @permitted
end

Instance Method Details

#[](key) ⇒ Object



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

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

#chck_parameters(*filters) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/action_controller/strict_parameters.rb', line 53

def chck_parameters(*filters)
  param_keys = filters.map do |filter|
    filter.is_a?(Hash) ? filter.keys : filter
  end.flatten.map(&:to_s)

  (self.keys - param_keys).tap do |diff|
    unless diff.empty?
      raise(ActionController::ParameterForbidden.new(diff))
    end
  end
end

#check_parameters(*filters) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/action_controller/parameters.rb', line 52

def check_parameters(*filters)
   param_keys = filters.map do |filter|
     filter.is_a?(Hash) ? filter.keys : filter
   end.flatten.map(&:to_s)

   (self.keys - param_keys).tap do |diff|
     raise(ActionController::ParameterForbidden.new(diff)) unless diff.empty?
   end
end

#dupObject



110
111
112
113
114
# File 'lib/action_controller/parameters.rb', line 110

def dup
  super.tap do |duplicate|
    duplicate.instance_variable_set :@permitted, @permitted
  end
end

#fetch(key, *args) ⇒ Object



100
101
102
103
104
# File 'lib/action_controller/parameters.rb', line 100

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

#permit(*filters) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/action_controller/parameters.rb', line 62

def permit(*filters)
  params = self.class.new

  if @strict || @@strict_config
    check_parameters(*filters)
  end

  filters.each do |filter|
    case filter
    when Symbol, String then
      params[filter] = self[filter] if has_key?(filter)
    when Hash then
      self.slice(*filter.keys).each do |key, value|
        return unless value

        key = key.to_sym

        params[key] = each_element(value) do |value|
          # filters are a Hash, so we expect value to be a Hash too
          next if filter.is_a?(Hash) && !value.is_a?(Hash)

          value = self.class.new(value) if !value.respond_to?(:permit)

          value.strict! if @strict

          value.permit(*Array.wrap(filter[key]))
        end
      end
    end
  end

  params.permit!
end

#permit!Object



36
37
38
39
# File 'lib/action_controller/parameters.rb', line 36

def permit!
  @permitted = true
  self
end

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



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

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

#slice(*keys) ⇒ Object



106
107
108
# File 'lib/action_controller/parameters.rb', line 106

def slice(*keys)
  self.class.new(super)
end

#strict!Object



47
48
49
50
# File 'lib/action_controller/parameters.rb', line 47

def strict!
  @strict = true
  self
end