Class: ActionController::Parameters

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

Constant Summary collapse

REQUIRED_FLAGS =
[:required, :require]
PERMITTED_FLAGS =
[:permitted, :permit] + REQUIRED_FLAGS

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) ⇒ Parameters

Returns a new instance of Parameters.



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

def initialize(attributes = nil)
  super(attributes)
end

Instance Method Details

#[](key) ⇒ Object



67
68
69
# File 'lib/action_controller/parameters.rb', line 67

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

#check_required(filter) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/action_controller/parameters.rb', line 90

def check_required(filter)
  if filter.kind_of? Hash
    filter.each do |key, value|
      flag_error_if_abscent(key) if required_flag?(value) or value.kind_of? Hash
      to_check[key].check_required(value) if value.respond_to? :check_required and to_check.has_key? key
    end
  end
  unless missing_required_fields.empty?
    raise ActionController::ParameterMissing.new("'#{missing_required_fields.join("', '")}' required by #{filter}")
  end
end

#dupObject



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

def dup
  self.class.new(self).tap do |duplicate|
    duplicate.default = default
    copy_instance_variables_to duplicate
  end
end

#fetch(key, *args) ⇒ Object



71
72
73
74
75
# File 'lib/action_controller/parameters.rb', line 71

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

#originalObject



63
64
65
# File 'lib/action_controller/parameters.rb', line 63

def original
  to_check
end

#permit(*filters) ⇒ Object



59
60
61
# File 'lib/action_controller/parameters.rb', line 59

def permit(*filters)
  strengthen(hash_from(filters, PERMITTED_FLAGS.first))
end

#permit!Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/action_controller/parameters.rb', line 30

def permit!
  replace to_check
  @strengthened = true
  each_pair do |key, value|
    convert_hashes_to_parameters(key, value)
    self[key].permit! if self[key].respond_to? :permit!
  end
  
  self
end

#require(key) ⇒ Object



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

def require(key)
  strengthen(key => REQUIRED_FLAGS.first)
  to_check[key].presence
end

#slice(*keys) ⇒ Object



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

def slice(*keys)
  self.class.new(super).tap do |new_instance|
    copy_instance_variables_to new_instance
  end
end

#strengthen(filter = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/action_controller/parameters.rb', line 41

def strengthen(filter = {})
  return unless filter.kind_of? Hash
  
  filter = filter.with_indifferent_access
  
  if keys_all_numbers?
    strengthen_numbered_hash_as_array(filter)
  else
    strengthen_hash(filter)
  end
   
end

#strengthened?Boolean Also known as: permitted?

Returns:

  • (Boolean)


20
21
22
# File 'lib/action_controller/parameters.rb', line 20

def strengthened?
  @strengthened
end