move Rails console top level methods to IRB context

This commit is contained in:
Akira Matsuda 2011-11-04 04:50:53 +09:00
parent b5f908a7ad
commit 7102a3d7fc
3 changed files with 46 additions and 32 deletions

@ -5,28 +5,32 @@
# work around the at_exit hook in test/unit, which kills IRB
Test::Unit.run = true if Test::Unit.respond_to?(:run=)
# reference the global "app" instance, created on demand. To recreate the
# instance, pass a non-false value as the parameter.
def app(create=false)
@app_integration_instance = nil if create
@app_integration_instance ||= new_session do |sess|
sess.host! "www.example.com"
module IRB
module ExtendCommandBundle
# reference the global "app" instance, created on demand. To recreate the
# instance, pass a non-false value as the parameter.
def app(create=false)
@app_integration_instance = nil if create
@app_integration_instance ||= new_session do |sess|
sess.host! "www.example.com"
end
end
# create a new session. If a block is given, the new session will be yielded
# to the block before being returned.
def new_session
app = Rails.application
session = ActionDispatch::Integration::Session.new(app)
yield session if block_given?
session
end
# reloads the environment
def reload!(print=true)
puts "Reloading..." if print
ActionDispatch::Reloader.cleanup!
ActionDispatch::Reloader.prepare!
true
end
end
end
# create a new session. If a block is given, the new session will be yielded
# to the block before being returned.
def new_session
app = Rails.application
session = ActionDispatch::Integration::Session.new(app)
yield session if block_given?
session
end
# reloads the environment
def reload!(print=true)
puts "Reloading..." if print
ActionDispatch::Reloader.cleanup!
ActionDispatch::Reloader.prepare!
true
end

@ -1,7 +1,11 @@
def helper
@helper ||= ApplicationController.helpers
end
module IRB
module ExtendCommandBundle
def helper
@helper ||= ApplicationController.helpers
end
def controller
@controller ||= ApplicationController.new
def controller
@controller ||= ApplicationController.new
end
end
end

@ -18,16 +18,20 @@ def load_environment(sandbox = false)
Rails.application.load_console
end
def irb_context
Object.new.extend(IRB::ExtendCommandBundle)
end
def test_app_method_should_return_integration_session
TestHelpers::Rack.send :remove_method, :app
load_environment
console_session = app
console_session = irb_context.app
assert_instance_of ActionDispatch::Integration::Session, console_session
end
def test_new_session_should_return_integration_session
load_environment
session = new_session
session = irb_context.new_session
assert_instance_of ActionDispatch::Integration::Session, session
end
@ -41,7 +45,7 @@ def test_reload_should_fire_preparation_and_cleanup_callbacks
ActionDispatch::Reloader.to_prepare { c = 3 }
# Hide Reloading... output
silence_stream(STDOUT) { reload! }
silence_stream(STDOUT) { irb_context.reload! }
assert_equal 1, a
assert_equal 2, b
@ -66,12 +70,14 @@ class User
MODEL
assert !User.new.respond_to?(:age)
silence_stream(STDOUT) { reload! }
silence_stream(STDOUT) { irb_context.reload! }
session = irb_context.new_session
assert User.new.respond_to?(:age)
end
def test_access_to_helpers
load_environment
helper = irb_context.helper
assert_not_nil helper
assert_instance_of ActionView::Base, helper
assert_equal 'Once upon a time in a world...',