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

If IntegrationSession is initialized with an objects that responds to #routes, automatically extend the URL helpers from the RouteSet onto it

This commit is contained in:
Carlhuda 2010-02-26 17:18:29 -08:00
parent 6324eeefd7
commit 050831803a
6 changed files with 24 additions and 14 deletions

View file

@ -203,7 +203,6 @@ module ActionDispatch
url_for(options) url_for(options)
end end
protected :#{selector}
END_EVAL END_EVAL
helpers << selector helpers << selector
end end

View file

@ -162,12 +162,31 @@ module ActionDispatch
# A running counter of the number of requests processed. # A running counter of the number of requests processed.
attr_accessor :request_count attr_accessor :request_count
include ActionDispatch::Routing::UrlFor
# Create and initialize a new Session instance. # Create and initialize a new Session instance.
def initialize(app) def initialize(app)
@app = app @app = app
# If the app is a Rails app, make url_helpers available on the session
# This makes app.url_for and app.foo_path available in the console
if app.respond_to?(:routes) && app.routes.respond_to?(:url_helpers)
singleton_class.class_eval { include app.routes.url_helpers }
end
reset! reset!
end end
def url_options
opts = super.reverse_merge(
:host => host,
:protocol => https? ? "https" : "http"
)
opts.merge!(:port => 443) if !opts.key?(:port) && https?
opts
end
# Resets the instance. This can be used to reset the state information # Resets the instance. This can be used to reset the state information
# in an existing session instance, so it can be used from a clean-slate # in an existing session instance, so it can be used from a clean-slate
# condition. # condition.
@ -346,13 +365,8 @@ module ActionDispatch
include ActionDispatch::Routing::UrlFor include ActionDispatch::Routing::UrlFor
def url_options def url_options
opts = super.reverse_merge( reset! unless @integration_session
:host => host, @integration_session.url_options
:protocol => https? ? "https" : "http"
)
opts.merge!(:port => 443) if !opts.key?(:port) && https?
opts
end end
# Delegate unhandled messages to the current session instance. # Delegate unhandled messages to the current session instance.

View file

@ -85,6 +85,7 @@ end
class RoutedRackApp class RoutedRackApp
attr_reader :router attr_reader :router
alias routes router
def initialize(router, &blk) def initialize(router, &blk)
@router = router @router = router

View file

@ -511,7 +511,6 @@ class ActionCacheTest < ActionController::TestCase
@request = ActionController::TestRequest.new @request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new @response = ActionController::TestResponse.new
@controller = ActionCachingTestController.new @controller = ActionCachingTestController.new
# ROUTES TODO: It seems bad to explicitly remix in the class
@controller.singleton_class.send(:include, @router.url_helpers) @controller.singleton_class.send(:include, @router.url_helpers)
@request.host = 'hostname.com' @request.host = 'hostname.com'
end end

View file

@ -178,8 +178,8 @@ class IntegrationTestTest < Test::Unit::TestCase
session1 = @test.open_session { |sess| } session1 = @test.open_session { |sess| }
session2 = @test.open_session # implicit session session2 = @test.open_session # implicit session
assert_equal ::ActionController::Integration::Session, session1.class assert_kind_of ::ActionController::Integration::Session, session1
assert_equal ::ActionController::Integration::Session, session2.class assert_kind_of ::ActionController::Integration::Session, session2
assert_not_equal session1, session2 assert_not_equal session1, session2
end end
@ -221,8 +221,6 @@ class IntegrationTestUsesCorrectClass < ActionController::IntegrationTest
end end
class IntegrationProcessTest < ActionController::IntegrationTest class IntegrationProcessTest < ActionController::IntegrationTest
include SharedTestRoutes.url_helpers
class IntegrationController < ActionController::Base class IntegrationController < ActionController::Base
def get def get
respond_to do |format| respond_to do |format|

View file

@ -1230,7 +1230,6 @@ class ResourcesTest < ActionController::TestCase
end end
@controller = "#{options[:options][:controller].camelize}Controller".constantize.new @controller = "#{options[:options][:controller].camelize}Controller".constantize.new
# ROUTES TODO: Figure out a way to not extend the routing helpers here
@controller.singleton_class.send(:include, @router.url_helpers) @controller.singleton_class.send(:include, @router.url_helpers)
@request = ActionController::TestRequest.new @request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new @response = ActionController::TestResponse.new