module Seven::Abilities::ClassMethods

Attributes

rule_procs[R]

Public Instance Methods

abilities(options = nil, &rule_proc) click to toggle source

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
# 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) click to toggle source
# 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