Exception: TrustedKeys::Error::NotTrusted

Inherits:
StandardError
  • Object
show all
Defined in:
lib/trusted_keys/error/not_trusted.rb

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ NotTrusted

Returns a new instance of NotTrusted.



4
5
6
7
# File 'lib/trusted_keys/error/not_trusted.rb', line 4

def initialize(env)
  @keys = {}
  @production = not(env.development? or env.test?)
end

Instance Method Details

#keys(options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/trusted_keys/error/not_trusted.rb', line 18

def keys(options)
  scope = options.fetch(:scope)
  key = options.fetch(:key)
  keys = options.fetch(:keys).flatten.map do |key|
    ":#{key.to_s.sub(/\(\di\)/, '')}"
  end.uniq.join(', ')

  scope_key = (scope + [key]).compact.join('.')
  @keys[scope_key] = keys unless scope_key.empty?

  self
end

#messageObject



9
10
11
12
13
14
15
16
# File 'lib/trusted_keys/error/not_trusted.rb', line 9

def message
  usage = @keys.map do |scope, keys|
    "`trust #{keys}, for: '#{scope}'`"
  end.join("\n")

  "\n\nError: There are keys in the params hash that are not trusted. " +
  "Set them as trusted with:\n#{usage} at the top of the controller."
end

#present?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'lib/trusted_keys/error/not_trusted.rb', line 31

def present?
  if @production
    false
  else
    not @keys.empty?
  end
end