Module: Opal::Connect::ConnectPlugins::Abilities

Defined in:
lib/opal/connect/plugins/abilities.rb

Defined Under Namespace

Modules: InstanceMethods

Constant Summary collapse

ConnectJavascript =
-> do
  if current_user.respond_to?(:id) && current_user.id
    abilities = Opal::Connect.options[:abilities][:list][current_user.role]
    "$current_user_abilities = Base64.decode64('#{Base64.encode64 abilities.to_json}')"
  else
    "$current_user_abilities = {}"
  end
end

Class Method Summary collapse

Class Method Details

.configure(connect, options = false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/opal/connect/plugins/abilities.rb', line 15

def self.configure(connect, options = false)
  if options
    unless RUBY_ENGINE == 'opal'
      if file = options[:file]
        options[:list] = YAML.load_file file
      end
    end

    connect.options[:abilities] = options
  end
end

.get_object(obj) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/opal/connect/plugins/abilities.rb', line 64

def self.get_object(obj)
  if RUBY_ENGINE == 'opal'
    obj.to_sym
  else
    if obj[0,1] == obj[0,1].upcase
      Object.const_get(obj)
    else
      obj.to_sym
    end
  end
end

.load_dependencies(connect, *args) ⇒ Object



11
12
13
# File 'lib/opal/connect/plugins/abilities.rb', line 11

def self.load_dependencies(connect, *args)
  connect.plugin :current_user
end

.process(abilities, type, scope, user) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/opal/connect/plugins/abilities.rb', line 50

def self.process(abilities, type, scope, user)
  (abilities || []).each do |ability|
    obj = get_object(ability['object'])

    if method = ability['method']
      scope.send type, ability['action'].to_sym, obj do |record|
        scope.send(method, record)
      end
    else
      scope.send type, ability['action'].to_sym, obj
    end
  end
end