Skip to content

Commit

Permalink
Merge pull request #6518 from kennyj/fix_5847-4
Browse files Browse the repository at this point in the history
(Try again) Fix #5847 and #4045.
  • Loading branch information
josevalim committed May 29, 2012
2 parents d46a368 + c0ba0f0 commit 9fa3926
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 2 deletions.
1 change: 1 addition & 0 deletions activerecord/lib/active_record.rb
Expand Up @@ -80,6 +80,7 @@ module ActiveRecord
autoload :Sanitization
autoload :Schema
autoload :SchemaDumper
autoload :SchemaMigration
autoload :Scoping
autoload :Serialization
autoload :SessionStore
Expand Down
@@ -1,5 +1,4 @@
require 'active_support/deprecation/reporting'
require 'active_record/schema_migration'
require 'active_record/migration/join_table'

module ActiveRecord
Expand Down
1 change: 0 additions & 1 deletion activerecord/lib/active_record/migration.rb
@@ -1,7 +1,6 @@
require "active_support/core_ext/module/delegation"
require "active_support/core_ext/class/attribute_accessors"
require 'active_support/deprecation'
require 'active_record/schema_migration'
require 'set'

module ActiveRecord
Expand Down
6 changes: 6 additions & 0 deletions activerecord/lib/active_record/railtie.rb
Expand Up @@ -30,6 +30,7 @@ class Railtie < Rails::Railtie
)

rake_tasks do
require "active_record/base"
load "active_record/railties/databases.rake"
end

Expand All @@ -38,10 +39,15 @@ class Railtie < Rails::Railtie
# first time. Also, make it output to STDERR.
console do |app|
require "active_record/railties/console_sandbox" if app.sandbox?
require "active_record/base"
console = ActiveSupport::Logger.new(STDERR)
Rails.logger.extend ActiveSupport::Logger.broadcast console
end

runner do |app|
require "active_record/base"
end

initializer "active_record.initialize_timezone" do
ActiveSupport.on_load(:active_record) do
self.time_zone_aware_attributes = true
Expand Down
11 changes: 11 additions & 0 deletions railties/lib/rails/application.rb
Expand Up @@ -158,6 +158,14 @@ def load_console(app=self)
self
end

# Load the application runner and invoke the registered hooks.
# Check <tt>Rails::Railtie.runner</tt> for more info.
def load_runner(app=self)
initialize_runner
super
self
end

# Stores some of the Rails initial environment parameters which
# will be used by middlewares and engines to configure themselves.
def env_config
Expand Down Expand Up @@ -304,6 +312,9 @@ def initialize_console #:nodoc:
require "rails/console/helpers"
end

def initialize_runner #:nodoc:
end

def build_original_fullpath(env)
path_info = env["PATH_INFO"]
query_string = env["QUERY_STRING"]
Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/commands/runner.rb
Expand Up @@ -41,6 +41,7 @@

require APP_PATH
Rails.application.require_environment!
Rails.application.load_runner

if code_or_file.nil?
$stderr.puts "Run '#{$0} -h' for help."
Expand Down
5 changes: 5 additions & 0 deletions railties/lib/rails/engine.rb
Expand Up @@ -437,6 +437,11 @@ def load_console(app=self)
super
end

def load_runner(app=self)
railties.all { |r| r.load_runner(app) }
super
end

def eager_load!
railties.all(&:eager_load!)

Expand Down
10 changes: 10 additions & 0 deletions railties/lib/rails/railtie.rb
Expand Up @@ -145,6 +145,12 @@ def console(&blk)
@load_console
end

def runner(&blk)
@load_runner ||= []
@load_runner << blk if blk
@load_runner
end

def generators(&blk)
@generators ||= []
@generators << blk if blk
Expand Down Expand Up @@ -179,6 +185,10 @@ def load_console(app=self)
self.class.console.each { |block| block.call(app) }
end

def load_runner(app=self)
self.class.runner.each { |block| block.call(app) }
end

def load_tasks(app=self)
extend Rake::DSL if defined? Rake::DSL
self.class.rake_tasks.each { |block| self.instance_exec(app, &block) }
Expand Down
23 changes: 23 additions & 0 deletions railties/test/application/rake_test.rb
Expand Up @@ -167,5 +167,28 @@ def test_rake_clear_schema_cache
end
assert !File.exists?(File.join(app_path, 'db', 'schema_cache.dump'))
end

def test_load_activerecord_base_when_we_use_observers
Dir.chdir(app_path) do
`bundle exec rails g model user;
bundle exec rake db:migrate;
bundle exec rails g observer user;`

add_to_config "config.active_record.observers = :user_observer"

assert_equal "0", `bundle exec rails r "puts User.count"`.strip

app_file "lib/tasks/count_user.rake", <<-RUBY
namespace :user do
task :count => :environment do
puts User.count
end
end
RUBY

assert_equal "0", `bundle exec rake user:count`.strip
end
end

end
end
10 changes: 10 additions & 0 deletions railties/test/application/runner_test.rb
Expand Up @@ -57,5 +57,15 @@ def test_should_set_dollar_program_name_to_file

assert_match "script/program_name.rb", Dir.chdir(app_path) { `bundle exec rails runner "script/program_name.rb"` }
end

def test_with_hook
add_to_config <<-RUBY
runner do |app|
app.config.ran = true
end
RUBY

assert_match "true", Dir.chdir(app_path) { `bundle exec rails runner "puts Rails.application.config.ran"` }
end
end
end
16 changes: 16 additions & 0 deletions railties/test/railties/railtie_test.rb
Expand Up @@ -163,6 +163,22 @@ class MyTie < Rails::Railtie
assert $ran_block
end

test "runner block is executed when MyApp.load_runner is called" do
$ran_block = false

class MyTie < Rails::Railtie
runner do
$ran_block = true
end
end

require "#{app_path}/config/environment"

assert !$ran_block
AppTemplate::Application.load_runner
assert $ran_block
end

test "railtie can add initializers" do
$ran_block = false

Expand Down

0 comments on commit 9fa3926

Please sign in to comment.