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: permissions[:can], object_rules: export_rules}
end

#export_rulesObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cancancan_js/export.rb', line 13

def export_rules
  new_list = {}
  rules.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