Sha256: c0b6b941f9e17459b1df58b9fd8d9a98b8cab4207637e13b5466d6bcb1dde77b

Contents?: true

Size: 794 Bytes

Versions: 18

Compression:

Stored size: 794 Bytes

Contents

require 'cancan'

class WCC::Auth::TieredAbility
  include CanCan::Ability

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

  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

  def levels
    self.class.levels
  end

  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

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

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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
wcc-auth-0.9.0 lib/wcc/auth/ability.rb
wcc-auth-0.8.3 lib/wcc/auth/ability.rb
wcc-auth-0.8.2 lib/wcc/auth/ability.rb
wcc-auth-0.8.1 lib/wcc/auth/ability.rb
wcc-auth-0.8.0 lib/wcc/auth/ability.rb
wcc-auth-0.7.3 lib/wcc/auth/ability.rb
wcc-auth-0.7.2 lib/wcc/auth/ability.rb
wcc-auth-0.7.1 lib/wcc/auth/ability.rb
wcc-auth-0.7.0 lib/wcc/auth/ability.rb
wcc-auth-0.6.0 lib/wcc/auth/ability.rb
wcc-auth-0.5.0 lib/wcc/auth/ability.rb
wcc-auth-0.4.0 lib/wcc/auth/ability.rb
wcc-auth-0.3.5 lib/wcc/auth/ability.rb
wcc-auth-0.3.4 lib/wcc/auth/ability.rb
wcc-auth-0.3.3 lib/wcc/auth/ability.rb
wcc-auth-0.3.2 lib/wcc/auth/ability.rb
wcc-auth-0.3.1 lib/wcc/auth/ability.rb
wcc-auth-0.3.0 lib/wcc/auth/ability.rb