Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
WIP: Remove the global router
  • Loading branch information
Carlhuda committed Feb 26, 2010
1 parent 76237f1 commit 226dfc2
Show file tree
Hide file tree
Showing 26 changed files with 342 additions and 263 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller.rb
Expand Up @@ -32,7 +32,7 @@ module ActionController
autoload :SessionManagement
autoload :Streaming
autoload :Testing
autoload :UrlFor
# ROUTES TODO: Proxy UrlFor to Rails.application.routes.url_helpers
autoload :Verification
end

Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/base.rb
Expand Up @@ -9,7 +9,7 @@ class Base < Metal
include ActionController::Helpers

include ActionController::HideActions
include ActionController::UrlFor
# include ActionController::UrlFor
include ActionController::Redirecting
include ActionController::Rendering
include ActionController::Renderers::All
Expand Down
3 changes: 2 additions & 1 deletion actionpack/lib/action_controller/deprecated.rb
@@ -1,4 +1,5 @@
ActionController::AbstractRequest = ActionController::Request = ActionDispatch::Request
ActionController::AbstractResponse = ActionController::Response = ActionDispatch::Response
ActionController::Routing = ActionDispatch::Routing
ActionController::UrlWriter = ActionController::UrlFor
# ROUTES TODO: Figure out how to deprecate this.
# ActionController::UrlWriter = ActionController::UrlFor
4 changes: 3 additions & 1 deletion actionpack/lib/action_controller/metal/head.rb
@@ -1,7 +1,6 @@
module ActionController
module Head
extend ActiveSupport::Concern
include ActionController::UrlFor

# Return a response that has no content (merely headers). The options
# argument is interpreted to be a hash of header names and values.
Expand All @@ -25,6 +24,9 @@ def head(status, options = {})
end

self.status = status
# ROUTES TODO: Figure out how to rescue from a no method error
# This is needed only if you wire up a controller yourself, and
# this not working would be baffling without a better error
self.location = url_for(location) if location
self.content_type = Mime[formats.first]
self.response_body = " "
Expand Down
4 changes: 3 additions & 1 deletion actionpack/lib/action_controller/metal/redirecting.rb
Expand Up @@ -12,7 +12,6 @@ module Redirecting

include AbstractController::Logger
include ActionController::RackDelegation
include ActionController::UrlFor

# Redirects the browser to the target specified in +options+. This parameter can take one of three forms:
#
Expand Down Expand Up @@ -84,6 +83,9 @@ def _compute_redirect_to_location(options)
raise RedirectBackError unless refer = request.headers["Referer"]
refer
else
# ROUTES TODO: Figure out how to rescue from a no method error
# This is needed only if you wire up a controller yourself, and
# this not working would be baffling without a better error
url_for(options)
end.gsub(/[\r\n]/, '')
end
Expand Down
165 changes: 0 additions & 165 deletions actionpack/lib/action_controller/metal/url_for.rb

This file was deleted.

10 changes: 5 additions & 5 deletions actionpack/lib/action_controller/test_case.rb
Expand Up @@ -17,9 +17,9 @@ def self.new_escaped(strings)
end
end

def assign_parameters(controller_path, action, parameters = {})
def assign_parameters(router, controller_path, action, parameters = {})
parameters = parameters.symbolize_keys.merge(:controller => controller_path, :action => action)
extra_keys = ActionDispatch::Routing::Routes.extra_keys(parameters)
extra_keys = router.extra_keys(parameters)
non_path_parameters = get? ? query_parameters : request_parameters
parameters.each do |key, value|
if value.is_a? Fixnum
Expand Down Expand Up @@ -220,7 +220,7 @@ def xml_http_request(request_method, action, parameters = nil, session = nil, fl
def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
# Sanity check for required instance variables so we can give an
# understandable error message.
%w(@controller @request @response).each do |iv_name|
%w(@router @controller @request @response).each do |iv_name|
if !(instance_variable_names.include?(iv_name) || instance_variable_names.include?(iv_name.to_sym)) || instance_variable_get(iv_name).nil?
raise "#{iv_name} is nil: make sure you set it in your test's setup method."
end
Expand All @@ -236,7 +236,7 @@ def process(action, parameters = nil, session = nil, flash = nil, http_method =
@request.env['REQUEST_METHOD'] = http_method

parameters ||= {}
@request.assign_parameters(@controller.class.name.underscore.sub(/_controller$/, ''), action.to_s, parameters)
@request.assign_parameters(@router, @controller.class.name.underscore.sub(/_controller$/, ''), action.to_s, parameters)

@request.session = ActionController::TestSession.new(session) unless session.nil?
@request.session["flash"] = @request.flash.update(flash || {})
Expand Down Expand Up @@ -340,7 +340,7 @@ def build_request_uri(action, parameters)
options.update(:only_path => true, :action => action)

url = ActionController::UrlRewriter.new(@request, parameters)
@request.request_uri = url.rewrite(options)
@request.request_uri = url.rewrite(@router, options)
end
end
end
Expand Down
9 changes: 5 additions & 4 deletions actionpack/lib/action_controller/url_rewriter.rb
Expand Up @@ -9,11 +9,11 @@ def initialize(request, parameters)
@request, @parameters = request, parameters
end

def rewrite(options = {})
def rewrite(router, options = {})
options[:host] ||= @request.host_with_port
options[:protocol] ||= @request.protocol

self.class.rewrite(options, @request.symbolized_path_parameters) do |options|
self.class.rewrite(router, options, @request.symbolized_path_parameters) do |options|
process_path_options(options)
end
end
Expand All @@ -24,7 +24,8 @@ def to_str

alias_method :to_s, :to_str

def self.rewrite(options, path_segments=nil)
# ROUTES TODO: Class method code smell
def self.rewrite(router, options, path_segments=nil)
rewritten_url = ""

unless options[:only_path]
Expand All @@ -40,7 +41,7 @@ def self.rewrite(options, path_segments=nil)

path_options = options.except(*RESERVED_OPTIONS)
path_options = yield(path_options) if block_given?
path = Routing::Routes.generate(path_options, path_segments || {})
path = router.generate(path_options, path_segments || {})

rewritten_url << ActionController::Base.relative_url_root.to_s unless options[:skip_relative_url_root]
rewritten_url << (options[:trailing_slash] ? path.sub(/\?|\z/) { "/" + $& } : path)
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_dispatch/routing.rb
Expand Up @@ -206,6 +206,7 @@ module Routing
autoload :Route, 'action_dispatch/routing/route'
autoload :Routes, 'action_dispatch/routing/routes'
autoload :RouteSet, 'action_dispatch/routing/route_set'
autoload :UrlFor, 'action_dispatch/routing/url_for'

SEPARATORS = %w( / . ? )
HTTP_METHODS = [:get, :head, :post, :put, :delete, :options]
Expand Down
30 changes: 30 additions & 0 deletions actionpack/lib/action_dispatch/routing/route_set.rb
Expand Up @@ -272,6 +272,36 @@ def install_helpers(destinations = [ActionController::Base, ActionView::Base], r
named_routes.install(destinations, regenerate_code)
end

# ROUTES TODO: Revisit the name of these methods
def url_helpers
@url_helpers ||= begin
router = self
Module.new do
extend ActiveSupport::Concern
include UrlFor

define_method(:_router) { router }
end
end
end

def named_url_helpers
@named_url_helpers ||= begin
router = self

Module.new do
extend ActiveSupport::Concern
include router.url_helpers

# ROUTES TODO: install_helpers isn't great... can we make a module with the stuff that
# we can include?
included do
router.install_helpers(self)
end
end
end
end

def empty?
routes.empty?
end
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_dispatch/routing/routes.rb
@@ -1,5 +1,5 @@
# A singleton that stores the current route set
ActionDispatch::Routing::Routes = ActionDispatch::Routing::RouteSet.new
# ActionDispatch::Routing::Routes = ActionDispatch::Routing::RouteSet.new

# To preserve compatibility with pre-3.0 Rails action_controller/deprecated.rb
# defines ActionDispatch::Routing::Routes as an alias

0 comments on commit 226dfc2

Please sign in to comment.