Module: Seven::Abilities::ClassMethods

Defined in:
lib/seven/abilities.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rule_procsObject (readonly)

Returns the value of attribute rule_procs.



50
51
52
# File 'lib/seven/abilities.rb', line 50

def rule_procs
  @rule_procs
end

Instance Method Details

#abilities(options = nil, &rule_proc) ⇒ Object

Params:

options:
  {check: :role, equal: 'admin'}  current_user.role == 'admin'
  {check: :role, in: %w{admin editor}}  %w{admin editor}.include?(current_user.role)
  {pass: :my_filter} call my_filter method
  {pass: Proc.new { ... }} proc.call


58
59
60
61
62
# File 'lib/seven/abilities.rb', line 58

def abilities(options = nil, &rule_proc)
  filter = build_seven_abilities_filter(options)
  @rule_procs ||= []
  @rule_procs << [filter, rule_proc]
end

#build_seven_abilities_filter(options) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/seven/abilities.rb', line 64

def build_seven_abilities_filter(options)
  return if options.nil?
  opts = options.symbolize_keys

  if val = opts[:pass]
    if val.is_a?(Proc)
      val
    else
      Proc.new { send val }
    end
  elsif attr = opts[:check]
    if list = opts[:in]
      Proc.new { list.include?(current_user.public_send(attr)) }
    elsif val = opts[:equal]
      Proc.new { current_user.public_send(attr) == val }
    else
      raise Seven::ArgsError, 'Invalid check definition'
    end
  else
    raise Seven::ArgsError, 'Invalid check definition'
  end
end