lib/rails/application.rb in railties-4.1.16 vs lib/rails/application.rb in railties-4.2.0.beta1

- old
+ new

@@ -85,13 +85,21 @@ autoload :RoutesReloader, 'rails/application/routes_reloader' class << self def inherited(base) super - base.instance + Rails.app_class = base end + def instance + super.run_load_hooks! + end + + def create(initial_variable_values = {}, &block) + new(initial_variable_values, &block).run_load_hooks! + end + # Makes the +new+ method public. # # Note that Rails::Application inherits from Rails::Engine, which # inherits from Rails::Railtie and the +new+ method on Rails::Railtie is # private @@ -114,30 +122,39 @@ @routes_reloader = nil @app_env_config = nil @ordered_railties = nil @railties = nil @message_verifiers = {} + @ran_load_hooks = false - Rails.application ||= self + # are these actually used? + @initial_variable_values = initial_variable_values + @block = block add_lib_to_load_path! + end + + # Returns true if the application is initialized. + def initialized? + @initialized + end + + def run_load_hooks! # :nodoc: + return self if @ran_load_hooks + @ran_load_hooks = true ActiveSupport.run_load_hooks(:before_configuration, self) - initial_variable_values.each do |variable_name, value| + @initial_variable_values.each do |variable_name, value| if INITIAL_VARIABLES.include?(variable_name) instance_variable_set("@#{variable_name}", value) end end - instance_eval(&block) if block_given? + instance_eval(&@block) if @block + self end - # Returns true if the application is initialized. - def initialized? - @initialized - end - # Implements call according to the Rack API. It simply # dispatches the request to the underlying middleware stack. def call(env) env["ORIGINAL_FULLPATH"] = build_original_fullpath(env) env["ORIGINAL_SCRIPT_NAME"] = env["SCRIPT_NAME"] @@ -151,18 +168,17 @@ # Return the application's KeyGenerator def key_generator # number of iterations selected based on consultation with the google security # team. Details at https://github.com/rails/rails/pull/6952#issuecomment-7661220 - @caching_key_generator ||= begin + @caching_key_generator ||= if secrets.secret_key_base key_generator = ActiveSupport::KeyGenerator.new(secrets.secret_key_base, iterations: 1000) ActiveSupport::CachingKeyGenerator.new(key_generator) else - ActiveSupport::LegacyKeyGenerator.new(secrets.secret_token) + ActiveSupport::LegacyKeyGenerator.new(config.secret_token) end - end end # Returns a message verifier object. # # This verifier can be used to generate and verify signed messages in the application. @@ -186,31 +202,64 @@ secret = key_generator.generate_key(verifier_name.to_s) ActiveSupport::MessageVerifier.new(secret) end end + # Convenience for loading config/foo.yml for the current Rails env. + # + # Example: + # + # # config/exception_notification.yml: + # production: + # url: http://127.0.0.1:8080 + # namespace: my_app_production + # development: + # url: http://localhost:3001 + # namespace: my_app_development + # + # # config/production.rb + # MyApp::Application.configure do + # config.middleware.use ExceptionNotifier, config_for(:exception_notification) + # end + def config_for(name) + yaml = Pathname.new("#{paths["config"].existent.first}/#{name}.yml") + + if yaml.exist? + require "yaml" + require "erb" + (YAML.load(ERB.new(yaml.read).result) || {})[Rails.env] || {} + else + raise "Could not load configuration. No such file - #{yaml}" + end + rescue Psych::SyntaxError => e + raise "YAML syntax error occurred while parsing #{yaml}. " \ + "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \ + "Error: #{e.message}" + end + # Stores some of the Rails initial environment parameters which # will be used by middlewares and engines to configure themselves. def env_config @app_env_config ||= begin validate_secret_key_config! super.merge({ "action_dispatch.parameter_filter" => config.filter_parameters, "action_dispatch.redirect_filter" => config.filter_redirect, - "action_dispatch.secret_token" => secrets.secret_token, + "action_dispatch.secret_token" => config.secret_token, "action_dispatch.secret_key_base" => secrets.secret_key_base, "action_dispatch.show_exceptions" => config.action_dispatch.show_exceptions, "action_dispatch.show_detailed_exceptions" => config.consider_all_requests_local, "action_dispatch.logger" => Rails.logger, "action_dispatch.backtrace_cleaner" => Rails.backtrace_cleaner, "action_dispatch.key_generator" => key_generator, "action_dispatch.http_auth_salt" => config.action_dispatch.http_auth_salt, "action_dispatch.signed_cookie_salt" => config.action_dispatch.signed_cookie_salt, "action_dispatch.encrypted_cookie_salt" => config.action_dispatch.encrypted_cookie_salt, "action_dispatch.encrypted_signed_cookie_salt" => config.action_dispatch.encrypted_signed_cookie_salt, - "action_dispatch.cookies_serializer" => config.action_dispatch.cookies_serializer + "action_dispatch.cookies_serializer" => config.action_dispatch.cookies_serializer, + "action_dispatch.cookies_digest" => config.action_dispatch.cookies_digest }) end end # If you try to define a set of rake tasks on the instance, these will get @@ -314,25 +363,11 @@ def config=(configuration) #:nodoc: @config = configuration end - # Returns secrets added to config/secrets.yml. - # - # Example: - # - # development: - # secret_key_base: 836fa3665997a860728bcb9e9a1e704d427cfc920e79d847d79c8a9a907b9e965defa4154b2b86bdec6930adbe33f21364523a6f6ce363865724549fdfc08553 - # test: - # secret_key_base: 5a37811464e7d378488b0f073e2193b093682e4e21f5d6f3ae0a4e1781e61a351fdc878a843424e81c73fb484a40d23f92c8dafac4870e74ede6e5e174423010 - # production: - # secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> - # namespace: my_app_production - # - # +Rails.application.secrets.namespace+ returns +my_app_production+ in the - # production environment. - def secrets + def secrets #:nodoc: @secrets ||= begin secrets = ActiveSupport::OrderedOptions.new yaml = config.paths["config/secrets"].first if File.exist?(yaml) require "erb" @@ -341,12 +376,10 @@ secrets.merge!(env_secrets.symbolize_keys) if env_secrets end # Fallback to config.secret_key_base if secrets.secret_key_base isn't set secrets.secret_key_base ||= config.secret_key_base - # Fallback to config.secret_token if secrets.secret_token isn't set - secrets.secret_token ||= config.secret_token secrets end end @@ -379,18 +412,12 @@ end end end end - # Return an array of railties respecting the order they're loaded - # and the order specified by the +railties_order+ config. - # - # While when running initializers we need engines in reverse - # order here when copying migrations from railties we need then in the same - # order as given by +railties_order+ def migration_railties # :nodoc: - ordered_railties.flatten - [self] + (ordered_railties & railties_without_main_app).reverse end protected alias :build_middleware_stack :app @@ -419,10 +446,15 @@ def run_console_blocks(app) #:nodoc: railties.each { |r| r.run_console_blocks(app) } super end + def railties_without_main_app # :nodoc: + @railties_without_main_app ||= Rails::Railtie.subclasses.map(&:instance) + + Rails::Engine.subclasses.map(&:instance) + end + # Returns the ordered railties for this application considering railties_order. def ordered_railties #:nodoc: @ordered_railties ||= begin order = config.railties_order.map do |railtie| if railtie == :main_app @@ -438,17 +470,17 @@ all.push(self) unless (all + order).include?(self) order.push(:all) unless order.include?(:all) index = order.index(:all) order[index] = all - order + order.reverse.flatten end end def railties_initializers(current) #:nodoc: initializers = [] - ordered_railties.reverse.flatten.each do |r| + ordered_railties.each do |r| if r == self initializers += current else initializers += r.initializers end @@ -472,16 +504,11 @@ "#{script_name}#{path_info}" end end def validate_secret_key_config! #:nodoc: - if secrets.secret_key_base.blank? - ActiveSupport::Deprecation.warn "You didn't set `secret_key_base`. " + - "Read the upgrade documentation to learn more about this new config option." - - if secrets.secret_token.blank? - raise "Missing `secret_token` and `secret_key_base` for '#{Rails.env}' environment, set these values in `config/secrets.yml`" - end + if secrets.secret_key_base.blank? && config.secret_token.blank? + raise "Missing `secret_key_base` for '#{Rails.env}' environment, set this value in `config/secrets.yml`" end end end end