diff options
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/application.rb | 11 | ||||
-rw-r--r-- | railties/lib/rails/commands/runner.rb | 1 | ||||
-rw-r--r-- | railties/lib/rails/engine.rb | 5 | ||||
-rw-r--r-- | railties/lib/rails/railtie.rb | 10 | ||||
-rw-r--r-- | railties/test/application/rake_test.rb | 23 | ||||
-rw-r--r-- | railties/test/application/runner_test.rb | 10 | ||||
-rw-r--r-- | railties/test/railties/railtie_test.rb | 16 |
7 files changed, 76 insertions, 0 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 660e864d2a..32797ee657 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -158,6 +158,14 @@ module Rails 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 @@ -304,6 +312,9 @@ module Rails 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"] diff --git a/railties/lib/rails/commands/runner.rb b/railties/lib/rails/commands/runner.rb index 77f1b15fb4..a672258aa6 100644 --- a/railties/lib/rails/commands/runner.rb +++ b/railties/lib/rails/commands/runner.rb @@ -41,6 +41,7 @@ ENV["RAILS_ENV"] = options[:environment] require APP_PATH Rails.application.require_environment! + Rails.application.load_runner if code_or_file.nil? $stderr.puts "Run '#{$0} -h' for help." diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 4c7199a2e2..806b553b81 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -437,6 +437,11 @@ module Rails super end + def load_runner(app=self) + railties.all { |r| r.load_runner(app) } + super + end + def eager_load! railties.all(&:eager_load!) diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index 2102f8a03c..c3cc65ab31 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -145,6 +145,12 @@ module Rails @load_console end + def runner(&blk) + @load_runner ||= [] + @load_runner << blk if blk + @load_runner + end + def generators(&blk) @generators ||= [] @generators << blk if blk @@ -179,6 +185,10 @@ module Rails 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) } diff --git a/railties/test/application/rake_test.rb b/railties/test/application/rake_test.rb index 27d521485c..8cf867da3c 100644 --- a/railties/test/application/rake_test.rb +++ b/railties/test/application/rake_test.rb @@ -167,5 +167,28 @@ module ApplicationTests 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 diff --git a/railties/test/application/runner_test.rb b/railties/test/application/runner_test.rb index e1d283a7fd..81ed5873a5 100644 --- a/railties/test/application/runner_test.rb +++ b/railties/test/application/runner_test.rb @@ -57,5 +57,15 @@ module ApplicationTests 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 diff --git a/railties/test/railties/railtie_test.rb b/railties/test/railties/railtie_test.rb index cd495320b5..c80b0f63af 100644 --- a/railties/test/railties/railtie_test.rb +++ b/railties/test/railties/railtie_test.rb @@ -163,6 +163,22 @@ module RailtiesTest 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 |