Module: TrustedKeys::Trustable

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::MassAssignmentSecurity
Defined in:
lib/trusted_keys/trustable.rb

Instance Method Summary collapse

Instance Method Details

#<=>(other) ⇒ Object



50
# File 'lib/trusted_keys/trustable.rb', line 50

def <=> (other); level <=> other.level; end

#attributes(params) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/trusted_keys/trustable.rb', line 23

def attributes(params)
  params = params[key.to_sym] || params[key.to_s] if key

  if nested?(params)
    {}.tap do |hash|
      params.each do |key, nested_hash|
        hash[key] = sanitize(nested_hash)
      end
    end
  elsif params.is_a?(Array)
    [].tap do |array|
      params.each do |hash|
        array << sanitize(hash)
      end
    end
  else
    sanitize(params)
  end
end

#initialize(options) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/trusted_keys/trustable.rb', line 8

def initialize(options)
  @scope = options.fetch(:scope)
  @trusted_keys = options.fetch(:trusted_keys)
  @untrusted = options.fetch(:untrusted)
  @nested = options.fetch(:nested, true)
  keys = options.fetch(:keys)

  if nested?({})
    keys << "_destroy"
    keys << "id"
  end

  self.class.send("attr_accessible", *keys)
end

#keyObject



49
# File 'lib/trusted_keys/trustable.rb', line 49

def key; @key ||= @scope.last; end

#levelObject



51
# File 'lib/trusted_keys/trustable.rb', line 51

def level; @scope.size; end

#on_scope(attributes) ⇒ Object



43
44
45
46
47
# File 'lib/trusted_keys/trustable.rb', line 43

def on_scope(attributes)
  @scope.slice(1, @scope.size - 2).reduce(attributes) do |attributes, key|
    attributes[key]
  end
end

#parent_nested?(params) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
# File 'lib/trusted_keys/trustable.rb', line 53

def parent_nested?(params)
  if level > 1
    parent and parent.nested?(params)
  else
    false
  end
end