Skip to content
This repository
Browse code

Install url helpers on module instance so they can be accessed

globally
  • Loading branch information...
commit 13a783672aad338b9c7ade5319ec7967768905d7 1 parent b652aa8
Joshua Peek authored March 17, 2010
18  actionpack/lib/action_dispatch/routing/route_set.rb
@@ -65,7 +65,7 @@ def split_glob_param!(params)
65 65
       # named routes.
66 66
       class NamedRouteCollection #:nodoc:
67 67
         include Enumerable
68  
-        attr_reader :routes, :helpers
  68
+        attr_reader :routes, :helpers, :module
69 69
 
70 70
         def initialize
71 71
           clear!
@@ -241,21 +241,29 @@ def install_helpers(destinations = [ActionController::Base, ActionView::Base], r
241 241
 
242 242
       def url_helpers
243 243
         @url_helpers ||= begin
244  
-          router = self
  244
+          routes = self
245 245
 
246  
-          Module.new do
  246
+          helpers = Module.new do
247 247
             extend ActiveSupport::Concern
248 248
             include UrlFor
249 249
 
  250
+            @routes = routes
  251
+            class << self
  252
+              delegate :url_for, :to => '@routes'
  253
+            end
  254
+            extend routes.named_routes.module
  255
+
250 256
             # ROUTES TODO: install_helpers isn't great... can we make a module with the stuff that
251 257
             # we can include?
252 258
             # Yes plz - JP
253 259
             included do
254  
-              router.install_helpers(self)
  260
+              routes.install_helpers(self)
255 261
             end
256 262
 
257  
-            define_method(:_router) { router }
  263
+            define_method(:_router) { routes }
258 264
           end
  265
+
  266
+          helpers
259 267
         end
260 268
       end
261 269
 
1  actionpack/test/dispatch/routing_test.rb
@@ -193,6 +193,7 @@ def test_login
193 193
       assert_equal '/login', url_for(:controller => 'sessions', :action => 'new', :only_path => true)
194 194
 
195 195
       assert_equal 'http://rubyonrails.org/login', Routes.url_for(:controller => 'sessions', :action => 'create')
  196
+      assert_equal 'http://rubyonrails.org/login', Routes.url_helpers.login_url
196 197
     end
197 198
   end
198 199
 

0 notes on commit 13a7836

Please sign in to comment.
Something went wrong with that request. Please try again.