diff options
author | José Valim <jose.valim@gmail.com> | 2010-07-17 10:59:41 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-07-18 11:02:07 +0200 |
commit | fa98eca75bd8666719bf3d061c87638850a20fe9 (patch) | |
tree | 8856316fd6c3bf49fc1ace113f6f62891701bc28 | |
parent | cfca55949f51bf3970bae7c506807db97dbcf05f (diff) | |
download | rails-fa98eca75bd8666719bf3d061c87638850a20fe9.tar.gz rails-fa98eca75bd8666719bf3d061c87638850a20fe9.tar.bz2 rails-fa98eca75bd8666719bf3d061c87638850a20fe9.zip |
Add console hook to force ActiveRecord::Base to be loaded when console starts avoiding reference loops.
-rw-r--r-- | activerecord/lib/active_record/railtie.rb | 6 | ||||
-rw-r--r-- | railties/lib/rails/application.rb | 13 | ||||
-rw-r--r-- | railties/lib/rails/commands/console.rb | 5 | ||||
-rw-r--r-- | railties/lib/rails/railtie.rb | 18 | ||||
-rw-r--r-- | railties/test/application/console_test.rb | 21 | ||||
-rw-r--r-- | railties/test/railties/railtie_test.rb | 16 |
6 files changed, 64 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index eff51a7c87..78fdb77216 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -22,6 +22,12 @@ module ActiveRecord load "active_record/railties/databases.rake" end + # When loading console, force ActiveRecord to be loaded to avoid cross + # references when loading a constant for the first time. + console do + ActiveRecord::Base + end + initializer "active_record.initialize_timezone" do ActiveSupport.on_load(:active_record) do self.time_zone_aware_attributes = true diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 458177b954..3f9bca0bd6 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -149,6 +149,13 @@ module Rails self end + def load_console(sandbox=false) + initialize_console(sandbox) + railties.all { |r| r.load_console } + super() + self + end + def app @app ||= begin config.middleware = config.middleware.merge_into(default_middleware_stack) @@ -212,5 +219,11 @@ module Rails def initialize_generators require "rails/generators" end + + def initialize_console(sandbox=false) + require "rails/console/app" + require "rails/console/sandbox" if sandbox + require "rails/console/helpers" + end end end diff --git a/railties/lib/rails/commands/console.rb b/railties/lib/rails/commands/console.rb index 50df6ba405..834a120c01 100644 --- a/railties/lib/rails/commands/console.rb +++ b/railties/lib/rails/commands/console.rb @@ -23,10 +23,7 @@ module Rails opt.parse!(ARGV) end - @app.initialize! - require "rails/console/app" - require "rails/console/sandbox" if options[:sandbox] - require "rails/console/helpers" + @app.load_console(options[:sandbox]) if options[:debugger] begin diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index dbdbfea509..1d6a2de87d 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -156,6 +156,12 @@ module Rails @rake_tasks end + def console(&blk) + @load_console ||= [] + @load_console << blk if blk + @load_console + end + def generators(&blk) @generators ||= [] @generators << blk if blk @@ -170,20 +176,16 @@ module Rails def eager_load! end - def rake_tasks - self.class.rake_tasks - end - - def generators - self.class.generators + def load_console + self.class.console.each(&:call) end def load_tasks - rake_tasks.each { |blk| blk.call } + self.class.rake_tasks.each(&:call) end def load_generators - generators.each { |blk| blk.call } + self.class.generators.each(&:call) end end end diff --git a/railties/test/application/console_test.rb b/railties/test/application/console_test.rb index 8ff69f0208..a72e6916dd 100644 --- a/railties/test/application/console_test.rb +++ b/railties/test/application/console_test.rb @@ -9,10 +9,8 @@ class ConsoleTest < Test::Unit::TestCase end def load_environment - # Load steps taken from rails/commands/console.rb require "#{rails_root}/config/environment" - require 'rails/console/app' - require 'rails/console/helpers' + Rails.application.load_console end def test_app_method_should_return_integration_session @@ -75,4 +73,21 @@ class ConsoleTest < Test::Unit::TestCase assert_equal 'Once upon a time in a world...', helper.truncate('Once upon a time in a world far far away') end + + def test_active_record_does_not_panic_when_referencing_an_observed_constant + add_to_config "config.active_record.observers = :user_observer" + + app_file "app/models/user.rb", <<-MODEL + class User < ActiveRecord::Base + end + MODEL + + app_file "app/models/user_observer.rb", <<-MODEL + class UserObserver < ActiveRecord::Observer + end + MODEL + + load_environment + assert_nothing_raised { User } + end end diff --git a/railties/test/railties/railtie_test.rb b/railties/test/railties/railtie_test.rb index c74cc01dc1..db0fd87491 100644 --- a/railties/test/railties/railtie_test.rb +++ b/railties/test/railties/railtie_test.rb @@ -103,6 +103,22 @@ module RailtiesTest assert $ran_block end + test "console block is executed when MyApp.load_console is called" do + $ran_block = false + + class MyTie < Rails::Railtie + console do + $ran_block = true + end + end + + require "#{app_path}/config/environment" + + assert !$ran_block + AppTemplate::Application.load_console + assert $ran_block + end + test "railtie can add initializers" do $ran_block = false |