Module: EffectiveRolesHelper

Defined in:
app/helpers/effective_roles_helper.rb

Instance Method Summary collapse

Instance Method Details

#effective_roles_authorization_badge(level) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/helpers/effective_roles_helper.rb', line 65

def effective_roles_authorization_badge(level)
  label = defined?(EffectiveBootstrap) ? 'badge' : 'label'

  case level
  when :manage
    (:span, 'Full', class: "#{label} #{label}-primary")
  when :update
    (:span, 'Edit', class: "#{label} #{label}-success")
  when :update_own
    (:span, 'Edit Own', class: "#{label} #{label}-info")
  when :create
    (:span, 'Create', class: "#{label} #{label}-success")
  when :show
    (:span, 'Read only', class: "#{label} #{label}-warning")
  when :index
    (:span, 'Read only', class: "#{label} #{label}-warning")
  when :destroy
    (:span, 'Delete only', class: "#{label} #{label}-warning")
  when :none
    (:span, 'No Access', class: "#{label} #{label}-danger")
  when :yes
    (:span, 'Yes', class: "#{label} #{label}-primary")
  when :no
    (:span, 'No', class: "#{label} #{label}-danger")
  when :unknown
    (:span, 'Unknown', class: "#{label}")
  else
    (:span, level.to_s.titleize, class: "#{label} #{label}-info")
  end
end

#effective_roles_authorization_label(klass) ⇒ Object



96
97
98
99
100
101
102
# File 'app/helpers/effective_roles_helper.rb', line 96

def effective_roles_authorization_label(klass)
  # Custom permissions
  return "#{klass.keys.first} #{klass.values.first}" if klass.kind_of?(Hash) && klass.length == 1

  klass = klass.keys.first if klass.kind_of?(Hash)
  klass.respond_to?(:name) ? klass.name : klass.to_s
end

#effective_roles_summary(obj, options = {}) ⇒ Object

User or a Post, any acts_as_roleable



2
3
4
5
6
7
8
9
# File 'app/helpers/effective_roles_helper.rb', line 2

def effective_roles_summary(obj, options = {}) # User or a Post, any acts_as_roleable
  raise 'expected an acts_as_roleable object' unless obj.respond_to?(:roles)

  descriptions = EffectiveRoles.role_descriptions[obj.class.name] || EffectiveRoles.role_descriptions || {}
  opts = { obj: obj, roles: obj.roles, descriptions: descriptions }.merge(options)

  render partial: 'effective/roles/summary', locals: opts
end

#effective_roles_summary_table(opts = {}) ⇒ Object

effective_roles_summary_table(roles: [:admin, :superadmin], only: [Post, Event]) effective_roles_summary_table(except: [Post, User]) effective_roles_summary_table(aditionally: [Report::PostReport, User, :export])



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/effective_roles_helper.rb', line 16

def effective_roles_summary_table(opts = {})
  raise 'Expected argument to be a Hash' unless opts.kind_of?(Hash)

  roles = Array(opts[:roles]).presence
  roles ||= [:public, :signed_in] + EffectiveRoles.roles

  if opts[:only].present?
    klasses = Array(opts[:only])
    render partial: '/effective/roles/summary_table', locals: { klasses: klasses, roles: roles }
    return
  end

  # Figure out all klasses (ActiveRecord objects)
  Rails.application.eager_load!
  tables = ActiveRecord::Base.connection.tables - ['schema_migrations', 'delayed_jobs', 'active_storage_attachments']

  klasses = ActiveRecord::Base.descendants.select do |model|
    (model.respond_to?(:table_name) && tables.include?(model.table_name))
  end

  if opts[:except]
    klasses = klasses - Array(opts[:except])
  end

  if opts[:plus]
    klasses = klasses + Array(opts[:plus])
  end

  klasses = klasses.sort do |a, b|
    a = a.respond_to?(:name) ? a.name : a.to_s
    b = b.respond_to?(:name) ? b.name : b.to_s

    a_namespaces = a.split('::')
    b_namespaces = b.split('::')

    if a_namespaces.length != b_namespaces.length
      a_namespaces.length <=> b_namespaces.length
    else
      a <=> b
    end
  end

  if opts[:additionally]
    klasses = klasses + Array(opts[:additionally])
  end

  render partial: '/effective/roles/summary_table', locals: { klasses: klasses, roles: roles }
end