aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/application
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib/rails/application')
-rw-r--r--railties/lib/rails/application/bootstrap.rb81
-rw-r--r--railties/lib/rails/application/configuration.rb85
-rw-r--r--railties/lib/rails/application/finisher.rb43
-rw-r--r--railties/lib/rails/application/railties.rb31
-rw-r--r--railties/lib/rails/application/routes_reloader.rb49
5 files changed, 289 insertions, 0 deletions
diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb
new file mode 100644
index 0000000000..3c339ffc57
--- /dev/null
+++ b/railties/lib/rails/application/bootstrap.rb
@@ -0,0 +1,81 @@
+module Rails
+ class Application
+ module Bootstrap
+ include Initializable
+
+ initializer :load_environment_config do |app|
+ app.require_environment!
+ end
+
+ initializer :load_all_active_support do |app|
+ require "active_support/all" unless app.config.active_support.bare
+ end
+
+ # Preload all frameworks specified by the Configuration#frameworks.
+ # Used by Passenger to ensure everything's loaded before forking and
+ # to avoid autoload race conditions in JRuby.
+ initializer :preload_frameworks do |app|
+ require 'active_support/dependencies'
+ ActiveSupport::Autoload.eager_autoload! if app.config.preload_frameworks
+ end
+
+ # Initialize the logger early in the stack in case we need to log some deprecation.
+ initializer :initialize_logger do |app|
+ Rails.logger ||= app.config.logger || begin
+ path = app.config.paths.log.to_a.first
+ logger = ActiveSupport::BufferedLogger.new(path)
+ logger.level = ActiveSupport::BufferedLogger.const_get(app.config.log_level.to_s.upcase)
+ logger.auto_flushing = false if Rails.env.production?
+ logger
+ rescue StandardError => e
+ logger = ActiveSupport::BufferedLogger.new(STDERR)
+ logger.level = ActiveSupport::BufferedLogger::WARN
+ logger.warn(
+ "Rails Error: Unable to access log file. Please ensure that #{path} exists and is chmod 0666. " +
+ "The log level has been raised to WARN and the output directed to STDERR until the problem is fixed."
+ )
+ logger
+ end
+ end
+
+ # Initialize cache early in the stack so railties can make use of it.
+ initializer :initialize_cache do |app|
+ unless defined?(RAILS_CACHE)
+ silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(app.config.cache_store) }
+
+ if RAILS_CACHE.respond_to?(:middleware)
+ app.config.middleware.insert_after(:"Rack::Lock", RAILS_CACHE.middleware)
+ end
+ end
+ end
+
+ # Initialize rails subscriber on top of notifications.
+ initializer :initialize_subscriber do |app|
+ require 'active_support/notifications'
+
+ if app.config.colorize_logging == false
+ Rails::Subscriber.colorize_logging = false
+ app.config.generators.colorize_logging = false
+ end
+
+ ActiveSupport::Notifications.subscribe do |*args|
+ Rails::Subscriber.dispatch(args)
+ end
+ end
+
+ initializer :set_clear_dependencies_hook do |app|
+ unless app.config.cache_classes
+ ActionDispatch::Callbacks.after do
+ ActiveSupport::Dependencies.clear
+ end
+ end
+ end
+
+ # Sets the dependency loading mechanism.
+ # TODO: Remove files from the $" and always use require.
+ initializer :initialize_dependency_mechanism do |app|
+ ActiveSupport::Dependencies.mechanism = app.config.cache_classes ? :require : :load
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/railties/lib/rails/application/configuration.rb b/railties/lib/rails/application/configuration.rb
new file mode 100644
index 0000000000..aaf18b5f51
--- /dev/null
+++ b/railties/lib/rails/application/configuration.rb
@@ -0,0 +1,85 @@
+require 'rails/engine/configuration'
+
+module Rails
+ class Application
+ class Configuration < ::Rails::Engine::Configuration
+ include ::Rails::Configuration::Deprecated
+
+ attr_accessor :cache_classes, :cache_store, :colorize_logging,
+ :consider_all_requests_local, :dependency_loading,
+ :filter_parameters, :log_level, :logger, :metals,
+ :plugins, :preload_frameworks, :reload_plugins,
+ :serve_static_assets, :time_zone, :whiny_nils
+
+ def initialize(*)
+ super
+ @filter_parameters = []
+ @dependency_loading = true
+ @serve_static_assets = true
+ end
+
+ def paths
+ @paths ||= begin
+ paths = super
+ paths.app.controllers << builtin_controller if builtin_controller
+ paths.config.database "config/database.yml"
+ paths.log "log/#{Rails.env}.log"
+ paths.tmp "tmp"
+ paths.tmp.cache "tmp/cache"
+ paths.vendor "vendor", :load_path => true
+ paths.vendor.plugins "vendor/plugins"
+
+ if File.exists?("#{root}/test/mocks/#{Rails.env}")
+ ActiveSupport::Deprecation.warn "\"RAILS_ROOT/test/mocks/#{Rails.env}\" won't be added " <<
+ "automatically to load paths anymore in future releases"
+ paths.mocks_path "test/mocks", :load_path => true, :glob => Rails.env
+ end
+
+ paths
+ end
+ 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.
+ def threadsafe!
+ self.preload_frameworks = true
+ self.cache_classes = true
+ self.dependency_loading = false
+ self.action_controller.allow_concurrency = true if respond_to?(:action_controller)
+ self
+ 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.
+ def database_configuration
+ require 'erb'
+ YAML::load(ERB.new(IO.read(paths.config.database.to_a.first)).result)
+ end
+
+ def cache_store
+ @cache_store ||= begin
+ if File.exist?("#{root}/tmp/cache/")
+ [ :file_store, "#{root}/tmp/cache/" ]
+ else
+ :memory_store
+ end
+ end
+ end
+
+ def builtin_controller
+ File.join(RAILTIES_PATH, "builtin", "rails_info") if Rails.env.development?
+ end
+
+ def log_level
+ @log_level ||= Rails.env.production? ? :info : :debug
+ end
+
+ def time_zone
+ @time_zone ||= "UTC"
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/railties/lib/rails/application/finisher.rb b/railties/lib/rails/application/finisher.rb
new file mode 100644
index 0000000000..5cc5b4ae88
--- /dev/null
+++ b/railties/lib/rails/application/finisher.rb
@@ -0,0 +1,43 @@
+module Rails
+ class Application
+ module Finisher
+ include Initializable
+
+ initializer :ensure_load_once_paths_as_subset do
+ extra = ActiveSupport::Dependencies.load_once_paths -
+ ActiveSupport::Dependencies.load_paths
+
+ unless extra.empty?
+ abort <<-end_error
+ load_once_paths must be a subset of the load_paths.
+ Extra items in load_once_paths: #{extra * ','}
+ end_error
+ end
+ end
+
+ initializer :add_builtin_route do |app|
+ if Rails.env.development?
+ Rails::Application::RoutesReloader.paths << File.join(RAILTIES_PATH, 'builtin', 'routes.rb')
+ end
+ end
+
+ initializer :build_middleware_stack do |app|
+ app.app
+ end
+
+ # Fires the user-supplied after_initialize block (config#after_initialize)
+ initializer :after_initialize do |app|
+ app.config.after_initialize_blocks.each do |block|
+ block.call(app)
+ end
+ end
+
+ # Disable dependency loading during request cycle
+ initializer :disable_dependency_loading do |app|
+ if app.config.cache_classes && !app.config.dependency_loading
+ ActiveSupport::Dependencies.unhook!
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/railties/lib/rails/application/railties.rb b/railties/lib/rails/application/railties.rb
new file mode 100644
index 0000000000..b3e6693f89
--- /dev/null
+++ b/railties/lib/rails/application/railties.rb
@@ -0,0 +1,31 @@
+module Rails
+ class Application
+ class Railties
+ # TODO Write tests for this behavior extracted from Application
+ def initialize(config)
+ @config = config
+ end
+
+ def all(&block)
+ @all ||= railties + engines + plugins
+ @all.each(&block) if block
+ @all
+ end
+
+ def railties
+ @railties ||= ::Rails::Railtie.subclasses.map(&:new)
+ end
+
+ def engines
+ @engines ||= ::Rails::Engine.subclasses.map(&:new)
+ end
+
+ def plugins
+ @plugins ||= begin
+ plugin_names = (@config.plugins || [:all]).map { |p| p.to_sym }
+ Plugin.all(plugin_names, @config.paths.vendor.plugins)
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/railties/lib/rails/application/routes_reloader.rb b/railties/lib/rails/application/routes_reloader.rb
new file mode 100644
index 0000000000..fe0cfb7801
--- /dev/null
+++ b/railties/lib/rails/application/routes_reloader.rb
@@ -0,0 +1,49 @@
+module Rails
+ class Application
+ # TODO Write tests for this behavior extracted from Application
+ class RoutesReloader
+ def self.paths
+ @paths ||= []
+ end
+
+ def initialize(config)
+ @config, @last_change_at = config, nil
+ end
+
+ def changed_at
+ routes_changed_at = nil
+
+ self.class.paths.each do |path|
+ config_changed_at = File.stat(path).mtime
+
+ if routes_changed_at.nil? || config_changed_at > routes_changed_at
+ routes_changed_at = config_changed_at
+ end
+ end
+
+ routes_changed_at
+ end
+
+ def reload!
+ routes = Rails::Application.routes
+ routes.disable_clear_and_finalize = true
+
+ routes.clear!
+ self.class.paths.each { |path| load(path) }
+ routes.finalize!
+
+ nil
+ ensure
+ routes.disable_clear_and_finalize = false
+ end
+
+ def reload_if_changed
+ current_change_at = changed_at
+ if @last_change_at != current_change_at
+ @last_change_at = current_change_at
+ reload!
+ end
+ end
+ end
+ end
+end \ No newline at end of file