Class: WCC::Auth::TieredAbility

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability
Defined in:
lib/wcc/auth/ability.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ TieredAbility

Returns a new instance of TieredAbility.



6
7
8
9
# File 'lib/wcc/auth/ability.rb', line 6

def initialize(user)
  @user = user
  grant_access_at(@user.access_level)
end

Class Method Details

.at_level(level_key, &can_block) ⇒ Object



23
24
25
26
27
# File 'lib/wcc/auth/ability.rb', line 23

def self.at_level(level_key, &can_block)
  level = WCC::Auth::AccessLevel[level_key] or
    raise ArgumentError, "#{level_key} is not a valid access level"
  levels[level] << can_block
end

.inherited(subclass) ⇒ Object



33
34
35
# File 'lib/wcc/auth/ability.rb', line 33

def self.inherited(subclass)
  subclass.send :instance_variable_set, :@levels, levels.dup
end

.levelsObject



29
30
31
# File 'lib/wcc/auth/ability.rb', line 29

def self.levels
  @levels ||= Hash.new { |hash, key| hash[key] = [] }
end

Instance Method Details

#grant_access_at(access_level) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/wcc/auth/ability.rb', line 11

def grant_access_at(access_level)
  levels.each do |level, can_blocks|
    if access_level >= level
      can_blocks.each { |can_block| instance_exec(@user, &can_block) }
    end
  end
end

#levelsObject



19
20
21
# File 'lib/wcc/auth/ability.rb', line 19

def levels
  self.class.levels
end