Permalink
Browse files

Have script/* and Rakefile use the application object

  • Loading branch information...
1 parent a2cb90c commit 530b8ff5ae811e0dd5e1343b478f42eed6fffdbd Carlhuda committed Nov 24, 2009
@@ -3,11 +3,6 @@ class Application
include Initializable
class << self
- def inherited(klass)
- Rails.application ||= klass unless klass.name =~ /Rails/
- super
- end
-
# Stub out App initialize
def initialize!
new
@@ -32,19 +27,32 @@ def root
config.root
end
+ def load_tasks
+ require "rails/tasks"
+ task :environment do
+ $rails_rake_task = true
+ initialize!
+ end
+ end
+
def call(env)
new.call(env)
end
end
def initialize
+ Rails.application ||= self
run_initializers(self)
end
def config
self.class.config
end
+ def root
+ config.root
+ end
+
alias configuration config
def middleware
@@ -6,8 +6,12 @@ module Rails
class Console
ENVIRONMENTS = %w(production development test)
- def self.start
- new.start
+ def self.start(app)
+ new(app).start
+ end
+
+ def initialize(app)
+ @app = app
end
def start
@@ -25,7 +29,7 @@ def start
ENV['RAILS_ENV'] = ENVIRONMENTS.find { |e| e.index(env) } || env
end
- require "#{Rails.root}/config/environment"
+ @app.initialize!
require "rails/console_app"
require "rails/console_sandbox" if options[:sandbox]
require "rails/console_with_helpers"
@@ -4,8 +4,12 @@
module Rails
class DBConsole
- def self.start
- new.start
+ def self.start(app)
+ new(app).start
+ end
+
+ def initialize(app)
+ @app = app
end
def start
@@ -31,7 +35,7 @@ def start
end
env = ARGV.first || ENV['RAILS_ENV'] || 'development'
- unless config = YAML::load(ERB.new(IO.read("#{Rails.root}/config/database.yml")).result)[env]
+ unless config = YAML::load(ERB.new(IO.read("#{@app.root}/config/database.yml")).result)[env]
abort "No database is configured for the environment '#{env}'"
end
@@ -37,6 +37,15 @@ def opt_parser
Options.new
end
+ def self.start(app)
+ new(app).start
+ end
+
+ def initialize(app)
+ super() # Call Rack::Server#initialize without passing any options to use.
+ @app = app
+ end
+
def start
puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
puts "=> Rails #{Rails.version} application starting on http://#{options[:Host]}:#{options[:Port]}"
@@ -54,20 +63,24 @@ def start
def middleware
middlewares = []
- middlewares << [Rails::Rack::LogTailer] unless options[:daemonize]
+ middlewares << [Rails::Rack::LogTailer, log_path] unless options[:daemonize]
middlewares << [Rails::Rack::Debugger] if options[:debugger]
Hash.new(middlewares)
end
+ def log_path
+ "#{File.expand_path(@app.root)}/log/#{options[:environment]}.log"
+ end
+
def default_options
{
:Port => 3000,
:Host => "0.0.0.0",
:environment => (ENV['RAILS_ENV'] || "development").dup,
- :rack_file => "#{Rails.root}/config.ru",
+ :rack_file => "#{@app.root}/config.ru",
:daemonize => false,
:debugger => false,
- :pid => "#{Rails.root}/tmp/pids/server.pid",
+ :pid => "#{@app.root}/tmp/pids/server.pid",
:AccessLog => []
}
end
@@ -48,9 +48,9 @@ def create_root
end
def create_root_files
- copy_file "Rakefile"
copy_file "README"
- copy_file "config.ru"
+ template "Rakefile"
+ template "config.ru"
template "Gemfile"
end
@@ -181,6 +181,10 @@ def app_name
@app_name ||= File.basename(destination_root)
end
+ def app_const
+ @app_const ||= app_name.classify
+ end
+
def app_secret
ActiveSupport::SecureRandom.hex(64)
end
@@ -7,4 +7,4 @@ require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
-require 'rails/tasks'
+<%= app_const %>.load_tasks
@@ -2,4 +2,4 @@
require ::File.expand_path('../config/environment', __FILE__)
# Dispatch the request
-run Rails.application
+run <%= app_const%>
@@ -1,6 +1,6 @@
require File.expand_path('../boot', __FILE__)
-Rails::Initializer.run do |config|
+class <%= app_const %> < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
@@ -2,4 +2,4 @@
require File.expand_path('../application', __FILE__)
# Initialize the rails application
-Rails.initialize!
+<%= app_const %>.initialize!
@@ -1,3 +1,3 @@
require File.expand_path('../../config/application', __FILE__)
require 'rails/commands/console'
-Rails::Console.start
+Rails::Console.start(<%= app_const %>)
@@ -1,3 +1,3 @@
require File.expand_path('../../config/application', __FILE__)
require 'rails/commands/dbconsole'
-Rails::DBConsole.start
+Rails::DBConsole.start(<%= app_const %>)
@@ -1,3 +1,3 @@
require File.expand_path('../../config/application', __FILE__)
require 'rails/commands/server'
-Rails::Server.start
+Rails::Server.start(<%= app_const %>)
@@ -1,12 +1,10 @@
module Rails
module Rack
class LogTailer
- EnvironmentLog = "#{File.expand_path(Rails.root)}/log/#{Rails.env}.log"
-
def initialize(app, log = nil)
@app = app
- path = Pathname.new(log || EnvironmentLog).cleanpath
+ path = Pathname.new(log || "#{File.expand_path(Rails.root)}/log/#{Rails.env}.log").cleanpath
@cursor = ::File.size(path)
@last_checked = Time.now.to_f
@@ -1,8 +1,4 @@
task :default => :test
-task :environment do
- $rails_rake_task = true
- require(File.join(Rails.root, 'config', 'environment'))
-end
task :rails_env do
unless defined? RAILS_ENV
@@ -7,21 +7,8 @@ class LoadTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation
def rackup
- config = "#{app_path}/config.ru"
- # Copied from ActionDispatch::Utils.parse_config
- # ActionDispatch is not necessarily available at this point.
- require 'rack'
- if config =~ /\.ru$/
- cfgfile = ::File.read(config)
- if cfgfile[/^#\\(.*)/]
- opts.parse! $1.split(/\s+/)
- end
- inner_app = eval "Rack::Builder.new {( " + cfgfile + "\n )}.to_app",
- nil, config
- else
- require config
- inner_app = Object.const_get(::File.basename(config, '.rb').capitalize)
- end
+ require "rack"
+ Rack::Builder.parse_file("#{app_path}/config.ru")
end
def setup
@@ -40,14 +27,14 @@ def setup
test "Rails.application is available after config.ru has been racked up" do
rackup
- assert Rails.application < Rails::Application
+ assert Rails.application.is_a?(Rails::Application)
end
# Passenger still uses AC::Dispatcher, so we need to
# keep it working for now
test "deprecated ActionController::Dispatcher still works" do
rackup
- assert ActionController::Dispatcher.new < Rails::Application
+ assert ActionController::Dispatcher.new.is_a?(Rails::Application)
end
test "the config object is available on the application object" do

0 comments on commit 530b8ff

Please sign in to comment.