diff options
author | José Valim <jose.valim@gmail.com> | 2011-11-09 02:14:50 -0800 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-11-09 02:14:50 -0800 |
commit | 4857339aab4f725639347c55e56a75012bb81411 (patch) | |
tree | 217083c728bfecf05f8f3db8132d3a20cfa7aa5e /railties | |
parent | fba28ece7e17efb6e5f8ebea672991952660c321 (diff) | |
parent | 6d09f275c6e3224a8ea9a738512c90ee5334e20d (diff) | |
download | rails-4857339aab4f725639347c55e56a75012bb81411.tar.gz rails-4857339aab4f725639347c55e56a75012bb81411.tar.bz2 rails-4857339aab4f725639347c55e56a75012bb81411.zip |
Merge pull request #3509 from amatsuda/console_extend_command_bundle
move Rails console top level methods to IRB context
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/application.rb | 2 | ||||
-rw-r--r-- | railties/lib/rails/console/app.rb | 48 | ||||
-rw-r--r-- | railties/lib/rails/console/helpers.rb | 14 | ||||
-rw-r--r-- | railties/test/application/console_test.rb | 14 |
4 files changed, 47 insertions, 31 deletions
diff --git a/railties/lib/rails/application.rb b/railties/lib/rails/application.rb index 82fffe86bb..e8b160448b 100644 --- a/railties/lib/rails/application.rb +++ b/railties/lib/rails/application.rb @@ -205,6 +205,8 @@ module Rails require "pp" require "rails/console/app" require "rails/console/helpers" + + IRB::ExtendCommandBundle.send :include, Rails::ConsoleMethods end end end diff --git a/railties/lib/rails/console/app.rb b/railties/lib/rails/console/app.rb index 95c74baae2..23d57379ba 100644 --- a/railties/lib/rails/console/app.rb +++ b/railties/lib/rails/console/app.rb @@ -5,28 +5,32 @@ require 'action_controller' # 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" - end -end +module Rails + module ConsoleMethods + # 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 + # 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 + # reloads the environment + def reload!(print=true) + puts "Reloading..." if print + ActionDispatch::Reloader.cleanup! + ActionDispatch::Reloader.prepare! + true + end + end end diff --git a/railties/lib/rails/console/helpers.rb b/railties/lib/rails/console/helpers.rb index 212fc6125a..230d3d9d04 100644 --- a/railties/lib/rails/console/helpers.rb +++ b/railties/lib/rails/console/helpers.rb @@ -1,7 +1,11 @@ -def helper - @helper ||= ApplicationController.helpers -end +module Rails + module ConsoleMethods + def helper + @helper ||= ApplicationController.helpers + end -def controller - @controller ||= ApplicationController.new + def controller + @controller ||= ApplicationController.new + end + end end diff --git a/railties/test/application/console_test.rb b/railties/test/application/console_test.rb index 1528d5dd87..b3745f194e 100644 --- a/railties/test/application/console_test.rb +++ b/railties/test/application/console_test.rb @@ -18,16 +18,20 @@ class ConsoleTest < Test::Unit::TestCase Rails.application.load_console end + def irb_context + Object.new.extend(Rails::ConsoleMethods) + 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 @@ class ConsoleTest < Test::Unit::TestCase 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 ConsoleTest < Test::Unit::TestCase 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...', |