aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails
diff options
context:
space:
mode:
authorCarl Lerche <carllerche@mac.com>2009-10-07 15:21:19 -0700
committerCarl Lerche <carllerche@mac.com>2009-10-08 12:31:09 -0700
commit6d6ae0841c94d3c0ce0c91311028ff7396c44a4a (patch)
tree14030762454f1afffa747fc6202498353c0337d9 /railties/lib/rails
parentd0965892456e0ec76f9cb95a151d3b8e11622e36 (diff)
downloadrails-6d6ae0841c94d3c0ce0c91311028ff7396c44a4a.tar.gz
rails-6d6ae0841c94d3c0ce0c91311028ff7396c44a4a.tar.bz2
rails-6d6ae0841c94d3c0ce0c91311028ff7396c44a4a.zip
Start moving the initializers into the application object
Diffstat (limited to 'railties/lib/rails')
-rw-r--r--railties/lib/rails/application.rb38
-rw-r--r--railties/lib/rails/generators/rails/app/templates/config/boot.rb13
-rw-r--r--railties/lib/rails/initializable.rb70
-rw-r--r--railties/lib/rails/initializer.rb42
4 files changed, 111 insertions, 52 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb
index 6139e20e95..783d45aa65 100644
--- a/railties/lib/rails/application.rb
+++ b/railties/lib/rails/application.rb
@@ -1,5 +1,10 @@
module Rails
class Application
+ extend Initializable
+
+ def self.inherited(child)
+ child.initializers = initializers.dup
+ end
def self.config
@config ||= Configuration.new
@@ -9,21 +14,40 @@ module Rails
@config = config
end
- def config
- self.class.config
- end
-
- def routes
+ def self.routes
ActionController::Routing::Routes
end
- def middleware
+ def self.middleware
config.middleware
end
- def call(env)
+ def self.call(env)
@app ||= middleware.build(routes)
@app.call(env)
end
+
+ def self.new
+ initializers.run
+ self
+ end
+
+ initializer :initialize_rails do
+ Rails.initializers.run
+ end
+
+ # Set the <tt>$LOAD_PATH</tt> based on the value of
+ # Configuration#load_paths. Duplicates are removed.
+ initializer :set_load_path do
+ config.paths.add_to_load_path
+ $LOAD_PATH.uniq!
+ end
+
+ # Bail if boot.rb is outdated
+ initializer :freak_out_if_boot_rb_is_outdated do
+ unless defined?(Rails::BOOTSTRAP_VERSION)
+ abort %{Your config/boot.rb is outdated: Run "rake rails:update".}
+ end
+ end
end
end
diff --git a/railties/lib/rails/generators/rails/app/templates/config/boot.rb b/railties/lib/rails/generators/rails/app/templates/config/boot.rb
index 52086fbc7d..6e0e2279cd 100644
--- a/railties/lib/rails/generators/rails/app/templates/config/boot.rb
+++ b/railties/lib/rails/generators/rails/app/templates/config/boot.rb
@@ -40,20 +40,20 @@ module Rails
class Boot
def run
- load_initializer
set_load_paths
+ load_initializer
end
def set_load_paths
%w(
- railties
- railties/lib
- activesupport/lib
+ actionmailer/lib
actionpack/lib
+ activemodel/lib
activerecord/lib
- actionmailer/lib
activeresource/lib
- actionwebservice/lib
+ activesupport/lib
+ railties/lib
+ railties
).reverse_each do |path|
path = "#{framework_root_path}/#{path}"
$LOAD_PATH.unshift(path) if File.directory?(path)
@@ -68,7 +68,6 @@ module Rails
class VendorBoot < Boot
def load_initializer
- $:.unshift("#{framework_root_path}/railties/lib")
require "rails"
install_gem_spec_stubs
Rails::GemDependency.add_frozen_gem_path
diff --git a/railties/lib/rails/initializable.rb b/railties/lib/rails/initializable.rb
new file mode 100644
index 0000000000..61c98d4f99
--- /dev/null
+++ b/railties/lib/rails/initializable.rb
@@ -0,0 +1,70 @@
+module Rails
+ module Initializable
+
+ # A collection of initializers
+ class Collection < ActiveSupport::OrderedHash
+ # def initialize_copy(other)
+ # super
+ # each do |key, value|
+ # self[key] = value.dup
+ # end
+ # end
+
+ def run
+ each do |key, initializer|
+ initializer.run
+ end
+ self
+ end
+ end
+
+ class Initializer
+ attr_reader :name, :options, :block
+
+ def initialize(name, options = {}, &block)
+ @name, @options, @block = name, options, block
+ end
+
+ def run
+ return if @already_ran
+ @block.call
+ @already_ran = true
+ end
+ end
+
+ def initializer(name, options = {}, &block)
+ initializers[name] = Initializer.new(name, options, &block)
+ end
+
+ def initializers
+ @initializers ||= Collection.new
+ end
+
+ def initializers=(initializers)
+ @initializers = initializers
+ end
+
+ end
+
+ extend Initializable
+
+ # Check for valid Ruby version (1.8.2 or 1.8.4 or higher). This is done in an
+ # external file, so we can use it from the `rails` program as well without duplication.
+ initializer :check_ruby_version do
+ require 'rails/ruby_version_check'
+ end
+
+ # For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
+ # multibyte safe operations. Plugin authors supporting other encodings
+ # should override this behaviour and set the relevant +default_charset+
+ # on ActionController::Base.
+ #
+ # For Ruby 1.9, UTF-8 is the default internal and external encoding.
+ initializer :initialize_encoding do
+ if RUBY_VERSION < '1.9'
+ $KCODE='u'
+ else
+ Encoding.default_external = Encoding::UTF_8
+ end
+ end
+end \ No newline at end of file
diff --git a/railties/lib/rails/initializer.rb b/railties/lib/rails/initializer.rb
index c2d6e1609f..d3e7f934ea 100644
--- a/railties/lib/rails/initializer.rb
+++ b/railties/lib/rails/initializer.rb
@@ -1,5 +1,7 @@
require "pathname"
+require 'active_support/ordered_hash'
+require 'rails/initializable'
require 'rails/application'
require 'rails/railties_path'
require 'rails/version'
@@ -12,10 +14,6 @@ require 'rails/configuration'
RAILS_ENV = (ENV['RAILS_ENV'] || 'development').dup unless defined?(RAILS_ENV)
module Rails
- # Sanity check to make sure this file is only loaded once
- # TODO: Get to the point where this can be removed.
- raise "It looks like initializer.rb was required twice" if defined?(Initializer)
-
class Initializer
class Error < StandardError ; end
@@ -110,6 +108,8 @@ module Rails
default.run(initializer)
else
Rails.application = Class.new(Application)
+ # Trigger the initializer
+ Rails.application.new
yield Rails.application.config if block_given?
default.config = Rails.application.config
default.run
@@ -117,26 +117,6 @@ module Rails
end
end
- # Check for valid Ruby version (1.8.2 or 1.8.4 or higher). This is done in an
- # external file, so we can use it from the `rails` program as well without duplication.
- Initializer.default.add :check_ruby_version do
- require 'rails/ruby_version_check'
- end
-
- # Bail if boot.rb is outdated
- Initializer.default.add :freak_out_if_boot_rb_is_outdated do
- unless defined?(Rails::BOOTSTRAP_VERSION)
- abort %{Your config/boot.rb is outdated: Run "rake rails:update".}
- end
- end
-
- # Set the <tt>$LOAD_PATH</tt> based on the value of
- # Configuration#load_paths. Duplicates are removed.
- Initializer.default.add :set_load_path do
- configuration.paths.add_to_load_path
- $LOAD_PATH.uniq!
- end
-
# Requires all frameworks specified by the Configuration#frameworks
# list. By default, all frameworks (Active Record, Active Support,
# Action Pack, Action Mailer, and Active Resource) are loaded.
@@ -230,20 +210,6 @@ module Rails
end
end
- # For Ruby 1.8, this initialization sets $KCODE to 'u' to enable the
- # multibyte safe operations. Plugin authors supporting other encodings
- # should override this behaviour and set the relevant +default_charset+
- # on ActionController::Base.
- #
- # For Ruby 1.9, UTF-8 is the default internal and external encoding.
- Initializer.default.add :initialize_encoding do
- if RUBY_VERSION < '1.9'
- $KCODE='u'
- else
- Encoding.default_external = Encoding::UTF_8
- end
- end
-
# This initialization routine does nothing unless <tt>:active_record</tt>
# is one of the frameworks to load (Configuration#frameworks). If it is,
# this sets the database configuration from Configuration#database_configuration