1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
require 'rails_admin/config/lazy_model'
require 'rails_admin/config/sections/list'
require 'active_support/core_ext/module/attribute_accessors'
module RailsAdmin
module Config
# 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_AUTHENTICATION = proc {}
DEFAULT_AUTHORIZE = proc {}
DEFAULT_AUDIT = proc {}
DEFAULT_CURRENT_USER = proc {}
class << self
# Application title, can be an array of two elements
attr_accessor :main_app_name
# Configuration option to specify which models you want to exclude.
attr_accessor :excluded_models
# 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).
attr_accessor :included_models
# Fields to be hidden in show, create and update views
attr_accessor :default_hidden_fields
# Default items per page value used if a model level option has not
# been configured
attr_accessor :default_items_per_page
attr_reader :default_search_operator
# 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]
attr_accessor :label_methods
# hide blank fields in show view if true
attr_accessor :compact_show_view
# Tell browsers whether to use the native HTML5 validations (novalidate form option).
attr_accessor :browser_validations
# Set the max width of columns in list view before a new set is created
attr_accessor :total_columns_width
# set parent controller
attr_accessor :parent_controller
# Stores model configuration objects in a hash identified by model's class
# name.
#
# @see RailsAdmin.config
attr_reader :registry
# accepts a hash of static links to be shown below the main navigation
attr_accessor :navigation_static_links
attr_accessor :navigation_static_label
# yell about fields that are not marked as accessible
attr_accessor :yell_for_non_accessible_fields
# 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
def authenticate_with(&blk)
@authenticate = blk if blk
@authenticate || DEFAULT_AUTHENTICATION
end
# Setup auditing/history/versioning provider that observe objects lifecycle
def audit_with(*args, &block)
extension = args.shift
if extension
@audit = proc do
@auditing_adapter = RailsAdmin::AUDITING_ADAPTERS[extension].new(*([self] + args).compact)
end
else
@audit = block if block
end
@audit || DEFAULT_AUDIT
end
# 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 CanCan[https://github.com/ryanb/cancan], pass it like this.
#
# @example CanCan
# RailsAdmin.config do |config|
# config.authorize_with :cancan
# end
#
# See the wiki[https://github.com/sferik/rails_admin/wiki] for more on authorization.
#
# @see RailsAdmin::Config::DEFAULT_AUTHORIZE
def authorize_with(*args, &block)
extension = args.shift
if extension
@authorize = proc do
@authorization_adapter = RailsAdmin::AUTHORIZATION_ADAPTERS[extension].new(*([self] + args).compact)
end
else
@authorize = block if block
end
@authorize || DEFAULT_AUTHORIZE
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
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["warden"].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
def current_user_method(&block)
@current_user = block if block
@current_user || DEFAULT_CURRENT_USER
end
def default_search_operator=(operator)
if %w(default like starts_with ends_with is =).include? operator
@default_search_operator = operator
else
fail(ArgumentError.new("Search operator '#{operator}' not supported"))
end
end
# pool of all found model names from the whole application
def models_pool
excluded = (excluded_models.collect(&:to_s) + ['RailsAdmin::History'])
(viable_models - excluded).uniq.sort
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
def model(entity, &block)
key = begin
if entity.is_a?(RailsAdmin::AbstractModel)
entity.model.try(:name).try :to_sym
elsif entity.is_a?(Class)
entity.name.to_sym
elsif entity.is_a?(String) || entity.is_a?(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
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
# Returns action configuration object
def actions(&block)
RailsAdmin::Config::Actions.instance_eval(&block) if block
end
# Returns all model configurations
#
# @see RailsAdmin::Config.registry
def models
RailsAdmin::AbstractModel.all.collect { |m| model(m) }
end
# Reset all configurations to defaults.
#
# @see RailsAdmin::Config.registry
def reset
@compact_show_view = true
@browser_validations = 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'
@excluded_models = []
@included_models = []
@total_columns_width = 697
@label_methods = [:name, :title]
@main_app_name = proc { [Rails.application.engine_name.titleize.chomp(' Application'), 'Admin'] }
@registry = {}
@navigation_static_links = {}
@navigation_static_label = nil
@parent_controller = '::ApplicationController'
RailsAdmin::Config::Actions.reset
end
# Reset a provided model's configuration.
#
# @see RailsAdmin::Config.registry
def reset_model(model)
key = model.is_a?(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
def visible_models(bindings)
visible_models_with_bindings(bindings).sort do |a, b|
if (weight_order = a.weight <=> b.weight) == 0
a.label.downcase <=> b.label.downcase
else
weight_order
end
end
end
private
def lchomp(base, arg)
base.to_s.reverse.chomp(arg.to_s.reverse).reverse
end
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.autoload_paths).collect do |load_path|
Dir.glob(app.root.join(load_path)).collect do |load_dir|
Dir.glob(load_dir + '/**/*.rb').collect 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.reject { |m| m.starts_with?('Concerns::') } # rubocop:disable MultilineBlockChain
end
end
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
end
# Set default values for configuration options on load
reset
end
end
|