module Protector::CanCan::Ability

Public Class Methods

included(mod) click to toggle source
# File lib/protector/cancan/ability.rb, line 20
def self.included(mod)
  mod.class_eval do

    def can_with_protector?(action, entity_set, *extra_args)
      if entity_set.is_a? Hash
        entity = entity_set.values.first
      else
        entity = entity_set
      end

      if entity.respond_to?(:restrict!) && @protector_subject_defined
        @protector_models ||= Set.new

        model = entity
        model = model.class unless model.is_a?(Class)

        unless @protector_models.include?(model)
          meta = entity.is_a?(Class) ? entity.protector_meta.evaluate(@protector_subject)
                                     : entity.protector_meta(@protector_subject)

          meta.access.each do |action, fields|
            action = :read if action == :view # *doh*
            can action, model unless fields.empty?
          end

          can :destroy, model if meta.destroyable?

          @protector_models << model
        end
      end

      can_without_protector? action, entity_set, *extra_args
    end

    alias_method_chain :can?, :protector
  end
end

Public Instance Methods

can_with_protector?(action, entity_set, *extra_args) click to toggle source
# File lib/protector/cancan/ability.rb, line 23
def can_with_protector?(action, entity_set, *extra_args)
  if entity_set.is_a? Hash
    entity = entity_set.values.first
  else
    entity = entity_set
  end

  if entity.respond_to?(:restrict!) && @protector_subject_defined
    @protector_models ||= Set.new

    model = entity
    model = model.class unless model.is_a?(Class)

    unless @protector_models.include?(model)
      meta = entity.is_a?(Class) ? entity.protector_meta.evaluate(@protector_subject)
                                 : entity.protector_meta(@protector_subject)

      meta.access.each do |action, fields|
        action = :read if action == :view # *doh*
        can action, model unless fields.empty?
      end

      can :destroy, model if meta.destroyable?

      @protector_models << model
    end
  end

  can_without_protector? action, entity_set, *extra_args
end
import_protector(subject) click to toggle source
# File lib/protector/cancan/ability.rb, line 7
def import_protector(subject)
  @protector_subject = subject
  @protector_subject_defined = true
end
protector_subject() click to toggle source
# File lib/protector/cancan/ability.rb, line 12
def protector_subject
  @protector_subject
end
protector_subject?() click to toggle source
# File lib/protector/cancan/ability.rb, line 16
def protector_subject?
  !!@protector_subject_defined
end