module RailsAdmin::Config

Constants

DEFAULT_AUDIT
DEFAULT_AUTHENTICATION

RailsAdmin is setup to try and authenticate with warden If warden is found, then it will try to authenticate

This is valid for custom warden setups, and also devise If you’re using the admin setup for devise, you should set RailsAdmin to use the admin

@see RailsAdmin::Config.authenticate_with @see RailsAdmin::Config.authorize_with

DEFAULT_AUTHORIZE
DEFAULT_CURRENT_USER

Attributes

asset_source[W]

Set where RailsAdmin fetches JS/CSS from, defaults to :sprockets

browser_validations[RW]

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

compact_show_view[RW]

hide blank fields in show view if true

composite_keys_serializer[RW]

For customization of composite keys representation

default_associated_collection_limit[RW]

Default association limit

default_hidden_fields[R]

Fields to be hidden in show, create and update views

default_items_per_page[RW]

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

default_search_operator[R]
excluded_models[RW]

Configuration option to specify which models you want to exclude.

forgery_protection_settings[RW]

set settings for ‘protect_from_forgery` method By default, it raises exception upon invalid CSRF tokens

included_models[RW]

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

label_methods[RW]

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]

main_app_name[RW]

Application title, can be an array of two elements

navbar_css_classes[RW]

Bootstrap CSS classes used for Navigation bar

navigation_static_label[RW]
parent_controller[R]

set parent controller

registry[R]

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

@see RailsAdmin.config

show_gravatar[RW]

show Gravatar in Navigation bar

Public Class Methods

actions(&block) click to toggle source

Setup actions to be used.

# File lib/rails_admin/config.rb, line 295
def actions(&block)
  return unless block

  RailsAdmin::Config::Actions.reset
  RailsAdmin::Config::Actions.instance_eval(&block)
end
asset_source() click to toggle source
# File lib/rails_admin/config.rb, line 249
      def asset_source
        @asset_source ||=
          begin
            detected = defined?(Sprockets) ? :sprockets : :invalid
            unless ARGV.join(' ').include? 'rails_admin:install'
              warn <<~MSG
                [Warning] After upgrading RailsAdmin to 3.x you haven't set asset_source yet, using :#{detected} as the default.
                To suppress this message, run 'rails rails_admin:install' to setup the asset delivery method suitable to you.
              MSG
            end
            detected
          end
      end
audit_with(*args, &block) click to toggle source

Setup auditing/versioning provider that observe objects lifecycle

# File lib/rails_admin/config.rb, line 120
def audit_with(*args, &block)
  extension = args.shift
  if extension
    klass = RailsAdmin::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) click to toggle source

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.

If you use devise, this will authenticate the same as authenticate_user!

@example Devise admin

RailsAdmin.config do |config|
  config.authenticate_with do
    authenticate_admin!
  end
end

@example Custom Warden

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

@see RailsAdmin::Config::DEFAULT_AUTHENTICATION

# File lib/rails_admin/config.rb, line 114
def authenticate_with(&blk)
  @authenticate = blk if blk
  @authenticate || DEFAULT_AUTHENTICATION
end
authorize_with(*args, &block) click to toggle source

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.

@example Custom

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

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

@example CanCanCan

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

See the wiki for more on authorization.

@see RailsAdmin::Config::DEFAULT_AUTHORIZE

# File lib/rails_admin/config.rb, line 157
def authorize_with(*args, &block)
  extension = args.shift
  if extension
    klass = RailsAdmin::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| ... } click to toggle source

Setup configuration using an extension-provided ConfigurationAdapter

@example Custom configuration for role-based setup.

RailsAdmin.config do |config|
  config.configure_with(:custom) do |config|
    config.models = ['User', 'Comment']
    config.roles  = {
      'Admin' => :all,
      'User'  => ['User']
    }
  end
end
# File lib/rails_admin/config.rb, line 183
def configure_with(extension)
  configuration = RailsAdmin::CONFIGURATION_ADAPTERS[extension].new
  yield(configuration) if block_given?
end
current_user_method(&block) click to toggle source

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.

@example Custom

RailsAdmin.config do |config|
  config.current_user_method do
    current_admin
  end
end

@see RailsAdmin::Config::DEFAULT_CURRENT_USER

# File lib/rails_admin/config.rb, line 201
def current_user_method(&block)
  @current_user = block if block
  @current_user || DEFAULT_CURRENT_USER
end
default_hidden_fields=(fields) click to toggle source
# File lib/rails_admin/config.rb, line 263
def default_hidden_fields=(fields)
  if fields.is_a?(Array)
    @default_hidden_fields = {}
    @default_hidden_fields[:edit] = fields
    @default_hidden_fields[:show] = fields
  else
    @default_hidden_fields = fields
  end
end
default_search_operator=(operator) click to toggle source
# File lib/rails_admin/config.rb, line 206
def default_search_operator=(operator)
  if %w[default like not_like starts_with ends_with is =].include? operator
    @default_search_operator = operator
  else
    raise ArgumentError.new("Search operator '#{operator}' not supported")
  end
end
model(entity, &block) click to toggle source

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 RailsAdmin::AbstractModel instance.

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

Returns given model’s configuration

@see RailsAdmin::Config.registry

# File lib/rails_admin/config.rb, line 231
def model(entity, &block)
  key =
    case entity
    when RailsAdmin::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] ||= RailsAdmin::Config::LazyModel.new(key.to_s)
  @registry[key].add_deferred_block(&block) if block
  @registry[key]
end
models() click to toggle source

Returns all model configurations

@see RailsAdmin::Config.registry

# File lib/rails_admin/config.rb, line 305
def models
  RailsAdmin::AbstractModel.all.collect { |m| model(m) }
end
models_pool() click to toggle source

pool of all found model names from the whole application

# File lib/rails_admin/config.rb, line 215
def models_pool
  (viable_models - excluded_models.collect(&:to_s)).uniq.sort
end
parent_controller=(name) click to toggle source
# File lib/rails_admin/config.rb, line 273
def parent_controller=(name)
  @parent_controller = name

  if defined?(RailsAdmin::ApplicationController) || defined?(RailsAdmin::MainController)
    RailsAdmin::Config::ConstLoadSuppressor.allowing do
      RailsAdmin.send(:remove_const, :ApplicationController)
      RailsAdmin.send(:remove_const, :MainController)
      load RailsAdmin::Engine.root.join('app/controllers/rails_admin/application_controller.rb')
      load RailsAdmin::Engine.root.join('app/controllers/rails_admin/main_controller.rb')
    end
  end
end
reload!() click to toggle source

Perform reset, then load RailsAdmin initializer again

# File lib/rails_admin/config.rb, line 352
def reload!
  reset
  load RailsAdmin::Engine.config.initializer_path
end
reset() click to toggle source

Reset all configurations to defaults.

@see RailsAdmin::Config.registry

# File lib/rails_admin/config.rb, line 312
def reset
  @compact_show_view = true
  @browser_validations = true
  @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]
  @main_app_name = proc { [Rails.application.engine_name.titleize.chomp(' Application'), 'Admin'] }
  @registry = {}
  @navbar_css_classes = %w[navbar-dark bg-primary border-bottom]
  @show_gravatar = true
  @navigation_static_links = {}
  @navigation_static_label = nil
  @asset_source = nil
  @composite_keys_serializer = RailsAdmin::Support::CompositeKeysSerializer
  @parent_controller = '::ActionController::Base'
  @forgery_protection_settings = {with: :exception}
  RailsAdmin::Config::Actions.reset
  RailsAdmin::AbstractModel.reset
end
reset_model(model) click to toggle source

Reset a provided model’s configuration.

@see RailsAdmin::Config.registry

# File lib/rails_admin/config.rb, line 346
def reset_model(model)
  key = model.is_a?(Class) ? model.name.to_sym : model.to_sym
  @registry.delete(key)
end
sidescroll=(_) click to toggle source
# File lib/rails_admin/config.rb, line 290
def sidescroll=(_)
  ActiveSupport::Deprecation.warn('The sidescroll configuration option was removed, it is always enabled now.')
end
total_columns_width=(_) click to toggle source
# File lib/rails_admin/config.rb, line 286
def total_columns_width=(_)
  ActiveSupport::Deprecation.warn('The total_columns_width configuration option is deprecated and has no effect.')
end
visible_models(bindings) click to toggle source

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

@see RailsAdmin::Config::Hideable

# File lib/rails_admin/config.rb, line 360
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

Private Class Methods

viable_models() click to toggle source
# File lib/rails_admin/config.rb, line 372
def viable_models
  included_models.collect(&:to_s).presence || begin
    @@system_models ||= # memoization for tests
      ([Rails.application] + Rails::Engine.subclasses.collect(&:instance)).flat_map do |app|
        (app.paths['app/models'].to_a + app.config.eager_load_paths).collect do |load_path|
          Dir.glob(app.root.join(load_path)).collect do |load_dir|
            path_prefix = "#{app.root.join(load_dir)}/"
            Dir.glob("#{load_dir}/**/*.rb").collect do |filename|
              # app/models/module/class.rb => module/class.rb => module/class => Module::Class
              filename.delete_prefix(path_prefix).chomp('.rb').camelize
            end
          end
        end
      end.flatten.reject { |m| m.starts_with?('Concerns::') } # rubocop:disable Style/MultilineBlockChain

    @@system_models + @registry.keys.collect(&:to_s)
  end
end
visible_models_with_bindings(bindings) click to toggle source
# File lib/rails_admin/config.rb, line 391
def visible_models_with_bindings(bindings)
  models.collect { |m| m.with(bindings) }.select do |m|
    m.visible? &&
      RailsAdmin::Config::Actions.find(:index, bindings.merge(abstract_model: m.abstract_model)).try(:authorized?) &&
      (!m.abstract_model.embedded? || m.abstract_model.cyclic?)
  end
end