Class: CrudComponents::Admin::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/crud_components/admin/configuration.rb

Overview

What the mounted admin needs to know that the models don't say themselves.

CrudComponents::Admin.configure do |config|
config.auth_with { head :forbidden unless current_user&.admin? }
config.title = 'Bookstore admin'
end

Constant Summary collapse

DEFAULT_EXCLUDED_NAMESPACES =

Model name prefixes that are framework bookkeeping, not application data.

%w[
  ActiveRecord ActiveStorage ActionText ActionMailbox
  SolidQueue SolidCache SolidCable
  Delayed GoodJob Que Noticed PgSearch FriendlyId
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/crud_components/admin/configuration.rb', line 57

def initialize
  @title = nil
  @layout = 'crud_components/admin'
  @only = nil
  @except = []
  @groups = []
  @excluded_namespaces = DEFAULT_EXCLUDED_NAMESPACES.dup
  @counts = true
  @parent_controller = '::ApplicationController'
  @per_page = 50
  @auth_mode = :cancan
  @auth_subject = :crud_admin
  @auth_block = nil
end

Instance Attribute Details

#auth_blockObject (readonly)

The before_action body that decides who gets in, for auth_with { … }.



52
53
54
# File 'lib/crud_components/admin/configuration.rb', line 52

def auth_block
  @auth_block
end

#auth_modeObject (readonly)

How the admin decides who gets in: :cancan, :none, or :block when auth_with was given one. See #auth_with.



49
50
51
# File 'lib/crud_components/admin/configuration.rb', line 49

def auth_mode
  @auth_mode
end

#auth_subjectObject

What the :cancan gate asks about: can?(:access, auth_subject).



55
56
57
# File 'lib/crud_components/admin/configuration.rb', line 55

def auth_subject
  @auth_subject
end

#countsObject

Whether the dashboard runs a COUNT(*) per model.



38
39
40
# File 'lib/crud_components/admin/configuration.rb', line 38

def counts
  @counts
end

#exceptObject

Model names to drop from an otherwise automatic registry.



29
30
31
# File 'lib/crud_components/admin/configuration.rb', line 29

def except
  @except
end

#excluded_namespacesObject

Model name prefixes to skip during discovery.



35
36
37
# File 'lib/crud_components/admin/configuration.rb', line 35

def excluded_namespaces
  @excluded_namespaces
end

#groupsObject

Sidebar group order. Groups not listed follow, alphabetically.



32
33
34
# File 'lib/crud_components/admin/configuration.rb', line 32

def groups
  @groups
end

#layoutObject

The layout the engine renders in. 'crud_components/admin' is the bundled Bootstrap shell; name one of your own (e.g. 'application') instead.



22
23
24
# File 'lib/crud_components/admin/configuration.rb', line 22

def layout
  @layout
end

#onlyObject

nil = every discovered model. An Array of model names (String, Symbol or class) means exactly those, in that order, discovery filters bypassed.



26
27
28
# File 'lib/crud_components/admin/configuration.rb', line 26

def only
  @only
end

#parent_controllerObject

The controller the engine's own controllers inherit from — how the admin reaches your current_user, your session and your rescue_froms.



42
43
44
# File 'lib/crud_components/admin/configuration.rb', line 42

def parent_controller
  @parent_controller
end

#per_pageObject

Rows per index page, when a pagination gem is present.



45
46
47
# File 'lib/crud_components/admin/configuration.rb', line 45

def per_page
  @per_page
end

#titleObject

Sidebar brand line; defaults to the application's name.



18
19
20
# File 'lib/crud_components/admin/configuration.rb', line 18

def title
  @title
end

Instance Method Details

#auth_with(mode = nil, subject: nil, &block) ⇒ Object

Who gets in. Three forms:

config.auth_with :cancan                 # the default: `can :access, :crud_admin`
config.auth_with :cancan, subject: :backend
config.auth_with :none                   # no gate at all — a demo, a playground
config.auth_with { redirect_to main_app.root_path unless current_user&.admin? }

A block runs as a before_action in the engine's controller, in that controller's own context — current_user, redirect_to, head and your rescue_froms all work as usual.

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
# File 'lib/crud_components/admin/configuration.rb', line 88

def auth_with(mode = nil, subject: nil, &block)
  raise ArgumentError, 'auth_with takes a mode or a block, not both' if mode && block

  @auth_subject = subject if subject
  @auth_mode = block ? :block : normalized_mode(mode)
  @auth_block = block
end

#cancan_gate?Boolean

Returns:

  • (Boolean)


96
# File 'lib/crud_components/admin/configuration.rb', line 96

def cancan_gate? = @auth_mode == :cancan

#open_gate?Boolean

Returns:

  • (Boolean)


98
# File 'lib/crud_components/admin/configuration.rb', line 98

def open_gate? = @auth_mode == :none

#parent_controller_classObject

The resolved parent controller class, falling back to ActionController::Base when the named one does not exist.



74
75
76
# File 'lib/crud_components/admin/configuration.rb', line 74

def parent_controller_class
  @parent_controller.to_s.safe_constantize || ActionController::Base
end

#resolved_titleObject



100
101
102
# File 'lib/crud_components/admin/configuration.rb', line 100

def resolved_title
  @title || default_title
end