Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add console hook to force ActiveRecord::Base to be loaded when consol…

…e starts avoiding reference loops.
  • Loading branch information...
commit fa98eca75bd8666719bf3d061c87638850a20fe9 1 parent cfca559
@josevalim josevalim authored
View
6 activerecord/lib/active_record/railtie.rb
@@ -22,6 +22,12 @@ class Railtie < Rails::Railtie
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
View
13 railties/lib/rails/application.rb
@@ -149,6 +149,13 @@ def load_generators
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 @@ def initialize_tasks
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
View
5 railties/lib/rails/commands/console.rb
@@ -23,10 +23,7 @@ def start
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
View
18 railties/lib/rails/railtie.rb
@@ -156,6 +156,12 @@ def rake_tasks(&blk)
@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 @@ def abstract_railtie?
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
View
21 railties/test/application/console_test.rb
@@ -9,10 +9,8 @@ def setup
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 @@ def test_access_to_helpers
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
View
16 railties/test/railties/railtie_test.rb
@@ -103,6 +103,22 @@ class MyTie < Rails::Railtie
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
Please sign in to comment.
Something went wrong with that request. Please try again.