Module: Godmin::Authorization

Extended by:
ActiveSupport::Concern
Includes:
Pundit::Authorization
Defined in:
lib/godmin/authorization.rb,
lib/godmin/authorization/policy.rb

Defined Under Namespace

Classes: Policy

Instance Method Summary collapse

Instance Method Details

#already_namespaced?(subject) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/godmin/authorization.rb', line 50

def already_namespaced?(subject)
  subject.to_s.start_with?("#{engine_wrapper.namespace.name}::")
end

#find_class_name(subject) ⇒ Object

Borrowed from Pundit::PolicyFinder



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/godmin/authorization.rb', line 36

def find_class_name(subject)
  if subject.respond_to?(:model_name)
    subject.model_name
  elsif subject.class.respond_to?(:model_name)
    subject.class.model_name
  elsif subject.is_a?(Class)
    subject
  elsif subject.is_a?(Symbol)
    subject.to_s.camelize
  else
    subject.class
  end
end

#namespaced_record(record) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/godmin/authorization.rb', line 24

def namespaced_record(record)
  return record unless engine_wrapper.namespaced?

  class_name = find_class_name(record)
  if already_namespaced?(class_name)
    record
  else
    engine_wrapper.namespaced_path.map(&:to_sym) << record
  end
end

#policy(record) ⇒ Object



16
17
18
# File 'lib/godmin/authorization.rb', line 16

def policy(record)
  policies[record] ||= Pundit.policy!(pundit_user, namespaced_record(record))
end

#pundit_userObject



20
21
22
# File 'lib/godmin/authorization.rb', line 20

def pundit_user
  admin_user
end