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)



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/clerk/account.rb', line 72

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



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/clerk/account.rb', line 29

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

#email(from_email_name:, email_template_token: nil, replacements: nil, subject: nil, body: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/clerk/account.rb', line 11

def email(
  from_email_name:,
  email_template_token: nil,
  replacements: nil,
  subject: nil,
  body: nil
)
  Clerk.email(
    account: self,
    from_email_name: from_email_name,
    email_template_token: email_template_token,
    replacements: replacements,
    subject: subject,
    body: body
  )
end

#has_permission?(permission, scope) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/models/clerk/account.rb', line 51

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

#has_role?(role, scope) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/clerk/account.rb', line 43

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

#permissions_for(scope) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/clerk/account.rb', line 55

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



47
48
49
# File 'app/models/clerk/account.rb', line 47

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