class Rails::Configuration
The Configuration
class holds all the parameters for the Initializer
and ships with defaults that suites most Rails applications. But it’s possible to overwrite everything. Usually, you’ll create an Configuration
file implicitly through the block running on the Initializer
, but it’s also possible to create the Configuration
instance in advance and pass it in like this:
config = Rails::Configuration.new Rails::Initializer.run(:process, config)
Attributes
A stub for setting options on ActionController::Base.
A stub for setting options on ActionMailer::Base.
A stub for setting options on ActionView::Base.
A stub for setting options on ActiveRecord::Base.
A stub for setting options on ActiveResource::Base.
A stub for setting options on ActiveSupport.
An array of paths from which Rails will automatically load from only once. All elements of this array must also be in autoload_paths
.
An array of additional paths to prepend to the load path. By default, all app
, lib
, vendor
and mock paths are included in this list.
Whether or not classes should be cached (set to false if you want application classes to be reloaded on each request)
The specific cache store to use. By default, the ActiveSupport::Cache::Store will be used.
The list of paths that should be searched for controllers. (Defaults to app/controllers
.)
The path to the database configuration file to use. (Defaults to config/database.yml
.)
Enables or disables dependency loading during the request cycle. Setting dependency_loading
to true will allow new classes to be loaded during a request. Setting it to false will disable this behavior.
Those who want to run in a threaded environment should disable this option and eager load or require all there classes on initialization.
If cache_classes
is disabled, dependency loaded will always be on.
An array of paths from which Rails will eager load on boot if cache classes is enabled. All elements of this array must also be in autoload_paths
.
The list of rails framework components that should be loaded. (Defaults to :active_record
, :action_controller
, :action_view
, :action_mailer
, and :active_resource
).
An array of gems that this rails application depends on. Rails will automatically load these gems during installation, and allow you to install any missing gems with:
rake gems:install
You can add gems with the gem
method.
Accessor for i18n settings.
The log level to use for the default Rails logger. In production mode, this defaults to :info
. In development mode, it defaults to :debug
.
The path to the log file to use. Defaults to log/#{environment}.log (e.g. log/development.log or log/production.log).
The list of metals to load. If this is set to nil
, all metals will be loaded in alphabetical order. If this is set to []
, no metals will be loaded. Otherwise metals will be loaded in the order specified
The class that handles loading each plugin. Defaults to Rails::Plugin::Loader, but a sub class would have access to fine grained modification of the loading behavior. See the implementation of Rails::Plugin::Loader for more details.
The classes that handle finding the desired plugins that you’d like to load for your application. By default it is the Rails::Plugin::FileSystemLocator which finds plugins to load in vendor/plugins
. You can hook into gem location by subclassing Rails::Plugin::Locator and adding it onto the list of plugin_locators
.
The path to the root of the plugins directory. By default, it is in vendor/plugins
.
The list of plugins to load. If this is set to nil
, all plugins will be loaded. If this is set to []
, no plugins will be loaded. Otherwise, plugins will be loaded in the order specified.
Whether to preload all frameworks at startup.
Enables or disables plugin reloading. You can get around this setting per plugin. If reload_plugins?
is false, add this to your plugin’s init.rb
to make it reloadable:
ActiveSupport::Dependencies.autoload_once_paths.delete lib_path
If reload_plugins?
is true, add this to your plugin’s init.rb
to only load it once:
ActiveSupport::Dependencies.autoload_once_paths << lib_path
The application’s base directory.
The path to the routes configuration file to use. (Defaults to config/routes.rb
.)
Sets the default time_zone
. Setting this will enable time_zone
awareness for Active Record models and set the Active Record default timezone to :utc
.
The root of the application’s views. (Defaults to app/views
.)
Set to true
if you want to be warned (noisily) when you try to invoke any method of nil
. Set to false
for the standard Ruby behavior.
Public Class Methods
Create a new Configuration
instance, initialized with the default values.
# File lib/rails-2.3.14/initializer.rb, line 885 def initialize set_root_path! self.frameworks = default_frameworks self.autoload_paths = default_autoload_paths self.autoload_once_paths = default_autoload_once_paths self.eager_load_paths = default_eager_load_paths self.log_path = default_log_path self.log_level = default_log_level self.view_path = default_view_path self.controller_paths = default_controller_paths self.preload_frameworks = default_preload_frameworks self.cache_classes = default_cache_classes self.dependency_loading = default_dependency_loading self.whiny_nils = default_whiny_nils self.plugins = default_plugins self.plugin_paths = default_plugin_paths self.plugin_locators = default_plugin_locators self.plugin_loader = default_plugin_loader self.database_configuration_file = default_database_configuration_file self.routes_configuration_file = default_routes_configuration_file self.gems = default_gems self.i18n = default_i18n for framework in default_frameworks self.send("#{framework}=", Rails::OrderedOptions.new) end self.active_support = Rails::OrderedOptions.new end
Public Instance Methods
Adds a block which will be executed after rails has been fully initialized. Useful for per-environment configuration which depends on the framework being fully initialized.
# File lib/rails-2.3.14/initializer.rb, line 970 def after_initialize(&after_initialize_block) after_initialize_blocks << after_initialize_block if after_initialize_block end
Returns the blocks added with Configuration#after_initialize
# File lib/rails-2.3.14/initializer.rb, line 975 def after_initialize_blocks @after_initialize_blocks ||= [] end
Deprecated options:
# File lib/rails-2.3.14/initializer.rb, line 866 def breakpoint_server(_ = nil) $stderr.puts %( ******************************************************************* * config.breakpoint_server has been deprecated and has no effect. * ******************************************************************* ) end
# File lib/rails-2.3.14/initializer.rb, line 995 def builtin_directories # Include builtins only in the development environment. (environment == 'development') ? Dir["#{RAILTIES_PATH}/builtin/*/"] : [] end
Loads and returns the contents of the database_configuration_file
. The contents of the file are processed via ERB before being sent through YAML::load.
# File lib/rails-2.3.14/initializer.rb, line 950 def database_configuration require 'erb' YAML::load(ERB.new(IO.read(database_configuration_file)).result) end
Return the currently selected environment. By default, it returns the value of the RAILS_ENV constant.
# File lib/rails-2.3.14/initializer.rb, line 963 def environment ::RAILS_ENV end
The path to the current environment’s file (development.rb
, etc.). By default the file is at config/environments/#{environment}.rb
.
# File lib/rails-2.3.14/initializer.rb, line 957 def environment_path "#{root_path}/config/environments/#{environment}.rb" end
# File lib/rails-2.3.14/initializer.rb, line 1000 def framework_paths paths = %w(railties railties/lib activesupport/lib) paths << 'actionpack/lib' if frameworks.include?(:action_controller) || frameworks.include?(:action_view) [:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework| paths << "#{framework.to_s.gsub('_', '')}/lib" if frameworks.include?(framework) end paths.map { |dir| "#{framework_root_path}/#{dir}" }.select { |dir| File.directory?(dir) } end
Adds a single Gem dependency to the rails application. By default, it will require the library with the same name as the gem. Use :lib to specify a different name.
# gem 'aws-s3', '>= 0.4.0' # require 'aws/s3' config.gem 'aws-s3', :lib => 'aws/s3', :version => '>= 0.4.0', \ :source => "http://code.whytheluckystiff.net"
To require a library be installed, but not attempt to load it, pass :lib => false
config.gem 'qrp', :version => '0.4.1', :lib => false
# File lib/rails-2.3.14/initializer.rb, line 861 def gem(name, options = {}) @gems << Rails::GemDependency.new(name, options) end
Deprecated, use autoload_once_paths.
# File lib/rails-2.3.14/initializer.rb, line 744 def load_once_paths $stderr.puts("config.load_once_paths is deprecated and removed in Rails 3, please use autoload_once_paths instead") autoload_once_paths end
Deprecated, use autoload_once_paths
=.
# File lib/rails-2.3.14/initializer.rb, line 750 def load_once_paths=(paths) $stderr.puts("config.load_once_paths= is deprecated and removed in Rails 3, please use autoload_once_paths= instead") self.autoload_once_paths = paths end
Deprecated, use autoload_paths.
# File lib/rails-2.3.14/initializer.rb, line 728 def load_paths $stderr.puts("config.load_paths is deprecated and removed in Rails 3, please use autoload_paths instead") autoload_paths end
Deprecated, use autoload_paths
=.
# File lib/rails-2.3.14/initializer.rb, line 734 def load_paths=(paths) $stderr.puts("config.load_paths= is deprecated and removed in Rails 3, please use autoload_paths= instead") self.autoload_paths = paths end
# File lib/rails-2.3.14/initializer.rb, line 990 def middleware require 'action_controller' ActionController::Dispatcher.middleware end
# File lib/rails-2.3.14/initializer.rb, line 789 def plugins=(plugins) @plugins = plugins.nil? ? nil : plugins.map { |p| p.to_sym } end
Returns true if plugin reloading is enabled.
# File lib/rails-2.3.14/initializer.rb, line 827 def reload_plugins? !!@reload_plugins end
Set the root_path
to RAILS_ROOT and canonicalize it.
# File lib/rails-2.3.14/initializer.rb, line 916 def set_root_path! raise 'RAILS_ROOT is not set' unless defined?(::RAILS_ROOT) raise 'RAILS_ROOT is not a directory' unless File.directory?(::RAILS_ROOT) @root_path = # Pathname is incompatible with Windows, but Windows doesn't have # real symlinks so File.expand_path is safe. if RUBY_PLATFORM =~ /(:?mswin|mingw)/ File.expand_path(::RAILS_ROOT) # Otherwise use Pathname#realpath which respects symlinks. else Pathname.new(::RAILS_ROOT).realpath.to_s end Object.const_set(:RELATIVE_RAILS_ROOT, ::RAILS_ROOT.dup) unless defined?(::RELATIVE_RAILS_ROOT) ::RAILS_ROOT.replace @root_path end
Enable threaded mode. Allows concurrent requests to controller actions and multiple database connections. Also disables automatic dependency loading after boot, and disables reloading code on every request, as these are fundamentally incompatible with thread safety.
# File lib/rails-2.3.14/initializer.rb, line 939 def threadsafe! self.preload_frameworks = true self.cache_classes = true self.dependency_loading = false self.action_controller.allow_concurrency = true self end
Add a preparation callback that will run before every request in development mode, or before the first request in production.
See Dispatcher#to_prepare.
# File lib/rails-2.3.14/initializer.rb, line 983 def to_prepare(&callback) after_initialize do require 'dispatcher' unless defined?(::Dispatcher) Dispatcher.to_prepare(&callback) end end
Private Instance Methods
Doesn’t matter since plugins aren’t in autoload_paths
yet.
# File lib/rails-2.3.14/initializer.rb, line 1045 def default_autoload_once_paths [] end
# File lib/rails-2.3.14/initializer.rb, line 1020 def default_autoload_paths paths = [] # Add the old mock paths only if the directories exists paths.concat(Dir["#{root_path}/test/mocks/#{environment}"]) if File.exists?("#{root_path}/test/mocks/#{environment}") # Add the app's controller directory paths.concat(Dir["#{root_path}/app/controllers/"]) # Followed by the standard includes. paths.concat %w( app app/metal app/models app/controllers app/helpers app/services lib vendor ).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) } paths.concat builtin_directories end
# File lib/rails-2.3.14/initializer.rb, line 1092 def default_cache_classes true end
# File lib/rails-2.3.14/initializer.rb, line 1118 def default_cache_store if File.exist?("#{root_path}/tmp/cache/") [ :file_store, "#{root_path}/tmp/cache/" ] else :memory_store end end
# File lib/rails-2.3.14/initializer.rb, line 1078 def default_controller_paths paths = [File.join(root_path, 'app', 'controllers')] paths.concat builtin_directories paths end
# File lib/rails-2.3.14/initializer.rb, line 1066 def default_database_configuration_file File.join(root_path, 'config', 'database.yml') end
# File lib/rails-2.3.14/initializer.rb, line 1084 def default_dependency_loading true end
# File lib/rails-2.3.14/initializer.rb, line 1049 def default_eager_load_paths %w( app/metal app/models app/controllers app/helpers ).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) } end
# File lib/rails-2.3.14/initializer.rb, line 1016 def default_frameworks [ :active_record, :action_controller, :action_view, :action_mailer, :active_resource ] end
# File lib/rails-2.3.14/initializer.rb, line 1126 def default_gems [] end
# File lib/rails-2.3.14/initializer.rb, line 1130 def default_i18n i18n = Rails::OrderedOptions.new i18n.load_path = [] if File.exist?(File.join(RAILS_ROOT, 'config', 'locales')) i18n.load_path << Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')] i18n.load_path.flatten! end i18n end
# File lib/rails-2.3.14/initializer.rb, line 1062 def default_log_level environment == 'production' ? :info : :debug end
# File lib/rails-2.3.14/initializer.rb, line 1058 def default_log_path File.join(root_path, 'log', "#{environment}.log") end
# File lib/rails-2.3.14/initializer.rb, line 1114 def default_plugin_loader Plugin::Loader end
# File lib/rails-2.3.14/initializer.rb, line 1108 def default_plugin_locators locators = [] locators << Plugin::GemLocator if defined? Gem locators << Plugin::FileSystemLocator end
# File lib/rails-2.3.14/initializer.rb, line 1104 def default_plugin_paths ["#{root_path}/vendor/plugins"] end
# File lib/rails-2.3.14/initializer.rb, line 1100 def default_plugins nil end
# File lib/rails-2.3.14/initializer.rb, line 1088 def default_preload_frameworks false end
# File lib/rails-2.3.14/initializer.rb, line 1070 def default_routes_configuration_file File.join(root_path, 'config', 'routes.rb') end
# File lib/rails-2.3.14/initializer.rb, line 1074 def default_view_path File.join(root_path, 'app', 'views') end
# File lib/rails-2.3.14/initializer.rb, line 1096 def default_whiny_nils false end
# File lib/rails-2.3.14/initializer.rb, line 1012 def framework_root_path defined?(::RAILS_FRAMEWORK_ROOT) ? ::RAILS_FRAMEWORK_ROOT : "#{root_path}/vendor/rails" end