Sha256: a5b49adf3317035554aa42fb66ea77e551bdc53afc38b24c907ebb8c4ea8d053

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Permit
  class Base 
    attr_reader :ability
       
    def initialize(ability)
      @ability = ability
    end

    def permit?(user, request=nil) 
      false
    end

    def can(action, subject, conditions = nil, &block)
      can_definitions << CanCan::CanDefinition.new(true, action, subject, conditions, block)
    end
        
    def cannot(action, subject, conditions = nil, &block)
      can_definitions << CanCan::CanDefinition.new(false, action, subject, conditions, block)
    end
    
    def owns(user, clazz, ownership_relation = :user_id, user_id_attribute = :id)
      begin
        user_id = user.send :"#{user_id_attribute}"              
      rescue
        raise ArgumentError, "ERROR (owns) - The user of class #{user.class} does not respond to ##{user_id_attribute}"
      end
        can :manage, clazz, ownership_relation => user_id
    end 

    protected

    def localhost_manager?
      Permits::Configuration.localhost_manager
    end

    def role_match? user
      user.has_role? self.class.last_name.downcase.to_sym
    end
      
    def can_definitions
      ability.send :can_definitions
    end    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cancan-permits-0.1.0 lib/cancan-permits/permit/base_permit.rb