Class: Clerk::Account

Inherits:
ApplicationRecord show all
Defined in:
app/models/clerk/account.rb

Defined Under Namespace

Classes: RolesWrapper

Instance Method Summary collapse

Methods inherited from ApplicationRecord

clerk_table_name, clerk_table_name_nc, transaction

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/clerk/account.rb', line 51

def method_missing(method_name, *args, &block)
  # Rails lazy loads modules.  If an object hasn't been loaded yet, any inverse association
  # will not be here yet.  Just in case, load the constant here and re-call the method to
  # before raising an error
  @miss_test ||= {}
  if @miss_test.has_key? method_name.to_sym
    super
  else
    @miss_test[method_name.to_sym] = true
    scope_class = method_name.to_s.classify.constantize
    send(method_name, *args, &block)  
  end
rescue => e
  super
end

Instance Method Details

#add_clerk_role(role_type_symbol, instance = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/models/clerk/account.rb', line 8

def add_clerk_role(role_type_symbol, instance = nil)
  json = { }
  json[:name] = role_type_symbol.to_s
  json[:account_id] = self.id

  if not instance.nil?
    json[:scope_class] = instance.class.name
    json[:scope_id] = instance.id
  end

  server_url = "#{Clerk.accounts_url}/roles"
  HTTP.auth("Bearer #{Clerk.key}").post(server_url, :json => json)
end

#has_permission?(permission, scope) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'app/models/clerk/account.rb', line 30

def has_permission?(permission, scope)
  has_role?(scope.class.roles_with_permission(permission), scope)
end

#has_role?(role, scope) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'app/models/clerk/account.rb', line 22

def has_role?(role, scope)
  roles.where(name: role, scope_class: scope.class.name, scope_id: scope.id).exists?
end

#permissions_for(scope) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/clerk/account.rb', line 34

def permissions_for(scope)
  permissions = Set.new

  roles = roles_for(scope)
  roles.each do |role|
    role_permissions = scope.class.clerk_permissions_map[role]

    unless role_permissions.nil?
      permissions.merge(role_permissions)
    end
  end

  return permissions.to_a
end

#roles_for(scope) ⇒ Object



26
27
28
# File 'app/models/clerk/account.rb', line 26

def roles_for(scope)
  roles.where(scope_class: scope.class.name, scope_id: scope.id).pluck(:name).map(&:to_sym)
end