Module: SolidusAdmin::PermissionSetsHelper

Included in:
Roles::Edit::Component, Roles::New::Component
Defined in:
app/helpers/solidus_admin/permission_sets_helper.rb

Instance Method Summary collapse

Instance Method Details

#organize_permissions(permission_sets:, view_label:, edit_label:) ⇒ Object

Parameters:

  • permission_sets (Array<Spree::PermissionSet>)

    an array of PermissionSet objects to be organized into categories based on their names.

  • view_label (String)

    A string of your choice associated with “View” or “Display” level permissions. Used when rendering the checkbox.

  • edit_label (String)

    A string of your choice associated with “Edit” or “Management” level permissions. Used when rendering the checkbox.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/solidus_admin/permission_sets_helper.rb', line 12

def organize_permissions(permission_sets:, view_label:, edit_label:)
  return {} if permission_sets.blank?

  permission_sets.each_with_object({}) do |permission, grouped_permissions|
    group_key = permission.category.to_sym

    case permission.privilege
    when "display"
      grouped_permissions[group_key] ||= []
      grouped_permissions[group_key] << { label: view_label, id: permission.id }
    when "management"
      grouped_permissions[group_key] ||= []
      grouped_permissions[group_key] << { label: edit_label, id: permission.id }
    else
      grouped_permissions[:other] ||= []
      grouped_permissions[:other] << { label: permission.name, id: permission.id }
    end
  end
end