Class: Spree::Billing::RolesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/spree/billing/roles_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#current_vendor, #default_url_options, #edit_object_url, #handle_unauthorized_vendor, #page, #per_page, #required_vendor_user!, #set_locale, #switch_vendor, #vendors

Methods included from SpreeCmCommissioner::Billing::RoleAuthorization

#auth_action, #auth_entry, #auth_user, #authorize!, #authorize?, #authorize_admin, #authorize_role!, #redirect_unauthorized_access

Instance Method Details

#collectionObject



8
9
10
11
12
13
# File 'app/controllers/spree/billing/roles_controller.rb', line 8

def collection
  return @collection if defined?(@collection)

  @search = Spree::Role.ransack(params[:q])
  @collection = @search.result.page(params[:page]).per(15)
end

#collection_url(options = {}) ⇒ Object



23
24
25
# File 'app/controllers/spree/billing/roles_controller.rb', line 23

def collection_url(options = {})
  billing_roles_url(options)
end

#construct_role_permissionsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/spree/billing/roles_controller.rb', line 81

def construct_role_permissions
  role_permissions_attributes = {}

  permitted_resource_params[:role_permissions_attributes].each do |id, role_permission_attributes|
    selected = role_permission_attributes[:selected] == 'true'
    persisted = role_permission_attributes.to_h.key?(:id)
    permission_persisted = role_permission_attributes[:permission_attributes].key?(:id)
    role_permission_attributes = role_permission_attributes.except(:selected)

    if permission_persisted
      role_permission_attributes[:permission_id] = role_permission_attributes[:permission_attributes][:id]
      role_permission_attributes.delete(:permission_attributes)
    end

    if selected
      role_permissions_attributes[id] = role_permission_attributes
    elsif persisted
      role_permission_attributes[:_destroy] = 1
      role_permissions_attributes[id] = role_permission_attributes
    end
  end

  permitted_resource_params[:role_permissions_attributes] = role_permissions_attributes
end

#include_permission_hashesObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/spree/billing/roles_controller.rb', line 59

def include_permission_hashes
  @include_permission_hashes ||= {
    'spree/billing/reports' => %w[index paid balance_due overdue failed_orders active_subscribers print_all_invoices export],
    'spree/billing/orders' => %w[index create new edit show update destroy],
    'spree/billing/users' => %w[index create new edit show update destroy],
    'spree/billing/subscriptions' => %w[index create new edit show update destroy],
    'spree/billing/customers' => %w[index create new edit show update destroy apply_promotion delete_promotion],
    'spree/billing/roles' => %w[index create new edit show update destroy],
    'spree/billing/vendors' => %w[edit show update],
    'spree/billing/places' => %w[index create new edit show update destroy],
    'spree/billing/businesses' => %w[index create new edit show update destroy],
    'spree/billing/products' => %w[index create new edit show update destroy],
    'spree/billing/variants' => %w[index create new edit show update destroy],
    'spree/billing/option_types' => %w[index create new edit show update destroy],
    'spree/billing/adjustments' => %w[index create new edit show update destroy],
    'spree/billing/payments' => %w[index create new edit show update destroy],
    'spree/billing/invoice' => %w[index show print_invoice_date],
    'spree/billing/addresses' => %w[index create new edit show update destroy],
    'spree/billing/store_credits' => %w[index create new edit show update destroy]
  }
end

#load_roleObject



27
28
29
# File 'app/controllers/spree/billing/roles_controller.rb', line 27

def load_role
  @role = @object || Spree::Role.new
end

#load_role_permissionsObject



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
# File 'app/controllers/spree/billing/roles_controller.rb', line 31

def load_role_permissions
  @role_permissions = Spree::Core::Engine.routes.routes.filter_map do |route|
    if route.defaults.key?(:controller) && route.defaults.key?(:action)
      entry = route.defaults.fetch(:controller)
      action = route.defaults.fetch(:action)

      if include_permission_hashes[entry]&.include?(action)
        permission = SpreeCmCommissioner::Permission.where(
          entry: entry,
          action: action
        ).first_or_initialize

        if @role.persisted? && permission.persisted?
          SpreeCmCommissioner::RolePermission.where(
            role: @role,
            permission: permission
          ).first_or_initialize
        else
          SpreeCmCommissioner::RolePermission.new(
            role: @role,
            permission: permission
          )
        end
      end
    end
  end.uniq(&:slug)
end

#model_classObject



15
16
17
# File 'app/controllers/spree/billing/roles_controller.rb', line 15

def model_class
  Spree::Role
end

#object_nameObject



19
20
21
# File 'app/controllers/spree/billing/roles_controller.rb', line 19

def object_name
  'role'
end