提交 49b6b499 编写于 作者: J José Valim

Clean up routes inclusion and add some comments for the next soul that decides...

Clean up routes inclusion and add some comments for the next soul that decides to adventure on this code.
上级 439d3407
...@@ -284,10 +284,5 @@ module Routing ...@@ -284,10 +284,5 @@ module Routing
SEPARATORS = %w( / . ? ) #:nodoc: SEPARATORS = %w( / . ? ) #:nodoc:
HTTP_METHODS = [:get, :head, :post, :put, :delete, :options] #:nodoc: HTTP_METHODS = [:get, :head, :post, :put, :delete, :options] #:nodoc:
# A helper module to hold URL related helpers.
module Helpers #:nodoc:
include PolymorphicRoutes
end
end end
end end
...@@ -123,11 +123,6 @@ def length ...@@ -123,11 +123,6 @@ def length
routes.length routes.length
end end
def install(destinations = [ActionController::Base, ActionView::Base])
helper = @module
destinations.each { |d| d.module_eval { include helper } }
end
private private
def url_helper_name(name, kind = :url) def url_helper_name(name, kind = :url)
:"#{name}_#{kind}" :"#{name}_#{kind}"
...@@ -276,14 +271,15 @@ def clear! ...@@ -276,14 +271,15 @@ def clear!
@prepend.each { |blk| eval_block(blk) } @prepend.each { |blk| eval_block(blk) }
end end
def install_helpers(destinations) module MountedHelpers #:nodoc:
destinations.each { |d| d.module_eval { include Helpers } } extend ActiveSupport::Concern
named_routes.install(destinations) include UrlFor
end
module MountedHelpers
end end
# Contains all the mounted helpers accross different
# engines and the `main_app` helper for the application.
# You can include this in your classes if you want to
# access routes for other engines.
def mounted_helpers def mounted_helpers
MountedHelpers MountedHelpers
end end
...@@ -294,7 +290,7 @@ def define_mounted_helper(name) ...@@ -294,7 +290,7 @@ def define_mounted_helper(name)
routes = self routes = self
MountedHelpers.class_eval do MountedHelpers.class_eval do
define_method "_#{name}" do define_method "_#{name}" do
RoutesProxy.new(routes, self._routes_context) RoutesProxy.new(routes, _routes_context)
end end
end end
...@@ -306,29 +302,40 @@ def #{name} ...@@ -306,29 +302,40 @@ def #{name}
end end
def url_helpers def url_helpers
routes = self @url_helpers ||= begin
routes = self
Module.new do
extend ActiveSupport::Concern
include UrlFor
# Define url_for in the singleton level so one can do:
# Rails.application.routes.url_helpers.url_for(args)
@_routes = routes
class << self
delegate :url_for, :to => '@_routes'
end
@url_helpers ||= Module.new { # Make named_routes available in the module singleton
extend ActiveSupport::Concern # as well, so one can do:
include UrlFor # Rails.application.routes.url_helpers.posts_path
extend routes.named_routes.module
@_routes = routes # Any class that includes this module will get all
def self.url_for(options) # named routes...
@_routes.url_for options include routes.named_routes.module
end
extend routes.named_routes.module # plus a singleton class method called _routes ...
included do
singleton_class.send(:redefine_method, :_routes) { routes }
end
# ROUTES TODO: install_helpers isn't great... can we make a module with the stuff that # And an instance method _routes. Note that
# we can include? # UrlFor (included in this module) add extra
# Yes plz - JP # conveniences for working with @_routes.
included do define_method(:_routes) { @_routes || routes }
routes.install_helpers([self])
singleton_class.send(:redefine_method, :_routes) { routes }
end end
end
define_method(:_routes) { @_routes || routes }
}
end end
def empty? def empty?
......
...@@ -8,7 +8,8 @@ module Routing ...@@ -8,7 +8,8 @@ module Routing
# #
# <b>Tip:</b> If you need to generate URLs from your models or some other place, # <b>Tip:</b> If you need to generate URLs from your models or some other place,
# then ActionController::UrlFor is what you're looking for. Read on for # then ActionController::UrlFor is what you're looking for. Read on for
# an introduction. # an introduction. In general, this module should not be included on its own,
# as it is usually included by url_helpers (as in Rails.application.routes.url_helpers).
# #
# == URL generation from parameters # == URL generation from parameters
# #
...@@ -84,7 +85,6 @@ module UrlFor ...@@ -84,7 +85,6 @@ module UrlFor
include PolymorphicRoutes include PolymorphicRoutes
included do included do
# TODO: with_routing extends @controller with url_helpers, trickling down to including this module which overrides its default_url_options
unless method_defined?(:default_url_options) unless method_defined?(:default_url_options)
# Including in a class uses an inheritable hash. Modules get a plain hash. # Including in a class uses an inheritable hash. Modules get a plain hash.
if respond_to?(:class_attribute) if respond_to?(:class_attribute)
...@@ -151,16 +151,17 @@ def url_for(options = nil) ...@@ -151,16 +151,17 @@ def url_for(options = nil)
end end
protected protected
def _with_routes(routes)
old_routes, @_routes = @_routes, routes
yield
ensure
@_routes = old_routes
end
def _routes_context def _with_routes(routes)
self old_routes, @_routes = @_routes, routes
end yield
ensure
@_routes = old_routes
end
def _routes_context
self
end
end end
end end
end end
...@@ -159,20 +159,6 @@ def test_get_post_request_switch ...@@ -159,20 +159,6 @@ def test_get_post_request_switch
assert_equal @response.body, 'request method: GET' assert_equal @response.body, 'request method: GET'
end end
def test_redirect_to_named_route
with_routing do |set|
set.draw do
match 'route_one', :to => 'action_pack_assertions#nothing', :as => :route_one
match ':controller/:action'
end
set.install_helpers([ActionController::Base, ActionView::Base])
process :redirect_to_named_route
assert_redirected_to 'http://test.host/route_one'
assert_redirected_to route_one_url
end
end
def test_string_constraint def test_string_constraint
with_routing do |set| with_routing do |set|
set.draw do set.draw do
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册