Module: Protector::CanCan::Ability

Defined in:
lib/protector/cancan/ability.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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

Instance Method Details

#import_protector(subject) ⇒ Object



7
8
9
10
# File 'lib/protector/cancan/ability.rb', line 7

def import_protector(subject)
  @protector_subject = subject
  @protector_subject_defined = true
end

#protector_subjectObject



12
13
14
# File 'lib/protector/cancan/ability.rb', line 12

def protector_subject
  @protector_subject
end

#protector_subject?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/protector/cancan/ability.rb', line 16

def protector_subject?
  !!@protector_subject_defined
end