Skip to content

Commit

Permalink
Add console hook to force ActiveRecord::Base to be loaded when consol…
Browse files Browse the repository at this point in the history
…e starts avoiding reference loops.
  • Loading branch information
josevalim committed Jul 18, 2010
1 parent cfca559 commit fa98eca
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 15 deletions.
6 changes: 6 additions & 0 deletions activerecord/lib/active_record/railtie.rb
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions railties/lib/rails/application.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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
5 changes: 1 addition & 4 deletions railties/lib/rails/commands/console.rb
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions railties/lib/rails/railtie.rb
Expand Up @@ -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
Expand All @@ -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
21 changes: 18 additions & 3 deletions railties/test/application/console_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
16 changes: 16 additions & 0 deletions railties/test/railties/railtie_test.rb
Expand Up @@ -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

Expand Down

0 comments on commit fa98eca

Please sign in to comment.