Skip to content

Commit 49b6b49

Browse files
committed
Clean up routes inclusion and add some comments for the next soul that decides to adventure on this code.
1 parent 439d340 commit 49b6b49

File tree

4 files changed

+49
-60
lines changed

4 files changed

+49
-60
lines changed

actionpack/lib/action_dispatch/routing.rb

-5
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,5 @@ module Routing
284284

285285
SEPARATORS = %w( / . ? ) #:nodoc:
286286
HTTP_METHODS = [:get, :head, :post, :put, :delete, :options] #:nodoc:
287-
288-
# A helper module to hold URL related helpers.
289-
module Helpers #:nodoc:
290-
include PolymorphicRoutes
291-
end
292287
end
293288
end

actionpack/lib/action_dispatch/routing/route_set.rb

+37-30
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,6 @@ def length
123123
routes.length
124124
end
125125

126-
def install(destinations = [ActionController::Base, ActionView::Base])
127-
helper = @module
128-
destinations.each { |d| d.module_eval { include helper } }
129-
end
130-
131126
private
132127
def url_helper_name(name, kind = :url)
133128
:"#{name}_#{kind}"
@@ -276,14 +271,15 @@ def clear!
276271
@prepend.each { |blk| eval_block(blk) }
277272
end
278273

279-
def install_helpers(destinations)
280-
destinations.each { |d| d.module_eval { include Helpers } }
281-
named_routes.install(destinations)
282-
end
283-
284-
module MountedHelpers
274+
module MountedHelpers #:nodoc:
275+
extend ActiveSupport::Concern
276+
include UrlFor
285277
end
286278

279+
# Contains all the mounted helpers accross different
280+
# engines and the `main_app` helper for the application.
281+
# You can include this in your classes if you want to
282+
# access routes for other engines.
287283
def mounted_helpers
288284
MountedHelpers
289285
end
@@ -294,7 +290,7 @@ def define_mounted_helper(name)
294290
routes = self
295291
MountedHelpers.class_eval do
296292
define_method "_#{name}" do
297-
RoutesProxy.new(routes, self._routes_context)
293+
RoutesProxy.new(routes, _routes_context)
298294
end
299295
end
300296

@@ -306,29 +302,40 @@ def #{name}
306302
end
307303

308304
def url_helpers
309-
routes = self
305+
@url_helpers ||= begin
306+
routes = self
307+
308+
Module.new do
309+
extend ActiveSupport::Concern
310+
include UrlFor
311+
312+
# Define url_for in the singleton level so one can do:
313+
# Rails.application.routes.url_helpers.url_for(args)
314+
@_routes = routes
315+
class << self
316+
delegate :url_for, :to => '@_routes'
317+
end
310318

311-
@url_helpers ||= Module.new {
312-
extend ActiveSupport::Concern
313-
include UrlFor
319+
# Make named_routes available in the module singleton
320+
# as well, so one can do:
321+
# Rails.application.routes.url_helpers.posts_path
322+
extend routes.named_routes.module
314323

315-
@_routes = routes
316-
def self.url_for(options)
317-
@_routes.url_for options
318-
end
324+
# Any class that includes this module will get all
325+
# named routes...
326+
include routes.named_routes.module
319327

320-
extend routes.named_routes.module
328+
# plus a singleton class method called _routes ...
329+
included do
330+
singleton_class.send(:redefine_method, :_routes) { routes }
331+
end
321332

322-
# ROUTES TODO: install_helpers isn't great... can we make a module with the stuff that
323-
# we can include?
324-
# Yes plz - JP
325-
included do
326-
routes.install_helpers([self])
327-
singleton_class.send(:redefine_method, :_routes) { routes }
333+
# And an instance method _routes. Note that
334+
# UrlFor (included in this module) add extra
335+
# conveniences for working with @_routes.
336+
define_method(:_routes) { @_routes || routes }
328337
end
329-
330-
define_method(:_routes) { @_routes || routes }
331-
}
338+
end
332339
end
333340

334341
def empty?

actionpack/lib/action_dispatch/routing/url_for.rb

+12-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ module Routing
88
#
99
# <b>Tip:</b> If you need to generate URLs from your models or some other place,
1010
# then ActionController::UrlFor is what you're looking for. Read on for
11-
# an introduction.
11+
# an introduction. In general, this module should not be included on its own,
12+
# as it is usually included by url_helpers (as in Rails.application.routes.url_helpers).
1213
#
1314
# == URL generation from parameters
1415
#
@@ -84,7 +85,6 @@ module UrlFor
8485
include PolymorphicRoutes
8586

8687
included do
87-
# TODO: with_routing extends @controller with url_helpers, trickling down to including this module which overrides its default_url_options
8888
unless method_defined?(:default_url_options)
8989
# Including in a class uses an inheritable hash. Modules get a plain hash.
9090
if respond_to?(:class_attribute)
@@ -151,16 +151,17 @@ def url_for(options = nil)
151151
end
152152

153153
protected
154-
def _with_routes(routes)
155-
old_routes, @_routes = @_routes, routes
156-
yield
157-
ensure
158-
@_routes = old_routes
159-
end
160154

161-
def _routes_context
162-
self
163-
end
155+
def _with_routes(routes)
156+
old_routes, @_routes = @_routes, routes
157+
yield
158+
ensure
159+
@_routes = old_routes
160+
end
161+
162+
def _routes_context
163+
self
164+
end
164165
end
165166
end
166167
end

actionpack/test/controller/action_pack_assertions_test.rb

-14
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,6 @@ def test_get_post_request_switch
159159
assert_equal @response.body, 'request method: GET'
160160
end
161161

162-
def test_redirect_to_named_route
163-
with_routing do |set|
164-
set.draw do
165-
match 'route_one', :to => 'action_pack_assertions#nothing', :as => :route_one
166-
match ':controller/:action'
167-
end
168-
set.install_helpers([ActionController::Base, ActionView::Base])
169-
170-
process :redirect_to_named_route
171-
assert_redirected_to 'http://test.host/route_one'
172-
assert_redirected_to route_one_url
173-
end
174-
end
175-
176162
def test_string_constraint
177163
with_routing do |set|
178164
set.draw do

0 commit comments

Comments
 (0)