Class: Upfluence::Mixin::StrongParameters::Parameters

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/upfluence/mixin/strong_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) ⇒ Parameters

Returns a new instance of Parameters.



32
33
34
35
# File 'lib/upfluence/mixin/strong_parameters.rb', line 32

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

Instance Attribute Details

#permittedObject Also known as: permitted?

Returns the value of attribute permitted.



23
24
25
# File 'lib/upfluence/mixin/strong_parameters.rb', line 23

def permitted
  @permitted
end

Instance Method Details

#[](key) ⇒ Object



78
79
80
# File 'lib/upfluence/mixin/strong_parameters.rb', line 78

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

#dupObject



94
95
96
97
98
99
# File 'lib/upfluence/mixin/strong_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/upfluence/mixin/strong_parameters.rb', line 82

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

#permit(*filters) ⇒ Object



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

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

  filters.flatten.each do |filter|
    case filter
    when Symbol, String
      permitted_scalar_filter(params, filter)
    when Hash then
      hash_filter(params, filter)
    end
  end

  unpermitted_parameters!(params) if self.class.action_on_unpermitted_parameters

  params.permit!
end

#permit!Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/upfluence/mixin/strong_parameters.rb', line 37

def permit!
  each_pair do |key, value|
    value = convert_hashes_to_parameters(key, value)
    Array.wrap(value).each do |_|
      _.permit! if _.respond_to? :permit!
    end
  end

  @permitted = true
  self
end

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



49
50
51
52
53
54
55
56
57
# File 'lib/upfluence/mixin/strong_parameters.rb', line 49

def require(key)
  return key.map { |k| require(k) } if key.is_a?(Array)
  value = self[key]
  if value.present? || value == false
    value
  else
    raise ParameterMissing.new(key)
  end
end

#slice(*keys) ⇒ Object



88
89
90
91
92
# File 'lib/upfluence/mixin/strong_parameters.rb', line 88

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