Skip to content

Commit

Permalink
Have script/* and Rakefile use the application object
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlhuda authored and Carlhuda committed Nov 24, 2009
1 parent a2cb90c commit 530b8ff
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 47 deletions.
18 changes: 13 additions & 5 deletions railties/lib/rails/application.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 7 additions & 3 deletions railties/lib/rails/commands/console.rb
Expand Up @@ -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
Expand All @@ -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"
Expand Down
10 changes: 7 additions & 3 deletions railties/lib/rails/commands/dbconsole.rb
Expand Up @@ -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
Expand All @@ -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

Expand Down
19 changes: 16 additions & 3 deletions railties/lib/rails/commands/server.rb
Expand Up @@ -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]}"
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions railties/lib/rails/generators/rails/app/app_generator.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/generators/rails/app/templates/Rakefile
Expand Up @@ -7,4 +7,4 @@ require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

require 'rails/tasks'
<%= app_const %>.load_tasks
Expand Up @@ -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.
Expand Down
Expand Up @@ -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 %>)
4 changes: 1 addition & 3 deletions railties/lib/rails/rack/log_tailer.rb
@@ -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

Expand Down
4 changes: 0 additions & 4 deletions railties/lib/rails/tasks/misc.rake
@@ -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
Expand Down
21 changes: 4 additions & 17 deletions railties/test/application/load_test.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 530b8ff

Please sign in to comment.