Module: RailsAdminNext::Config

Defined in:
lib/rails_admin_next/config.rb,
lib/rails_admin_next/config/model.rb,
lib/rails_admin_next/config/fields.rb,
lib/rails_admin_next/config/actions.rb,
lib/rails_admin_next/config/hideable.rb,
lib/rails_admin_next/config/sections.rb,
lib/rails_admin_next/config/groupable.rb,
lib/rails_admin_next/config/proxyable.rb,
lib/rails_admin_next/config/has_fields.rb,
lib/rails_admin_next/config/has_groups.rb,
lib/rails_admin_next/config/lazy_model.rb,
lib/rails_admin_next/config/actions/new.rb,
lib/rails_admin_next/config/fields/base.rb,
lib/rails_admin_next/config/inspectable.rb,
lib/rails_admin_next/config/actions/base.rb,
lib/rails_admin_next/config/actions/edit.rb,
lib/rails_admin_next/config/actions/show.rb,
lib/rails_admin_next/config/configurable.rb,
lib/rails_admin_next/config/fields/group.rb,
lib/rails_admin_next/config/fields/types.rb,
lib/rails_admin_next/config/actions/index.rb,
lib/rails_admin_next/config/sections/base.rb,
lib/rails_admin_next/config/sections/edit.rb,
lib/rails_admin_next/config/sections/list.rb,
lib/rails_admin_next/config/sections/show.rb,
lib/rails_admin_next/config/actions/delete.rb,
lib/rails_admin_next/config/actions/export.rb,
lib/rails_admin_next/config/sections/modal.rb,
lib/rails_admin_next/config/has_description.rb,
lib/rails_admin_next/config/proxyable/proxy.rb,
lib/rails_admin_next/config/sections/create.rb,
lib/rails_admin_next/config/sections/export.rb,
lib/rails_admin_next/config/sections/nested.rb,
lib/rails_admin_next/config/sections/update.rb,
lib/rails_admin_next/config/actions/dashboard.rb,
lib/rails_admin_next/config/fields/types/date.rb,
lib/rails_admin_next/config/fields/types/enum.rb,
lib/rails_admin_next/config/fields/types/inet.rb,
lib/rails_admin_next/config/fields/types/json.rb,
lib/rails_admin_next/config/fields/types/text.rb,
lib/rails_admin_next/config/fields/types/time.rb,
lib/rails_admin_next/config/fields/types/uuid.rb,
lib/rails_admin_next/config/fields/association.rb,
lib/rails_admin_next/config/fields/types/color.rb,
lib/rails_admin_next/config/fields/types/float.rb,
lib/rails_admin_next/config/actions/bulk_delete.rb,
lib/rails_admin_next/config/actions/show_in_app.rb,
lib/rails_admin_next/config/fields/types/citext.rb,
lib/rails_admin_next/config/fields/types/hidden.rb,
lib/rails_admin_next/config/fields/types/string.rb,
lib/rails_admin_next/config/actions/history_show.rb,
lib/rails_admin_next/config/fields/types/boolean.rb,
lib/rails_admin_next/config/fields/types/decimal.rb,
lib/rails_admin_next/config/fields/types/integer.rb,
lib/rails_admin_next/config/fields/types/numeric.rb,
lib/rails_admin_next/config/actions/history_index.rb,
lib/rails_admin_next/config/const_load_suppressor.rb,
lib/rails_admin_next/config/fields/types/datetime.rb,
lib/rails_admin_next/config/fields/types/password.rb,
lib/rails_admin_next/config/fields/types/timestamp.rb,
lib/rails_admin_next/config/fields/types/serialized.rb,
lib/rails_admin_next/config/fields/types/action_text.rb,
lib/rails_admin_next/config/fields/types/file_upload.rb,
lib/rails_admin_next/config/fields/types/string_like.rb,
lib/rails_admin_next/config/fields/singular_association.rb,
lib/rails_admin_next/config/fields/types/active_storage.rb,
lib/rails_admin_next/config/fields/collection_association.rb,
lib/rails_admin_next/config/fields/types/active_record_enum.rb,
lib/rails_admin_next/config/fields/types/has_one_association.rb,
lib/rails_admin_next/config/fields/types/has_many_association.rb,
lib/rails_admin_next/config/fields/types/multiple_file_upload.rb,
lib/rails_admin_next/config/fields/types/belongs_to_association.rb,
lib/rails_admin_next/config/fields/types/multiple_active_storage.rb,
lib/rails_admin_next/config/fields/types/polymorphic_association.rb,
lib/rails_admin_next/config/fields/types/has_and_belongs_to_many_association.rb

Defined Under Namespace

Modules: Actions, Configurable, ConstLoadSuppressor, Fields, Groupable, HasDescription, HasFields, HasGroups, Hideable, Inspectable, Proxyable, Sections Classes: LazyModel, Model

Constant Summary collapse

DEFAULT_AUTHENTICATION =

RailsAdminNext authentication is fully pluggable — set authenticate_with to any proc. Works with Devise, Rodauth, authentication-zero, has_secure_password, or any Warden setup.

proc {}
DEFAULT_AUTHORIZE =
proc {}
DEFAULT_AUDIT =
proc {}
DEFAULT_CURRENT_USER =
proc {}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.browser_validationsObject

Tell browsers whether to use the native HTML5 validations (novalidate form option).



54
55
56
# File 'lib/rails_admin_next/config.rb', line 54

def browser_validations
  @browser_validations
end

.color_schemeObject

Color scheme of the admin UI: :auto (default) follows the OS preference, :light or :dark pins it. Rendered as the layout's , which every light-dark() token pair in the engine stylesheets resolves against.



59
60
61
# File 'lib/rails_admin_next/config.rb', line 59

def color_scheme
  @color_scheme
end

.compact_show_viewObject

hide blank fields in show view if true



51
52
53
# File 'lib/rails_admin_next/config.rb', line 51

def compact_show_view
  @compact_show_view
end

.composite_keys_serializerObject

For customization of composite keys representation



81
82
83
# File 'lib/rails_admin_next/config.rb', line 81

def composite_keys_serializer
  @composite_keys_serializer
end

.content_security_policy_report_onlyObject (readonly)

Whether the opt-in CSP is sent as report-only (set via #content_security_policy).



145
146
147
# File 'lib/rails_admin_next/config.rb', line 145

def content_security_policy_report_only
  @content_security_policy_report_only
end

.default_associated_collection_limitObject

Default association limit



42
43
44
# File 'lib/rails_admin_next/config.rb', line 42

def default_associated_collection_limit
  @default_associated_collection_limit
end

.default_hidden_fieldsObject

Fields to be hidden in show, create and update views



35
36
37
# File 'lib/rails_admin_next/config.rb', line 35

def default_hidden_fields
  @default_hidden_fields
end

.default_items_per_pageObject

Default items per page value used if a model level option has not been configured



39
40
41
# File 'lib/rails_admin_next/config.rb', line 39

def default_items_per_page
  @default_items_per_page
end

.default_search_operatorObject

Returns the value of attribute default_search_operator.



44
45
46
# File 'lib/rails_admin_next/config.rb', line 44

def default_search_operator
  @default_search_operator
end

.excluded_modelsObject

Configuration option to specify which models you want to exclude.



25
26
27
# File 'lib/rails_admin_next/config.rb', line 25

def excluded_models
  @excluded_models
end

.included_modelsObject

Configuration option to specify a allowlist of models you want to RailsAdminNext to work with. The excluded_models list applies against the allowlist as well and further reduces the models RailsAdminNext will use. If included_models is left empty ([]), then RailsAdminNext will automatically use all the models in your application (less any excluded_models you may have specified).



32
33
34
# File 'lib/rails_admin_next/config.rb', line 32

def included_models
  @included_models
end

.label_methodsObject

Configuration option to specify which method names will be searched for to be used as a label for object records. This defaults to [:name, :title]



48
49
50
# File 'lib/rails_admin_next/config.rb', line 48

def label_methods
  @label_methods
end

Bootstrap CSS classes used for Navigation bar



71
72
73
# File 'lib/rails_admin_next/config.rb', line 71

def navbar_css_classes
  @navbar_css_classes
end

Returns the value of attribute navigation_static_label.



78
79
80
# File 'lib/rails_admin_next/config.rb', line 78

def navigation_static_label
  @navigation_static_label
end

accepts a hash of static links to be shown below the main navigation



77
78
79
# File 'lib/rails_admin_next/config.rb', line 77

def navigation_static_links
  @navigation_static_links
end

.parent_controllerObject

set parent controller



62
63
64
# File 'lib/rails_admin_next/config.rb', line 62

def parent_controller
  @parent_controller
end

.registryObject (readonly)

Stores model configuration objects in a hash identified by model's class name.



68
69
70
# File 'lib/rails_admin_next/config.rb', line 68

def registry
  @registry
end

.show_gravatarObject

show Gravatar in Navigation bar



74
75
76
# File 'lib/rails_admin_next/config.rb', line 74

def show_gravatar
  @show_gravatar
end

Class Method Details

.actions(&block) ⇒ Object

Setup actions to be used.



328
329
330
331
332
333
# File 'lib/rails_admin_next/config.rb', line 328

def actions(&block)
  return unless block

  RailsAdminNext::Config::Actions.reset
  RailsAdminNext::Config::Actions.instance_eval(&block)
end

.asset_sourceObject

The engine ships a single zero-build asset path: browser-native ESM + CSS served by Propshaft and pinned via an engine-owned importmap. The option is vestigial — assignment is deprecated and ignored, kept only so a host's legacy config.asset_source = :sprockets doesn't raise.



288
289
290
# File 'lib/rails_admin_next/config.rb', line 288

def asset_source
  :importmap
end

.asset_source=(_) ⇒ Object



292
293
294
# File 'lib/rails_admin_next/config.rb', line 292

def asset_source=(_)
  RailsAdminNext.deprecator.warn("The asset_source configuration option was removed, RailsAdminNext serves a single importmap + Propshaft asset pipeline now.")
end

.audit_with(*args, &block) ⇒ Object

Setup auditing/versioning provider that observe objects lifecycle



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rails_admin_next/config.rb', line 148

def audit_with(*args, &block)
  extension = args.shift
  if extension
    klass = RailsAdminNext::AUDITING_ADAPTERS[extension]
    klass.setup if klass.respond_to? :setup
    @audit = proc do
      @auditing_adapter = klass.new(*([self] + args).compact, &block)
    end
  elsif block
    @audit = block
  end
  @audit || DEFAULT_AUDIT
end

.authenticate_with(&blk) ⇒ Object

Setup authentication to be run as a before filter This is run inside the controller instance so you can setup any authentication you need to

By default, the authentication will run via warden if available and will run the default.

Works with any auth: Devise, Rodauth, authentication-zero, has_secure_password.

Examples:

Warden (any setup)

RailsAdminNext.config do |config|
  config.authenticate_with do
    warden.authenticate! scope: :user
  end
end

Custom scope

RailsAdminNext.config do |config|
  config.authenticate_with do
    warden.authenticate! scope: :paranoid
  end
end

See Also:



106
107
108
109
# File 'lib/rails_admin_next/config.rb', line 106

def authenticate_with(&blk)
  @authenticate = blk if blk
  @authenticate || DEFAULT_AUTHENTICATION
end

.authorize_with(*args, &block) ⇒ Object

Setup authorization to be run as a before filter This is run inside the controller instance so you can setup any authorization you need to.

By default, there is no authorization.

To use an authorization adapter, pass the name of the adapter. For example, to use with CanCanCan, pass it like this.

See the wiki for more on authorization.

Examples:

Custom

RailsAdminNext.config do |config|
  config.authorize_with do
    redirect_to root_path unless warden.user.is_admin?
  end
end

CanCanCan

RailsAdminNext.config do |config|
  config.authorize_with :cancancan
end

See Also:



185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rails_admin_next/config.rb', line 185

def authorize_with(*args, &block)
  extension = args.shift
  if extension
    klass = RailsAdminNext::AUTHORIZATION_ADAPTERS[extension]
    klass.setup if klass.respond_to? :setup
    @authorize = proc do
      @authorization_adapter = klass.new(*([self] + args).compact, &block)
    end
  elsif block
    @authorize = block
  end
  @authorize || DEFAULT_AUTHORIZE
end

.configure_with(extension) {|configuration| ... } ⇒ Object

Setup configuration using an extension-provided ConfigurationAdapter

Examples:

Custom configuration for role-based setup.

RailsAdminNext.config do |config|
  config.configure_with(:custom) do |config|
    config.models = ['User', 'Comment']
    config.roles  = {
      'Admin' => :all,
      'User'  => ['User']
    }
  end
end

Yields:

  • (configuration)


211
212
213
214
# File 'lib/rails_admin_next/config.rb', line 211

def configure_with(extension)
  configuration = RailsAdminNext::CONFIGURATION_ADAPTERS[extension].new
  yield(configuration) if block_given?
end

.content_security_policy(report_only: false, &block) ⇒ Object

Content Security Policy for the admin. Opt-in: the engine enforces no policy by default.

Pass a block receiving an ActionDispatch::ContentSecurityPolicy; it is applied per-request to admin responses only. The engine threads a per-request nonce onto every inline tag it emits (the importmap JSON, the import entry point, the index column-width style), so a policy may use :self + nonces without breaking the admin's own modules. Pass report_only: true to send Content-Security-Policy-Report-Only instead of enforcing.

NOTE: the admin still renders a few inline style="…" attributes (enumerated in docs/security.md), so a style-src directive currently needs :unsafe_inline.

Examples:

Enforce a locked-down policy for the admin

RailsAdminNext.config do |config|
  config.content_security_policy do |policy|
    policy.default_src :self
    policy.script_src  :self
    policy.style_src   :self, :unsafe_inline
    policy.img_src     :self, :data
  end
end

Report-only while tuning it

RailsAdminNext.config do |config|
  config.content_security_policy(report_only: true) { |policy| policy.default_src :self }
end


136
137
138
139
140
141
142
# File 'lib/rails_admin_next/config.rb', line 136

def content_security_policy(report_only: false, &block)
  if block
    @content_security_policy = block
    @content_security_policy_report_only = report_only
  end
  @content_security_policy
end

.current_user_method(&block) ⇒ Object

Setup a different method to determine the current user or admin logged in. This is run inside the controller instance and made available as a helper.

By default, request.env.user or current_user will be used (auth-library agnostic).

Examples:

Custom

RailsAdminNext.config do |config|
  config.current_user_method do
    current_admin
  end
end

See Also:



229
230
231
232
# File 'lib/rails_admin_next/config.rb', line 229

def current_user_method(&block)
  @current_user = block if block
  @current_user || DEFAULT_CURRENT_USER
end

.model(entity, &block) ⇒ Object

Loads a model configuration instance from the registry or registers a new one if one is yet to be added.

First argument can be an instance of requested model, its class object, its class name as a string or symbol or a RailsAdminNext::AbstractModel instance.

If a block is given it is evaluated in the context of configuration instance.

Returns given model's configuration

See Also:



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/rails_admin_next/config.rb', line 267

def model(entity, &block)
  key =
    case entity
    when RailsAdminNext::AbstractModel
      entity.model.try(:name).try :to_sym
    when Class, ConstLoadSuppressor::ConstProxy
      entity.name.to_sym
    when String, Symbol
      entity.to_sym
    else
      entity.class.name.to_sym
    end

  @registry[key] ||= RailsAdminNext::Config::LazyModel.new(key.to_s)
  @registry[key].add_deferred_block(&block) if block
  @registry[key]
end

.modelsObject

Returns all model configurations

See Also:



338
339
340
# File 'lib/rails_admin_next/config.rb', line 338

def models
  RailsAdminNext::AbstractModel.all.collect { |m| model(m) }
end

.models_poolObject

pool of all found model names from the whole application



251
252
253
# File 'lib/rails_admin_next/config.rb', line 251

def models_pool
  (viable_models - excluded_models.collect(&:to_s)).uniq.sort
end

.reload!Object

Perform reset, then load RailsAdminNext initializer again



385
386
387
388
# File 'lib/rails_admin_next/config.rb', line 385

def reload!
  reset
  load RailsAdminNext::Engine.config.initializer_path
end

.resetObject

Reset all configurations to defaults.

See Also:



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/rails_admin_next/config.rb', line 345

def reset
  @compact_show_view = true
  @browser_validations = true
  @color_scheme = :auto
  @authenticate = nil
  @authorize = nil
  @audit = nil
  @current_user = nil
  @default_hidden_fields = {}
  @default_hidden_fields[:base] = [:_type]
  @default_hidden_fields[:edit] = %i[id _id created_at created_on deleted_at updated_at updated_on deleted_on]
  @default_hidden_fields[:show] = %i[id _id created_at created_on deleted_at updated_at updated_on deleted_on]
  @default_items_per_page = 20
  @default_associated_collection_limit = 100
  @default_search_operator = "default"
  @excluded_models = []
  @included_models = []
  @label_methods = %i[name title]
  @registry = {}
  @navbar_css_classes = %w[navbar-dark bg-primary border-bottom]
  @show_gravatar = true
  @navigation_static_links = {}
  @navigation_static_label = nil
  @composite_keys_serializer = RailsAdminNext::Support::CompositeKeysSerializer
  @parent_controller = "::ActionController::Base"
  @content_security_policy = nil
  @content_security_policy_report_only = false
  RailsAdminNext::Config::Actions.reset
  RailsAdminNext::AbstractModel.reset
end

.reset_model(model) ⇒ Object

Reset a provided model's configuration.

See Also:



379
380
381
382
# File 'lib/rails_admin_next/config.rb', line 379

def reset_model(model)
  key = model.is_a?(Class) ? model.name.to_sym : model.to_sym
  @registry.delete(key)
end

.sidescroll=(_) ⇒ Object



323
324
325
# File 'lib/rails_admin_next/config.rb', line 323

def sidescroll=(_)
  RailsAdminNext.deprecator.warn("The sidescroll configuration option was removed, it is always enabled now.")
end

.total_columns_width=(_) ⇒ Object



319
320
321
# File 'lib/rails_admin_next/config.rb', line 319

def total_columns_width=(_)
  RailsAdminNext.deprecator.warn("The total_columns_width configuration option is deprecated and has no effect.")
end

.visible_models(bindings) ⇒ Object

Get all models that are configured as visible sorted by their weight and label.

See Also:



393
394
395
396
397
398
399
400
401
# File 'lib/rails_admin_next/config.rb', line 393

def visible_models(bindings)
  visible_models_with_bindings(bindings).sort do |a, b|
    if (weight_order = a.weight <=> b.weight) == 0
      a.label.casecmp(b.label)
    else
      weight_order
    end
  end
end