5 $LOAD_PATH.unshift File
.dirname(__FILE__
)
6 require 'railties_path'
7 require 'rails/version'
8 require 'rails/plugin/locator'
9 require 'rails/plugin/loader'
10 require 'rails/gem_dependency'
14 RAILS_ENV
= (ENV['RAILS_ENV'] || 'development').dup
unless defined?(RAILS_ENV
)
18 # The Configuration instance used to configure the Rails environment
23 def configuration
=(configuration
)
24 @
@configuration = configuration
31 def initialized
=(initialized
)
32 @initialized ||= initialized
36 if defined?(RAILS_DEFAULT_LOGGER
)
44 @
@backtrace_cleaner ||= begin
45 # Relies on ActiveSupport, so we have to lazy load to postpone definition until AS has been loaded
46 require 'rails/backtrace_cleaner'
47 Rails
::BacktraceCleaner.new
52 Pathname
.new(RAILS_ROOT
) if defined?(RAILS_ROOT
)
56 @_env ||= ActiveSupport
::StringInquirer.new(RAILS_ENV
)
68 @
@public_path ||= self.root
? File
.join(self.root
, "public") : "public"
71 def public_path
=(path
)
76 # The Initializer is responsible for processing the Rails configuration, such
77 # as setting the $LOAD_PATH, requiring the right frameworks, initializing
78 # logging, and more. It can be run either as a single command that'll just
79 # use the default configuration, like this:
81 # Rails::Initializer.run
83 # But normally it's more interesting to pass in a custom configuration
84 # through the block running:
86 # Rails::Initializer.run do |config|
87 # config.frameworks -= [ :action_mailer ]
90 # This will use the default configuration options from Rails::Configuration,
91 # but allow for overwriting on select areas.
93 # The Configuration instance used by this Initializer instance.
94 attr_reader
:configuration
96 # The set of loaded plugins.
97 attr_reader
:loaded_plugins
99 # Whether or not all the gem dependencies have been met
100 attr_reader
:gems_dependencies_loaded
102 # Runs the initializer. By default, this will invoke the #process method,
103 # which simply executes all of the initialization routines. Alternately,
104 # you can specify explicitly which initialization routine you want:
106 # Rails::Initializer.run(:set_load_path)
108 # This is useful if you only want the load path initialized, without
109 # incurring the overhead of completely loading the entire environment.
110 def self.run(command
= :process, configuration
= Configuration
.new
)
111 yield configuration
if block_given
?
112 initializer
= new configuration
113 initializer
.send(command
)
117 # Create a new Initializer instance that references the given Configuration
119 def initialize(configuration
)
120 @configuration = configuration
124 # Sequentially step through all of the available initialization routines,
125 # in order (view execution order in source).
127 Rails
.configuration
= configuration
130 install_gem_spec_stubs
136 add_plugin_load_paths
144 initialize_framework_caches
147 initialize_framework_logging
149 initialize_dependency_mechanism
150 initialize_whiny_nils
155 initialize_framework_settings
156 initialize_framework_views
160 add_support_load_paths
165 # pick up any gems that plugins depend on
168 check_gem_dependencies
170 # bail out if gems are missing - note that check_gem_dependencies will have
171 # already called abort() unless $gems_rake_task is set
172 return unless gems_dependencies_loaded
174 load_application_initializers
176 # the framework is now fully initialized
179 # Setup database middleware after initializers have run
180 initialize_database_middleware
182 # Prepare dispatcher callbacks and run 'prepare' callbacks
185 # Routing must be initialized after plugins to allow the former to extend the routes
188 # Observers are loaded after plugins in case Observers or observed models are modified by plugins.
191 # Load view path cache
194 # Load application classes
195 load_application_classes
197 # Disable dependency loading during request cycle
198 disable_dependency_loading
201 Rails
.initialized
= true
204 # Check for valid Ruby version
205 # This is done in an external file, so we can use it
206 # from the `rails` program as well without duplication.
207 def check_ruby_version
208 require 'ruby_version_check'
211 # If Rails is vendored and RubyGems is available, install stub GemSpecs
212 # for Rails, Active Support, Active Record, Action Pack, Action Mailer, and
213 # Active Resource. This allows Gem plugins to depend on Rails even when
214 # the Gem version of Rails shouldn't be loaded.
215 def install_gem_spec_stubs
216 unless Rails
.respond_to
?(:vendor_rails?)
217 abort %
{Your config
/boot
.rb is outdated
: Run
"rake rails:update".}
220 if Rails
.vendor_rails
?
221 begin; require "rubygems"; rescue LoadError
; return; end
223 stubs
= %w(rails activesupport activerecord actionpack actionmailer activeresource
)
224 stubs
.reject
! { |s
| Gem
.loaded_specs
.key
?(s
) }
227 Gem
.loaded_specs
[stub
] = Gem
::Specification.new
do |s
|
229 s
.version = Rails
::VERSION::STRING
236 # Set the <tt>$LOAD_PATH</tt> based on the value of
237 # Configuration#load_paths. Duplicates are removed.
239 load_paths
= configuration
.load_paths
+ configuration
.framework_paths
240 load_paths
.reverse_each
{ |dir
| $LOAD_PATH.unshift(dir
) if File
.directory
?(dir
) }
244 # Set the paths from which Rails will automatically load source files, and
245 # the load_once paths.
246 def set_autoload_paths
247 ActiveSupport
::Dependencies.load_paths
= configuration
.load_paths
.uniq
248 ActiveSupport
::Dependencies.load_once_paths
= configuration
.load_once_paths
.uniq
250 extra
= ActiveSupport
::Dependencies.load_once_paths
- ActiveSupport
::Dependencies.load_paths
253 load_once_paths must be a subset of the load_paths.
254 Extra items in load_once_paths: #{extra * ','}
258 # Freeze the arrays so future modifications will fail rather than do nothing mysteriously
259 configuration
.load_once_paths
.freeze
262 # Requires all frameworks specified by the Configuration#frameworks
263 # list. By default, all frameworks (Active Record, Active Support,
264 # Action Pack, Action Mailer, and Active Resource) are loaded.
265 def require_frameworks
266 configuration
.frameworks
.each
{ |framework
| require(framework
.to_s
) }
267 rescue LoadError
=> e
268 # Re-raise as RuntimeError because Mongrel would swallow LoadError.
272 # Preload all frameworks specified by the Configuration#frameworks.
273 # Used by Passenger to ensure everything's loaded before forking and
274 # to avoid autoload race conditions in JRuby.
275 def preload_frameworks
276 if configuration
.preload_frameworks
277 configuration
.frameworks
.each
do |framework
|
278 # String#classify and #constantize aren't available yet.
279 toplevel
= Object
.const_get(framework
.to_s
.gsub(/(?:^|_)(.)/) { $1.upcase
})
280 toplevel
.load_all
! if toplevel
.respond_to
?(:load_all!)
285 # Add the load paths used by support functions such as the info controller
286 def add_support_load_paths
289 # Adds all load paths from plugins to the global set of load paths, so that
290 # code from plugins can be required (explicitly or automatically via ActiveSupport::Dependencies).
291 def add_plugin_load_paths
292 plugin_loader
.add_plugin_load_paths
295 def add_gem_load_paths
296 Rails
::GemDependency.add_frozen_gem_path
297 unless @configuration.gems
.empty
?
299 @configuration.gems
.each
{ |gem
| gem
.add_load_paths
}
304 unless $gems_build_rake_task
305 @configuration.gems
.each
{ |gem
| gem
.load
}
309 def check_gem_dependencies
310 unloaded_gems
= @configuration.gems
.reject
{ |g
| g
.loaded
? }
311 if unloaded_gems
.size
> 0
312 @gems_dependencies_loaded = false
313 # don't print if the gems rake tasks are being run
314 unless $gems_rake_task
316 Missing these required gems:
317 #{unloaded_gems.map { |gem| "#{gem.name} #{gem.requirement}" } * "\n "}
320 ruby #{Gem.ruby_version} at #{Gem.ruby}
321 rubygems #{Gem::RubyGemsVersion} at #{Gem.path * ', '}
323 Run `rake gems:install` to install the missing gems.
327 @gems_dependencies_loaded = true
331 # Loads all plugins in <tt>config.plugin_paths</tt>. <tt>plugin_paths</tt>
332 # defaults to <tt>vendor/plugins</tt> but may also be set to a list of
334 # config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]
336 # In the default implementation, as each plugin discovered in <tt>plugin_paths</tt> is initialized:
337 # * its +lib+ directory, if present, is added to the load path (immediately after the applications lib directory)
338 # * <tt>init.rb</tt> is evaluated, if present
340 # After all plugins are loaded, duplicates are removed from the load path.
341 # If an array of plugin names is specified in config.plugins, only those plugins will be loaded
342 # and they plugins will be loaded in that order. Otherwise, plugins are loaded in alphabetical
345 # if config.plugins ends contains :all then the named plugins will be loaded in the given order and all other
346 # plugins will be loaded in alphabetical order
348 plugin_loader
.load_plugins
352 @plugin_loader ||= configuration
.plugin_loader
.new(self)
355 # Loads the environment specified by Configuration#environment_path, which
356 # is typically one of development, test, or production.
359 return if @environment_loaded
360 @environment_loaded = true
362 config
= configuration
363 constants
= self.class.constants
365 eval(IO
.read(configuration
.environment_path
), binding
, configuration
.environment_path
)
367 (self.class.constants
- constants
).each
do |const
|
368 Object
.const_set(const
, self.class.const_get(const
))
374 if gems_dependencies_loaded
&& configuration
.frameworks
.include?(:active_record)
375 ActiveRecord
::Base.instantiate_observers
380 if configuration
.frameworks
.include?(:action_view)
381 ActionController
::Base.view_paths
.load
! if configuration
.frameworks
.include?(:action_controller)
382 ActionMailer
::Base.view_paths
.load
! if configuration
.frameworks
.include?(:action_mailer)
386 # Eager load application classes
387 def load_application_classes
388 return if $rails_rake_task
389 if configuration
.cache_classes
390 configuration
.eager_load_paths
.each
do |load_path
|
391 matcher
= /\A#{Regexp.escape(load_path)}(.*)\.rb\Z/
392 Dir
.glob("#{load_path}/**/*.rb").sort
.each
do |file
|
393 require_dependency file
.sub(matcher
, '\1')
399 # For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
400 # multibyte safe operations. Plugin authors supporting other encodings
401 # should override this behaviour and set the relevant +default_charset+
402 # on ActionController::Base.
404 # For Ruby 1.9, this does nothing. Specify the default encoding in the Ruby
405 # shebang line if you don't want UTF-8.
406 def initialize_encoding
407 $KCODE='u' if RUBY_VERSION < '1.9'
410 # This initialization routine does nothing unless <tt>:active_record</tt>
411 # is one of the frameworks to load (Configuration#frameworks). If it is,
412 # this sets the database configuration from Configuration#database_configuration
413 # and then establishes the connection.
414 def initialize_database
415 if configuration
.frameworks
.include?(:active_record)
416 ActiveRecord
::Base.configurations
= configuration
.database_configuration
417 ActiveRecord
::Base.establish_connection
421 def initialize_database_middleware
422 if configuration
.frameworks
.include?(:active_record)
423 if ActionController
::Base.session_store
== ActiveRecord
::SessionStore
424 configuration
.middleware
.insert_before
:"ActiveRecord::SessionStore", ActiveRecord
::ConnectionAdapters::ConnectionManagement
425 configuration
.middleware
.insert_before
:"ActiveRecord::SessionStore", ActiveRecord
::QueryCache
427 configuration
.middleware
.use ActiveRecord
::ConnectionAdapters::ConnectionManagement
428 configuration
.middleware
.use ActiveRecord
::QueryCache
434 unless defined?(RAILS_CACHE
)
435 silence_warnings
{ Object
.const_set
"RAILS_CACHE", ActiveSupport
::Cache.lookup_store(configuration
.cache_store
) }
437 if RAILS_CACHE
.respond_to
?(:middleware)
438 # Insert middleware to setup and teardown local cache for each request
439 configuration
.middleware
.insert_after(:"ActionController::Failsafe", RAILS_CACHE
.middleware
)
444 def initialize_framework_caches
445 if configuration
.frameworks
.include?(:action_controller)
446 ActionController
::Base.cache_store
||= RAILS_CACHE
450 # If the RAILS_DEFAULT_LOGGER constant is already set, this initialization
451 # routine does nothing. If the constant is not set, and Configuration#logger
452 # is not +nil+, this also does nothing. Otherwise, a new logger instance
453 # is created at Configuration#log_path, with a default log level of
454 # Configuration#log_level.
456 # If the log could not be created, the log will be set to output to
457 # +STDERR+, with a log level of +WARN+.
458 def initialize_logger
459 # if the environment has explicitly defined a logger, use it
460 return if Rails
.logger
462 unless logger
= configuration
.logger
464 logger
= ActiveSupport
::BufferedLogger.new(configuration
.log_path
)
465 logger
.level
= ActiveSupport
::BufferedLogger.const_get(configuration
.log_level
.to_s
.upcase
)
466 if configuration
.environment
== "production"
467 logger
.auto_flushing
= false
469 rescue StandardError
=> e
470 logger
= ActiveSupport
::BufferedLogger.new(STDERR)
471 logger
.level
= ActiveSupport
::BufferedLogger::WARN
473 "Rails Error: Unable to access log file. Please ensure that #{configuration.log_path} exists and is chmod 0666. " +
474 "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
479 silence_warnings
{ Object
.const_set
"RAILS_DEFAULT_LOGGER", logger
}
482 # Sets the logger for Active Record, Action Controller, and Action Mailer
483 # (but only for those frameworks that are to be loaded). If the framework's
484 # logger is already set, it is not changed, otherwise it is set to use
485 # RAILS_DEFAULT_LOGGER.
486 def initialize_framework_logging
487 for framework
in ([ :active_record, :action_controller, :action_mailer ] & configuration
.frameworks
)
488 framework
.to_s
.camelize
.constantize
.const_get("Base").logger
||= Rails
.logger
491 ActiveSupport
::Dependencies.logger
||= Rails
.logger
492 Rails
.cache
.logger
||= Rails
.logger
495 # Sets +ActionController::Base#view_paths+ and +ActionMailer::Base#template_root+
496 # (but only for those frameworks that are to be loaded). If the framework's
497 # paths have already been set, it is not changed, otherwise it is
498 # set to use Configuration#view_path.
499 def initialize_framework_views
500 if configuration
.frameworks
.include?(:action_view)
501 view_path
= ActionView
::PathSet.type_cast(configuration
.view_path
)
502 ActionMailer
::Base.template_root
= view_path
if configuration
.frameworks
.include?(:action_mailer) && ActionMailer
::Base.view_paths
.blank
?
503 ActionController
::Base.view_paths
= view_path
if configuration
.frameworks
.include?(:action_controller) && ActionController
::Base.view_paths
.blank
?
507 # If Action Controller is not one of the loaded frameworks (Configuration#frameworks)
508 # this does nothing. Otherwise, it loads the routing definitions and sets up
509 # loading module used to lazily load controllers (Configuration#controller_paths).
510 def initialize_routing
511 return unless configuration
.frameworks
.include?(:action_controller)
513 ActionController
::Routing.controller_paths
+= configuration
.controller_paths
514 ActionController
::Routing::Routes.add_configuration_file(configuration
.routes_configuration_file
)
515 ActionController
::Routing::Routes.reload
!
518 # Sets the dependency loading mechanism based on the value of
519 # Configuration#cache_classes.
520 def initialize_dependency_mechanism
521 ActiveSupport
::Dependencies.mechanism
= configuration
.cache_classes
? :require : :load
524 # Loads support for "whiny nil" (noisy warnings when methods are invoked
525 # on +nil+ values) if Configuration#whiny_nils is true.
526 def initialize_whiny_nils
527 require('active_support/whiny_nil') if configuration
.whiny_nils
530 # Sets the default value for Time.zone, and turns on ActiveRecord::Base#time_zone_aware_attributes.
531 # If assigned value cannot be matched to a TimeZone, an exception will be raised.
532 def initialize_time_zone
533 if configuration
.time_zone
534 zone_default
= Time
.__send__(:get_zone, configuration
.time_zone
)
538 'Value assigned to config.time_zone not recognized.' +
539 'Run "rake -D time" for a list of tasks for finding appropriate time zone names.'
542 Time
.zone_default
= zone_default
544 if configuration
.frameworks
.include?(:active_record)
545 ActiveRecord
::Base.time_zone_aware_attributes
= true
546 ActiveRecord
::Base.default_timezone
= :utc
551 # Set the i18n configuration from config.i18n but special-case for the load_path which should be
552 # appended to what's already set instead of overwritten.
554 configuration
.i18n
.each
do |setting
, value
|
555 if setting
== :load_path
556 I18n
.load_path
+= value
558 I18n
.send("#{setting}=", value
)
564 Rails
::Rack::Metal.requested_metals
= configuration
.metals
565 Rails
::Rack::Metal.metal_paths
+= plugin_loader
.engine_metal_paths
567 configuration
.middleware
.insert_before(
568 :"ActionController::RewindableInput",
569 Rails
::Rack::Metal, :if => Rails
::Rack::Metal.metals
.any
?)
572 # Initializes framework-specific settings for each of the loaded frameworks
573 # (Configuration#frameworks). The available settings map to the accessors
574 # on each of the corresponding Base classes.
575 def initialize_framework_settings
576 configuration
.frameworks
.each
do |framework
|
577 base_class
= framework
.to_s
.camelize
.constantize
.const_get("Base")
579 configuration
.send(framework
).each
do |setting
, value
|
580 base_class
.send("#{setting}=", value
)
583 configuration
.active_support
.each
do |setting
, value
|
584 ActiveSupport
.send("#{setting}=", value
)
588 # Fires the user-supplied after_initialize block (Configuration#after_initialize)
590 if gems_dependencies_loaded
591 configuration
.after_initialize_blocks
.each
do |block
|
597 def load_application_initializers
598 if gems_dependencies_loaded
599 Dir
["#{configuration.root_path}/config/initializers/**/*.rb"].sort
.each
do |initializer
|
605 def prepare_dispatcher
606 return unless configuration
.frameworks
.include?(:action_controller)
607 require 'dispatcher' unless defined?(::Dispatcher)
608 Dispatcher
.define_dispatcher_callbacks(configuration
.cache_classes
)
609 Dispatcher
.run_prepare_callbacks
612 def disable_dependency_loading
613 if configuration
.cache_classes
&& !configuration
.dependency_loading
614 ActiveSupport
::Dependencies.unhook
!
619 # The Configuration class holds all the parameters for the Initializer and
620 # ships with defaults that suites most Rails applications. But it's possible
621 # to overwrite everything. Usually, you'll create an Configuration file
622 # implicitly through the block running on the Initializer, but it's also
623 # possible to create the Configuration instance in advance and pass it in
626 # config = Rails::Configuration.new
627 # Rails::Initializer.run(:process, config)
629 # The application's base directory.
630 attr_reader
:root_path
632 # A stub for setting options on ActionController::Base.
633 attr_accessor
:action_controller
635 # A stub for setting options on ActionMailer::Base.
636 attr_accessor
:action_mailer
638 # A stub for setting options on ActionView::Base.
639 attr_accessor
:action_view
641 # A stub for setting options on ActiveRecord::Base.
642 attr_accessor
:active_record
644 # A stub for setting options on ActiveResource::Base.
645 attr_accessor
:active_resource
647 # A stub for setting options on ActiveSupport.
648 attr_accessor
:active_support
650 # Whether to preload all frameworks at startup.
651 attr_accessor
:preload_frameworks
653 # Whether or not classes should be cached (set to false if you want
654 # application classes to be reloaded on each request)
655 attr_accessor
:cache_classes
657 # The list of paths that should be searched for controllers. (Defaults
658 # to <tt>app/controllers</tt>.)
659 attr_accessor
:controller_paths
661 # The path to the database configuration file to use. (Defaults to
662 # <tt>config/database.yml</tt>.)
663 attr_accessor
:database_configuration_file
665 # The path to the routes configuration file to use. (Defaults to
666 # <tt>config/routes.rb</tt>.)
667 attr_accessor
:routes_configuration_file
669 # The list of rails framework components that should be loaded. (Defaults
670 # to <tt>:active_record</tt>, <tt>:action_controller</tt>,
671 # <tt>:action_view</tt>, <tt>:action_mailer</tt>, and
672 # <tt>:active_resource</tt>).
673 attr_accessor
:frameworks
675 # An array of additional paths to prepend to the load path. By default,
676 # all +app+, +lib+, +vendor+ and mock paths are included in this list.
677 attr_accessor
:load_paths
679 # An array of paths from which Rails will automatically load from only once.
680 # All elements of this array must also be in +load_paths+.
681 attr_accessor
:load_once_paths
683 # An array of paths from which Rails will eager load on boot if cache
684 # classes is enabled. All elements of this array must also be in
686 attr_accessor
:eager_load_paths
688 # The log level to use for the default Rails logger. In production mode,
689 # this defaults to <tt>:info</tt>. In development mode, it defaults to
691 attr_accessor
:log_level
693 # The path to the log file to use. Defaults to log/#{environment}.log
694 # (e.g. log/development.log or log/production.log).
695 attr_accessor
:log_path
697 # The specific logger to use. By default, a logger will be created and
698 # initialized using #log_path and #log_level, but a programmer may
699 # specifically set the logger to use via this accessor and it will be
701 attr_accessor
:logger
703 # The specific cache store to use. By default, the ActiveSupport::Cache::Store will be used.
704 attr_accessor
:cache_store
706 # The root of the application's views. (Defaults to <tt>app/views</tt>.)
707 attr_accessor
:view_path
709 # Set to +true+ if you want to be warned (noisily) when you try to invoke
710 # any method of +nil+. Set to +false+ for the standard Ruby behavior.
711 attr_accessor
:whiny_nils
713 # The list of plugins to load. If this is set to <tt>nil</tt>, all plugins will
714 # be loaded. If this is set to <tt>[]</tt>, no plugins will be loaded. Otherwise,
715 # plugins will be loaded in the order specified.
717 def plugins
=(plugins
)
718 @plugins = plugins
.nil? ? nil : plugins
.map
{ |p
| p
.to_sym
}
721 # The list of metals to load. If this is set to <tt>nil</tt>, all metals will
722 # be loaded in alphabetical order. If this is set to <tt>[]</tt>, no metals will
723 # be loaded. Otherwise metals will be loaded in the order specified
724 attr_accessor
:metals
726 # The path to the root of the plugins directory. By default, it is in
727 # <tt>vendor/plugins</tt>.
728 attr_accessor
:plugin_paths
730 # The classes that handle finding the desired plugins that you'd like to load for
731 # your application. By default it is the Rails::Plugin::FileSystemLocator which finds
732 # plugins to load in <tt>vendor/plugins</tt>. You can hook into gem location by subclassing
733 # Rails::Plugin::Locator and adding it onto the list of <tt>plugin_locators</tt>.
734 attr_accessor
:plugin_locators
736 # The class that handles loading each plugin. Defaults to Rails::Plugin::Loader, but
737 # a sub class would have access to fine grained modification of the loading behavior. See
738 # the implementation of Rails::Plugin::Loader for more details.
739 attr_accessor
:plugin_loader
741 # Enables or disables plugin reloading. You can get around this setting per plugin.
742 # If <tt>reload_plugins?</tt> is false, add this to your plugin's <tt>init.rb</tt>
743 # to make it reloadable:
745 # ActiveSupport::Dependencies.load_once_paths.delete lib_path
747 # If <tt>reload_plugins?</tt> is true, add this to your plugin's <tt>init.rb</tt>
748 # to only load it once:
750 # ActiveSupport::Dependencies.load_once_paths << lib_path
752 attr_accessor
:reload_plugins
754 # Returns true if plugin reloading is enabled.
759 # Enables or disables dependency loading during the request cycle. Setting
760 # <tt>dependency_loading</tt> to true will allow new classes to be loaded
761 # during a request. Setting it to false will disable this behavior.
763 # Those who want to run in a threaded environment should disable this
764 # option and eager load or require all there classes on initialization.
766 # If <tt>cache_classes</tt> is disabled, dependency loaded will always be
768 attr_accessor
:dependency_loading
770 # An array of gems that this rails application depends on. Rails will automatically load
771 # these gems during installation, and allow you to install any missing gems with:
775 # You can add gems with the #gem method.
778 # Adds a single Gem dependency to the rails application. By default, it will require
779 # the library with the same name as the gem. Use :lib to specify a different name.
781 # # gem 'aws-s3', '>= 0.4.0'
783 # config.gem 'aws-s3', :lib => 'aws/s3', :version => '>= 0.4.0', \
784 # :source => "http://code.whytheluckystiff.net"
786 # To require a library be installed, but not attempt to load it, pass :lib => false
788 # config.gem 'qrp', :version => '0.4.1', :lib => false
789 def gem(name
, options
= {})
790 @gems << Rails
::GemDependency.new(name
, options
)
793 # Deprecated options:
794 def breakpoint_server(_
= nil)
796 *******************************************************************
797 * config.breakpoint_server has been deprecated and has no effect. *
798 *******************************************************************
801 alias_method :breakpoint_server=, :breakpoint_server
803 # Sets the default +time_zone+. Setting this will enable +time_zone+
804 # awareness for Active Record models and set the Active Record default
805 # timezone to <tt>:utc</tt>.
806 attr_accessor :time_zone
808 # Accessor for i18n settings.
811 # Create a new Configuration instance, initialized with the default
816 self.frameworks = default_frameworks
817 self.load_paths = default_load_paths
818 self.load_once_paths = default_load_once_paths
819 self.eager_load_paths = default_eager_load_paths
820 self.log_path = default_log_path
821 self.log_level = default_log_level
822 self.view_path = default_view_path
823 self.controller_paths = default_controller_paths
824 self.preload_frameworks = default_preload_frameworks
825 self.cache_classes = default_cache_classes
826 self.dependency_loading = default_dependency_loading
827 self.whiny_nils = default_whiny_nils
828 self.plugins = default_plugins
829 self.plugin_paths = default_plugin_paths
830 self.plugin_locators = default_plugin_locators
831 self.plugin_loader = default_plugin_loader
832 self.database_configuration_file = default_database_configuration_file
833 self.routes_configuration_file = default_routes_configuration_file
834 self.gems = default_gems
835 self.i18n = default_i18n
837 for framework in default_frameworks
838 self.send("#{framework}=", Rails::OrderedOptions.new)
840 self.active_support = Rails::OrderedOptions.new
843 # Set the root_path to RAILS_ROOT and canonicalize it.
845 raise 'RAILS_ROOT is not set' unless defined?(::RAILS_ROOT)
846 raise 'RAILS_ROOT is not a directory' unless File.directory?(::RAILS_ROOT)
849 # Pathname is incompatible with Windows, but Windows doesn't have
850 # real symlinks so File.expand_path is safe.
851 if RUBY_PLATFORM =~ /(:?mswin|mingw)/
852 File.expand_path(::RAILS_ROOT)
854 # Otherwise use Pathname#realpath which respects symlinks.
856 Pathname.new(::RAILS_ROOT).realpath.to_s
859 Object.const_set(:RELATIVE_RAILS_ROOT, ::RAILS_ROOT.dup) unless defined?(::RELATIVE_RAILS_ROOT)
860 ::RAILS_ROOT.replace @root_path
863 # Enable threaded mode. Allows concurrent requests to controller actions and
864 # multiple database connections. Also disables automatic dependency loading
865 # after boot, and disables reloading code on every request, as these are
866 # fundamentally incompatible with thread safety.
868 self.preload_frameworks = true
869 self.cache_classes = true
870 self.dependency_loading = false
871 self.action_controller.allow_concurrency = true
875 # Loads and returns the contents of the #database_configuration_file. The
876 # contents of the file are processed via ERB before being sent through
878 def database_configuration
880 YAML::load(ERB.new(IO.read(database_configuration_file)).result)
883 # The path to the current environment's file (<tt>development.rb</tt>, etc.). By
884 # default the file is at <tt>config/environments/#{environment}.rb</tt>.
886 "#{root_path}/config/environments/#{environment}.rb"
889 # Return the currently selected environment. By default, it returns the
890 # value of the RAILS_ENV constant.
895 # Adds a block which will be executed after rails has been fully initialized.
896 # Useful for per-environment configuration which depends on the framework being
898 def after_initialize(&after_initialize_block)
899 after_initialize_blocks << after_initialize_block if after_initialize_block
902 # Returns the blocks added with Configuration#after_initialize
903 def after_initialize_blocks
904 @after_initialize_blocks ||= []
907 # Add a preparation callback that will run before every request in development
908 # mode, or before the first request in production.
910 # See Dispatcher#to_prepare.
911 def to_prepare(&callback)
913 require 'dispatcher' unless defined?(::Dispatcher)
914 Dispatcher.to_prepare(&callback)
919 require 'action_controller'
920 ActionController::Dispatcher.middleware
923 def builtin_directories
924 # Include builtins only in the development environment.
925 (environment == 'development') ? Dir["#{RAILTIES_PATH}/builtin/*/"] : []
929 paths = %w(railties railties/lib activesupport/lib)
930 paths << 'actionpack/lib' if frameworks.include?(:action_controller) || frameworks.include?(:action_view)
932 [:active_record, :action_mailer, :active_resource, :action_web_service].each do |framework|
933 paths << "#{framework.to_s.gsub('_', '')}/lib" if frameworks.include?(framework)
936 paths.map { |dir| "#{framework_root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
940 def framework_root_path
941 defined?(::RAILS_FRAMEWORK_ROOT) ? ::RAILS_FRAMEWORK_ROOT : "#{root_path}/vendor/rails"
944 def default_frameworks
945 [ :active_record, :action_controller, :action_view, :action_mailer, :active_resource ]
948 def default_load_paths
951 # Add the old mock paths only if the directories exists
952 paths.concat(Dir["#{root_path}/test/mocks/#{environment}"]) if File.exists?("#{root_path}/test/mocks/#{environment}")
954 # Add the app's controller directory
955 paths.concat(Dir["#{root_path}/app/controllers/"])
957 # Followed by the standard includes.
967 ).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
969 paths.concat builtin_directories
972 # Doesn't matter since plugins aren't in load_paths yet.
973 def default_load_once_paths
977 def default_eager_load_paths
983 ).map { |dir| "#{root_path}/#{dir}" }.select { |dir| File.directory?(dir) }
987 File.join(root_path, 'log', "#{environment}.log")
990 def default_log_level
991 environment == 'production' ? :info : :debug
994 def default_database_configuration_file
995 File.join(root_path, 'config', 'database.yml')
998 def default_routes_configuration_file
999 File.join(root_path, 'config', 'routes.rb')
1002 def default_view_path
1003 File.join(root_path, 'app', 'views')
1006 def default_controller_paths
1007 paths = [File.join(root_path, 'app', 'controllers')]
1008 paths.concat builtin_directories
1012 def default_dependency_loading
1016 def default_preload_frameworks
1020 def default_cache_classes
1024 def default_whiny_nils
1032 def default_plugin_paths
1033 ["#{root_path}/vendor/plugins"]
1036 def default_plugin_locators
1038 locators << Plugin::GemLocator if defined? Gem
1039 locators << Plugin::FileSystemLocator
1042 def default_plugin_loader
1046 def default_cache_store
1047 if File.exist?("#{root_path}/tmp/cache/")
1048 [ :file_store, "#{root_path}/tmp/cache/" ]
1059 i18n = Rails::OrderedOptions.new
1062 if File.exist?(File.join(RAILS_ROOT, 'config', 'locales'))
1063 i18n.load_path << Dir[File.join(RAILS_ROOT, 'config', 'locales', '*.{rb,yml}')]
1064 i18n.load_path.flatten!
1072 # Needs to be duplicated from Active Support since its needed before Active
1073 # Support is available. Here both Options and Hash are namespaced to prevent
1074 # conflicts with other implementations AND with the classes residing in Active Support.
1075 class Rails::OrderedOptions < Array #:nodoc:
1079 if pair = find_pair(key)
1083 self << [key, value]
1088 pair = find_pair(key.to_sym)
1089 pair ? pair.last : nil
1092 def method_missing(name, *args)
1093 if name.to_s =~ /(.*)=$/
1094 self[$1.to_sym] = args.first
1102 self.each { |i| return i if i.first == key }