Permalink
Browse files

Extract Railtie load from application.

  • Loading branch information...
1 parent 4f03603 commit 13d66cdf2544af0d465d596383743b16b5005996 @josevalim josevalim committed Jan 23, 2010
@@ -4,6 +4,7 @@ module Rails
class Application < Engine
autoload :Bootstrap, 'rails/application/bootstrap'
autoload :Finisher, 'rails/application/finisher'
+ autoload :Railties, 'rails/application/railties'
autoload :RoutesReloader, 'rails/application/routes_reloader'
# TODO Check helpers works as expected
@@ -25,9 +26,22 @@ def original_root
end
def inherited(base)
+ # TODO Add this check
+ # raise "You cannot have more than one Rails::Application" if Rails.application
super
- Railtie.plugins.delete(base)
- Rails.application = base.instance
+
+ # TODO Add a test which ensures me
+ # Railtie.plugins.delete(base)
+ Rails.application ||= base.instance
+
+ base.rake_tasks do
+ require "rails/tasks"
+ paths.lib.tasks.to_a.sort.each { |r| load(rake) }
+ task :environment do
+ $rails_rake_task = true
+ initialize!
+ end
+ end
end
protected
@@ -38,11 +52,16 @@ def method_missing(*args, &block)
end
def initialize
- require_environment
+ environment = config.paths.config.environment.to_a.first
+ require environment if environment
end
def routes
- ActionController::Routing::Routes
+ ::ActionController::Routing::Routes
+ end
+
+ def railties
+ @railties ||= Railties.new(config)
end
def routes_reloader
@@ -58,34 +77,16 @@ def initialize!
self
end
- def require_environment
- environment = config.paths.config.environment.to_a.first
- require environment if environment
- end
-
def load_tasks
- require "rails/tasks"
- plugins.each { |p| p.load_tasks }
- # Load all application tasks
- # TODO: extract out the path to the rake tasks
- Dir["#{root}/lib/tasks/**/*.rake"].sort.each { |ext| load ext }
- task :environment do
- $rails_rake_task = true
- initialize!
- end
+ super
+ railties.all { |r| r.load_tasks }
+ self
end
def load_generators
- plugins.each { |p| p.load_generators }
- end
-
- # TODO: Fix this method. It loads all railties independent if :all is given
- # or not, otherwise frameworks are never loaded.
- def plugins
- @plugins ||= begin
- plugin_names = (config.plugins || [:all]).map { |p| p.to_sym }
- Railtie.plugins.map(&:new) + Plugin.all(plugin_names, config.paths.vendor.plugins)
- end
+ super
+ railties.all { |r| r.load_generators }
+ self
end
def app
@@ -100,7 +101,7 @@ def call(env)
def initializers
initializers = Bootstrap.initializers
initializers += super
- plugins.each { |p| initializers += p.initializers }
+ railties.all { |r| initializers += r.initializers }
initializers += Finisher.initializers
initializers
end
@@ -0,0 +1,31 @@
+module Rails
+ class Application
+ class Railties
+ # TODO Write tests
+ 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
@@ -1,17 +1,17 @@
module Rails
class Application
class RoutesReloader
- attr_reader :config
-
+ # TODO Change config.action_dispatch.route_files to config.action_dispatch.routes_path
+ # TODO Write tests
def initialize(config)
@config, @last_change_at = config, nil
end
def changed_at
routes_changed_at = nil
- config.action_dispatch.route_files.each do |config|
- config_changed_at = File.stat(config).mtime
+ files.each do |file|
+ config_changed_at = File.stat(file).mtime
if routes_changed_at.nil? || config_changed_at > routes_changed_at
routes_changed_at = config_changed_at
@@ -26,7 +26,7 @@ def reload!
routes.disable_clear_and_finalize = true
routes.clear!
- config.action_dispatch.route_files.each { |config| load(config) }
+ files.each { |file| load(file) }
routes.finalize!
nil
@@ -41,6 +41,10 @@ def reload_if_changed
reload!
end
end
+
+ def files
+ @config.action_dispatch.route_files
+ end
end
end
end
@@ -115,6 +115,7 @@ def config_key_regexp
/^(#{bits})(?:=)?$/
end
+ # TODO Fix me
def config_keys
Railtie.plugin_names.map { |n| n.to_s }.uniq
end
@@ -182,6 +183,7 @@ def paths
paths = super
paths.app.controllers << builtin_controller if builtin_controller
paths.config.database "config/database.yml"
+ paths.lib.tasks "lib/tasks", :glob => "**/*.rake"
paths.log "log/#{Rails.env}.log"
paths.tmp "tmp"
paths.tmp.cache "tmp/cache"
@@ -4,6 +4,7 @@ module Rails
class Engine < Railtie
class << self
attr_accessor :called_from
+ delegate :middleware, :root, :paths, :to => :config
def original_root
@original_root ||= find_root_with_file_flag("lib")
@@ -18,7 +19,6 @@ def inherited(base)
call_stack = caller.map { |p| p.split(':').first }
File.dirname(call_stack.detect { |p| p !~ %r[railties/lib/rails|rack/lib/rack] })
end
-
super
end
@@ -33,17 +33,14 @@ def find_root_with_file_flag(flag, default=nil)
end
root = File.exist?("#{root_path}/#{flag}") ? root_path : default
-
raise "Could not find root path for #{self}" unless root
RUBY_PLATFORM =~ /(:?mswin|mingw)/ ?
- Pathname.new(root).expand_path :
- Pathname.new(root).realpath
+ Pathname.new(root).expand_path : Pathname.new(root).realpath
end
end
- delegate :config, :to => :'self.class'
- delegate :middleware, :root, :to => :config
+ delegate :middleware, :paths, :root, :config, :to => :'self.class'
# Add configured load paths to ruby load paths and remove duplicates.
initializer :set_load_path do
@@ -5,26 +5,30 @@ class Railtie
ABSTRACT_RAILTIES = %w(Rails::Plugin Rails::Engine Rails::Application)
class << self
+ attr_reader :subclasses
+
def abstract_railtie?(base)
ABSTRACT_RAILTIES.include?(base.name)
end
def inherited(base)
- @@plugins ||= []
- @@plugins << base unless abstract_railtie?(base)
+ @subclasses ||= []
+ @subclasses << base unless abstract_railtie?(base)
end
- # This should be called railtie_name and engine_name
+ # TODO This should be called railtie_name and engine_name
def plugin_name(plugin_name = nil)
@plugin_name ||= name.demodulize.underscore
@plugin_name = plugin_name if plugin_name
@plugin_name
end
+ # TODO Deprecate me
def plugins
- @@plugins
+ @subclasses
end
+ # TODO Deprecate me
def plugin_names
plugins.map { |p| p.plugin_name }
end
@@ -59,12 +63,10 @@ def generators
end
def load_tasks
- return unless rake_tasks
rake_tasks.each { |blk| blk.call }
end
def load_generators
- return unless generators
generators.each { |blk| blk.call }
end
end

0 comments on commit 13d66cd

Please sign in to comment.