1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add console hook to force ActiveRecord::Base to be loaded when console starts avoiding reference loops.

This commit is contained in:
José Valim 2010-07-17 10:59:41 +02:00
parent cfca55949f
commit fa98eca75b
6 changed files with 64 additions and 15 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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