module RailsAdmin::Config
Constants
- DEFAULT_ATTR_ACCESSIBLE_ROLE
- DEFAULT_AUDIT
- DEFAULT_AUTHENTICATION
RailsAdmin
is setup to try and authenticate with warden If warden is found, then it will try to authenticateThis 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
@seeRailsAdmin::Config.authorize_with
- DEFAULT_AUTHORIZE
- DEFAULT_CURRENT_USER
Attributes
hide blank fields in show view if true
Default items per page value used if a model level option has not been configured
Configuration option to specify which models you want to exclude.
Configuration option to specify a whitelist of models you want to RailsAdmin
to work with. The excluded_models
list applies against the whitelist 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).
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]
Application title, can be an array of two elements
Stores model configuration objects in a hash identified by model’s class name.
@see RailsAdmin.config
Set the max width of columns in list view before a new set is created
yell about fields that are not marked as accessible
Public Class Methods
Returns action configuration object
# File lib/rails_admin/config.rb, line 266 def actions(&block) RailsAdmin::Config::Actions.instance_eval(&block) if block end
# File lib/rails_admin/config.rb, line 105 def attr_accessible_role(&blk) @attr_accessible_role = blk if blk @attr_accessible_role || DEFAULT_ATTR_ACCESSIBLE_ROLE end
Setup auditing/history/versioning provider that observe objects lifecycle
# File lib/rails_admin/config.rb, line 111 def audit_with(*args, &block) extension = args.shift if(extension) @audit = Proc.new { @auditing_adapter = RailsAdmin::AUDITING_ADAPTERS[extension].new(*([self] + args).compact) } else @audit = block if block end @audit || DEFAULT_AUDIT end
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 99 def authenticate_with(&blk) @authenticate = blk if blk @authenticate || DEFAULT_AUTHENTICATION end
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 170 def configure_with(extension) configuration = RailsAdmin::CONFIGURATION_ADAPTERS[extension].new yield(configuration) if block_given? end
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 188 def current_user_method(&block) @current_user = block if block @current_user || DEFAULT_CURRENT_USER end
# File lib/rails_admin/config.rb, line 193 def default_search_operator=(operator) if %w{ default like starts_with ends_with is = }.include? operator @default_search_operator = operator else raise ArgumentError, "Search operator '#{operator}' not supported" end end
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 235 def model(entity, &block) key = begin if entity.kind_of?(RailsAdmin::AbstractModel) entity.model.try(:name).try :to_sym elsif entity.kind_of?(Class) entity.name.to_sym elsif entity.kind_of?(String) || entity.kind_of?(Symbol) entity.to_sym else entity.class.name.to_sym end end if block @registry[key] = RailsAdmin::Config::LazyModel.new(entity, &block) else @registry[key] ||= RailsAdmin::Config::LazyModel.new(entity) end end
Returns all model configurations
@see RailsAdmin::Config.registry
# File lib/rails_admin/config.rb, line 273 def models RailsAdmin::AbstractModel.all.map{|m| model(m)} end
pool of all found model names from the whole application
# File lib/rails_admin/config.rb, line 202 def models_pool possible = included_models.map(&:to_s).presence || ( @@system_models ||= # memoization for tests ([Rails.application] + Rails::Application::Railties.engines).map do |app| (app.paths['app/models'] + app.config.autoload_paths).map do |load_path| Dir.glob(app.root.join(load_path)).map do |load_dir| Dir.glob(load_dir + "/**/*.rb").map do |filename| # app/models/module/class.rb => module/class.rb => module/class => Module::Class lchomp(filename, "#{app.root.join(load_dir)}/").chomp('.rb').camelize end end end end.flatten ) excluded = (excluded_models.map(&:to_s) + ['RailsAdmin::History']) (possible - excluded).uniq.sort end
Reset all configurations to defaults.
@see RailsAdmin::Config.registry
# File lib/rails_admin/config.rb, line 280 def reset @compact_show_view = true @yell_for_non_accessible_fields = true @authenticate = nil @authorize = nil @audit = nil @current_user = nil @default_hidden_fields = {} @default_hidden_fields[:base] = [:_type] @default_hidden_fields[:edit] = [:id, :_id, :created_at, :created_on, :deleted_at, :updated_at, :updated_on, :deleted_on] @default_hidden_fields[:show] = [:id, :_id, :created_at, :created_on, :deleted_at, :updated_at, :updated_on, :deleted_on] @default_items_per_page = 20 @default_search_operator = 'default' @attr_accessible_role = nil @excluded_models = [] @included_models = [] @total_columns_width = 697 @label_methods = [:name, :title] @main_app_name = Proc.new { [Rails.application.engine_name.titleize.chomp(' Application'), 'Admin'] } @registry = {} @navigation_static_links = {} @navigation_static_label = nil RailsAdmin::Config::Actions.reset end
Reset a provided model’s configuration.
@see RailsAdmin::Config.registry
# File lib/rails_admin/config.rb, line 308 def reset_model(model) key = model.kind_of?(Class) ? model.name.to_sym : model.to_sym @registry.delete(key) end
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 317 def visible_models(bindings) models.map{|m| m.with(bindings) }.select{|m| m.visible? && bindings[:controller].authorized?(:index, m.abstract_model) && !m.abstract_model.embedded?}.sort do |a, b| (weight_order = a.weight <=> b.weight) == 0 ? a.label.downcase <=> b.label.downcase : weight_order end end
Private Class Methods
# File lib/rails_admin/config.rb, line 325 def lchomp(base, arg) base.to_s.reverse.chomp(arg.to_s.reverse).reverse end