Sha256: 849d0b56cb28625350d2159e84aa9269befa7ad19e72b754dfbe772aee1026ce

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

class Ability
  include CanCan::Ability
  attr_accessor :user

  def initialize(user)
    self.user = user||CanPlay::Config.user_class_name.constantize.new
    CanPlay::Config.super_roles.each do |role_name|
      can(:manage, :all) if user.role?(role_name)
    end
    CanPlay::Config.role_class_name.constantize.all.each do |role|
      next unless user.role?(role.name)
      role.send(CanPlay::Config.role_resources_relation_name).each do |role_resource|
        resource = CanPlay.find_by_name(role_resource.resource_name)
        next unless resource
        if resource[:type] == 'collection'
          if resource[:behavior]
            block = resource[:behavior]
            can(resource[:verb], resource[:object]) if block.call(user)
          else
            can resource[:verb], resource[:object]
          end
        elsif resource[:type] == 'member'
          if resource[:behavior]
            block = resource[:behavior]
            can resource[:verb], resource[:object] do |object|
              block.call(user, object)
            end
          else
            can resource[:verb], resource[:object]
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
can_play-0.2.4 lib/can_play/ability.rb