Module: CanCanCanJs::Export

Defined in:
lib/cancancan_js/export.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
# File 'lib/cancancan_js/export.rb', line 4

def self.included(base)
  # base is our target class. Invoke `extend` on it and pass nested module with class methods.
  base.extend ClassMethods
end

Instance Method Details

#exportObject



9
10
11
# File 'lib/cancancan_js/export.rb', line 9

def export
  {class_abilities: front_end_permissions[:can], object_rules: export_rules}
end

#export_rulesObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cancancan_js/export.rb', line 36

def export_rules
  new_list = {}
  usable_rules_list = front_end_rules
  if CanCanCanJs.configuration.export_all_back_end_rules
    usable_rules_list = rules
  end
  usable_rules_list.each do |rule|
    # init subjects if necessary
    rule.subjects.each do |subject|
      # subject_key is Class name as sym.
      subject_key = subject.is_a?(Symbol) ? subject : subject.name.to_sym
      new_list[subject_key] ||= {}
      # init actions
      rule.actions.each do |action|
        # Change must match at least one conditional group in order to auth the action, or if condition_groups are nil
        new_list[subject_key][action] ||= {condition_groups: nil}

        new_list[subject_key][action][:condition_groups] ||= []
        if rule.conditions.present?
          new_list[subject_key][action][:condition_groups] << rule.conditions
        else
          # if no conditions, then the action SHOULD be allowed!
          # - without this line, can-lines without conditions would not be exportable to the front-end
          new_list[subject_key][action][:condition_groups] << nil
        end
        if action == :update || action == :create
          klass = subject_key.to_s.constantize
          new_list[subject_key][action][:whitelist_attribs] = permitted_attributes(action, klass)
        end
      end
    end
  end
  return new_list
end

#front_end(&block) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/cancancan_js/export.rb', line 13

def front_end &block
  CanCanCanJs.configuration.start_block_front_end_rules = true
  begin
    block.call
  ensure
    CanCanCanJs.configuration.start_block_front_end_rules = false
  end
end

#front_end_permissionsObject

replicating Ability#permissions method, but for front-end export



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cancancan_js/export.rb', line 23

def front_end_permissions
  permissions_list = {
    can: Hash.new { |actions, k1| actions[k1] = Hash.new { |subjects, k2| subjects[k2] = [] } },
    cannot: Hash.new { |actions, k1| actions[k1] = Hash.new { |subjects, k2| subjects[k2] = [] } }
  }
  usable_rules_list = front_end_rules
  if CanCanCanJs.configuration.export_all_back_end_rules
    usable_rules_list = rules
  end
  usable_rules_list.each { |rule| extract_rule_in_permissions(permissions_list, rule) }
  permissions_list
end