Included a test for page names with spaces.
Upgraded to Rails 2.0.2 routing code. Kept the "old" CGI-style escaping rather than using URI.escape.
This commit is contained in:
parent
feed609d86
commit
f1106428dc
|
@ -46,8 +46,8 @@ class RoutesTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_cases_broken_by_routes
|
||||
# assert_routing('web/show/Page+With+Spaces',
|
||||
# :controller => 'wiki', :web => 'web', :action => 'show', :id => 'Page With Spaces')
|
||||
assert_routing('web/show/Page+With+Spaces',
|
||||
:controller => 'wiki', :web => 'web', :action => 'show', :id => 'Page With Spaces')
|
||||
# assert_routing('web/show/HomePage%2Fsomething_else',
|
||||
# :controller => 'wiki', :web => 'web', :action => 'show', :id => 'HomePage/something_else')
|
||||
end
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
require 'cgi'
|
||||
require 'uri'
|
||||
require 'action_controller/polymorphic_routes'
|
||||
require 'action_controller/routing_optimisation'
|
||||
|
||||
class Object
|
||||
def to_param
|
||||
|
@ -51,7 +54,7 @@ module ActionController
|
|||
#
|
||||
# The routing module provides URL rewriting in native Ruby. It's a way to
|
||||
# redirect incoming requests to controllers and actions. This replaces
|
||||
# mod_rewrite rules. Best of all Rails' Routing works with any web server.
|
||||
# mod_rewrite rules. Best of all, Rails' Routing works with any web server.
|
||||
# Routes are defined in routes.rb in your RAILS_ROOT/config directory.
|
||||
#
|
||||
# Consider the following route, installed by Rails when you generate your
|
||||
|
@ -60,13 +63,13 @@ module ActionController
|
|||
# map.connect ':controller/:action/:id'
|
||||
#
|
||||
# This route states that it expects requests to consist of a
|
||||
# :controller followed by an :action that in turns is fed by some :id
|
||||
# :controller followed by an :action that in turn is fed some :id.
|
||||
#
|
||||
# Suppose you get an incoming request for <tt>/blog/edit/22</tt>, you'll end up
|
||||
# with:
|
||||
#
|
||||
# params = { :controller => 'blog',
|
||||
# :action => 'edit'
|
||||
# :action => 'edit',
|
||||
# :id => '22'
|
||||
# }
|
||||
#
|
||||
|
@ -91,16 +94,16 @@ module ActionController
|
|||
# Not all routes are created equally. Routes have priority defined by the
|
||||
# order of appearance of the routes in the routes.rb file. The priority goes
|
||||
# from top to bottom. The last route in that file is at the lowest priority
|
||||
# will be applied last. If no route matches, 404 is returned.
|
||||
# and will be applied last. If no route matches, 404 is returned.
|
||||
#
|
||||
# Within blocks, the empty pattern goes first i.e. is at the highest priority.
|
||||
# Within blocks, the empty pattern is at the highest priority.
|
||||
# In practice this works out nicely:
|
||||
#
|
||||
# ActionController::Routing::Routes.draw do |map|
|
||||
# map.with_options :controller => 'blog' do |blog|
|
||||
# blog.show '', :action => 'list'
|
||||
# blog.show '', :action => 'list'
|
||||
# end
|
||||
# map.connect ':controller/:action/:view
|
||||
# map.connect ':controller/:action/:view'
|
||||
# end
|
||||
#
|
||||
# In this case, invoking blog controller (with an URL like '/blog/')
|
||||
|
@ -108,20 +111,20 @@ module ActionController
|
|||
#
|
||||
# == Defaults routes and default parameters
|
||||
#
|
||||
# Setting a default route is straightforward in Rails because by appending a
|
||||
# Hash to the end of your mapping you can set default parameters.
|
||||
# Setting a default route is straightforward in Rails - you simply append a
|
||||
# Hash at the end of your mapping to set any default parameters.
|
||||
#
|
||||
# Example:
|
||||
# ActionController::Routing:Routes.draw do |map|
|
||||
# map.connect ':controller/:action/:id', :controller => 'blog'
|
||||
# end
|
||||
#
|
||||
# This sets up +blog+ as the default controller if no other is specified.
|
||||
# This sets up +blog+ as the default controller if no other is specified.
|
||||
# This means visiting '/' would invoke the blog controller.
|
||||
#
|
||||
# More formally, you can define defaults in a route with the +:defaults+ key.
|
||||
#
|
||||
# map.connect ':controller/:id/:action', :action => 'show', :defaults => { :page => 'Dashboard' }
|
||||
# map.connect ':controller/:action/:id', :action => 'show', :defaults => { :page => 'Dashboard' }
|
||||
#
|
||||
# == Named routes
|
||||
#
|
||||
|
@ -140,7 +143,7 @@ module ActionController
|
|||
#
|
||||
# redirect_to show_item_path(:id => 25)
|
||||
#
|
||||
# Use <tt>map.root</tt> as a shorthand to name a route for the root path ""
|
||||
# Use <tt>map.root</tt> as a shorthand to name a route for the root path "".
|
||||
#
|
||||
# # In routes.rb
|
||||
# map.root :controller => 'blogs'
|
||||
|
@ -173,40 +176,60 @@ module ActionController
|
|||
# :controller => 'articles',
|
||||
# :action => 'find_by_date',
|
||||
# :year => /\d{4}/,
|
||||
# :month => /\d{1,2}/,
|
||||
# :day => /\d{1,2}/
|
||||
# :month => /\d{1,2}/,
|
||||
# :day => /\d{1,2}/
|
||||
#
|
||||
# # Using the route above, the url below maps to:
|
||||
# # params = {:year => '2005', :month => '11', :day => '06'}
|
||||
# # http://localhost:3000/articles/2005/11/06
|
||||
#
|
||||
# == Regular Expressions and parameters
|
||||
# You can specify a reqular expression to define a format for a parameter.
|
||||
# You can specify a regular expression to define a format for a parameter.
|
||||
#
|
||||
# map.geocode 'geocode/:postalcode', :controller => 'geocode',
|
||||
# :action => 'show', :postalcode => /\d{5}(-\d{4})?/
|
||||
#
|
||||
# or more formally:
|
||||
# or, more formally:
|
||||
#
|
||||
# map.geocode 'geocode/:postalcode', :controller => 'geocode',
|
||||
# :action => 'show',
|
||||
# :requirements { :postalcode => /\d{5}(-\d{4})?/ }
|
||||
# :action => 'show', :requirements => { :postalcode => /\d{5}(-\d{4})?/ }
|
||||
#
|
||||
# == Route globbing
|
||||
#
|
||||
# Specifying <tt>*[string]</tt> as part of a rule like :
|
||||
# Specifying <tt>*[string]</tt> as part of a rule like:
|
||||
#
|
||||
# map.connect '*path' , :controller => 'blog' , :action => 'unrecognized?'
|
||||
#
|
||||
# will glob all remaining parts of the route that were not recognized earlier. This idiom must appear at the end of the path. The globbed values are in <tt>params[:path]</tt> in this case.
|
||||
# will glob all remaining parts of the route that were not recognized earlier. This idiom
|
||||
# must appear at the end of the path. The globbed values are in <tt>params[:path]</tt> in
|
||||
# this case.
|
||||
#
|
||||
# == Route conditions
|
||||
#
|
||||
# With conditions you can define restrictions on routes. Currently the only valid condition is <tt>:method</tt>.
|
||||
#
|
||||
# * <tt>:method</tt> - Allows you to specify which method can access the route. Possible values are <tt>:post</tt>,
|
||||
# <tt>:get</tt>, <tt>:put</tt>, <tt>:delete</tt> and <tt>:any</tt>. The default value is <tt>:any</tt>,
|
||||
# <tt>:any</tt> means that any method can access the route.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# map.connect 'post/:id', :controller => 'posts', :action => 'show',
|
||||
# :conditions => { :method => :get }
|
||||
# map.connect 'post/:id', :controller => 'posts', :action => 'create_comment',
|
||||
# :conditions => { :method => :post }
|
||||
#
|
||||
# Now, if you POST to <tt>/posts/:id</tt>, it will route to the <tt>create_comment</tt> action. A GET on the same
|
||||
# URL will route to the <tt>show</tt> action.
|
||||
#
|
||||
# == Reloading routes
|
||||
#
|
||||
# You can reload routes if you feel you must:
|
||||
#
|
||||
# Action::Controller::Routes.reload
|
||||
# ActionController::Routing::Routes.reload
|
||||
#
|
||||
# This will clear all named routes and reload routes.rb
|
||||
# This will clear all named routes and reload routes.rb if the file has been modified from
|
||||
# last load. To absolutely force reloading, use +reload!+.
|
||||
#
|
||||
# == Testing Routes
|
||||
#
|
||||
|
@ -229,7 +252,7 @@ module ActionController
|
|||
# end
|
||||
#
|
||||
# Note the subtle difference between the two: +assert_routing+ tests that
|
||||
# an URL fits options while +assert_recognizes+ tests that an URL
|
||||
# a URL fits options while +assert_recognizes+ tests that a URL
|
||||
# breaks into parameters properly.
|
||||
#
|
||||
# In tests you can simply pass the URL or named route to +get+ or +post+.
|
||||
|
@ -245,13 +268,26 @@ module ActionController
|
|||
# #...
|
||||
# end
|
||||
#
|
||||
# == View a list of all your routes
|
||||
#
|
||||
# Run <tt>rake routes</tt>.
|
||||
#
|
||||
module Routing
|
||||
SEPARATORS = %w( / ; . , ? )
|
||||
SEPARATORS = %w( / . ? )
|
||||
|
||||
HTTP_METHODS = [:get, :head, :post, :put, :delete]
|
||||
|
||||
ALLOWED_REQUIREMENTS_FOR_OPTIMISATION = [:controller, :action].to_set
|
||||
|
||||
# The root paths which may contain controller files
|
||||
mattr_accessor :controller_paths
|
||||
self.controller_paths = []
|
||||
|
||||
# A helper module to hold URL related helpers.
|
||||
module Helpers
|
||||
include PolymorphicRoutes
|
||||
end
|
||||
|
||||
class << self
|
||||
def with_controllers(names)
|
||||
prior_controllers = @possible_controllers
|
||||
|
@ -317,12 +353,25 @@ module ActionController
|
|||
end
|
||||
|
||||
class Route #:nodoc:
|
||||
attr_accessor :segments, :requirements, :conditions
|
||||
attr_accessor :segments, :requirements, :conditions, :optimise
|
||||
|
||||
def initialize
|
||||
@segments = []
|
||||
@requirements = {}
|
||||
@conditions = {}
|
||||
@optimise = true
|
||||
end
|
||||
|
||||
# Indicates whether the routes should be optimised with the string interpolation
|
||||
# version of the named routes methods.
|
||||
def optimise?
|
||||
@optimise && ActionController::Base::optimise_named_routes
|
||||
end
|
||||
|
||||
def segment_keys
|
||||
segments.collect do |segment|
|
||||
segment.key if segment.respond_to? :key
|
||||
end.compact
|
||||
end
|
||||
|
||||
# Write and compile a +generate+ method for this Route.
|
||||
|
@ -373,6 +422,7 @@ module ActionController
|
|||
end
|
||||
requirement_conditions * ' && ' unless requirement_conditions.empty?
|
||||
end
|
||||
|
||||
def generation_structure
|
||||
segments.last.string_structure segments[0..-2]
|
||||
end
|
||||
|
@ -411,7 +461,7 @@ module ActionController
|
|||
def recognition_extraction
|
||||
next_capture = 1
|
||||
extraction = segments.collect do |segment|
|
||||
x = segment.match_extraction next_capture
|
||||
x = segment.match_extraction(next_capture)
|
||||
next_capture += Regexp.new(segment.regexp_chunk).number_of_captures
|
||||
x
|
||||
end
|
||||
|
@ -459,6 +509,7 @@ module ActionController
|
|||
elements << value.to_query(key)
|
||||
end
|
||||
end
|
||||
|
||||
elements.empty? ? '' : "?#{elements.sort * '&'}"
|
||||
end
|
||||
|
||||
|
@ -510,7 +561,7 @@ module ActionController
|
|||
end
|
||||
|
||||
def matches_controller_and_action?(controller, action)
|
||||
unless @matching_prepared
|
||||
unless defined? @matching_prepared
|
||||
@controller_requirement = requirement_for(:controller)
|
||||
@action_requirement = requirement_for(:action)
|
||||
@matching_prepared = true
|
||||
|
@ -539,6 +590,9 @@ module ActionController
|
|||
end
|
||||
|
||||
class Segment #:nodoc:
|
||||
# RESERVED_PCHAR = ':@&=+$,;'
|
||||
# UNSAFE_PCHAR = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}]", false, 'N').freeze
|
||||
|
||||
attr_accessor :is_optional
|
||||
alias_method :optional?, :is_optional
|
||||
|
||||
|
@ -560,6 +614,10 @@ module ActionController
|
|||
end
|
||||
end
|
||||
|
||||
def interpolation_chunk
|
||||
CGI.escape(value)
|
||||
end
|
||||
|
||||
# Return a string interpolation statement for this segment and those before it.
|
||||
def interpolation_statement(prior_segments)
|
||||
chunks = prior_segments.collect { |s| s.interpolation_chunk }
|
||||
|
@ -603,11 +661,11 @@ module ActionController
|
|||
end
|
||||
|
||||
def interpolation_chunk
|
||||
raw? ? value : CGI.escape(value)
|
||||
raw? ? value : super
|
||||
end
|
||||
|
||||
def regexp_chunk
|
||||
chunk = Regexp.escape value
|
||||
chunk = Regexp.escape(value)
|
||||
optional? ? Regexp.optionalize(chunk) : chunk
|
||||
end
|
||||
|
||||
|
@ -683,8 +741,8 @@ module ActionController
|
|||
s << "\n#{expiry_statement}"
|
||||
end
|
||||
|
||||
def interpolation_chunk
|
||||
"\#{CGI.escape(#{local_name}.to_s)}"
|
||||
def interpolation_chunk(value_code = "#{local_name}")
|
||||
"\#{CGI.escape(#{value_code}.to_s)}"
|
||||
end
|
||||
|
||||
def string_structure(prior_segments)
|
||||
|
@ -715,10 +773,17 @@ module ActionController
|
|||
optional? ? Regexp.optionalize(pattern) : pattern
|
||||
end
|
||||
def match_extraction(next_capture)
|
||||
hangon = (default ? "|| #{default.inspect}" : "if match[#{next_capture}]")
|
||||
|
||||
# All non code-related keys (such as :id, :slug) have to be unescaped as other CGI params
|
||||
"params[:#{key}] = match[#{next_capture}] #{hangon}"
|
||||
# All non code-related keys (such as :id, :slug) are URI-unescaped as
|
||||
# path parameters.
|
||||
default_value = default ? default.inspect : nil
|
||||
%[
|
||||
value = if (m = match[#{next_capture}])
|
||||
CGI.unescape(m)
|
||||
else
|
||||
#{default_value}
|
||||
end
|
||||
params[:#{key}] = value if value
|
||||
]
|
||||
end
|
||||
|
||||
def optionality_implied?
|
||||
|
@ -733,10 +798,9 @@ module ActionController
|
|||
"(?i-:(#{(regexp || Regexp.union(*possible_names)).source}))"
|
||||
end
|
||||
|
||||
# Don't CGI.escape the controller name, since it may have slashes in it,
|
||||
# like admin/foo.
|
||||
def interpolation_chunk
|
||||
"\#{#{local_name}.to_s}"
|
||||
# Don't CGI.escape the controller name since it may contain slashes.
|
||||
def interpolation_chunk(value_code = "#{local_name}")
|
||||
"\#{#{value_code}.to_s}"
|
||||
end
|
||||
|
||||
# Make sure controller names like Admin/Content are correctly normalized to
|
||||
|
@ -755,9 +819,12 @@ module ActionController
|
|||
end
|
||||
|
||||
class PathSegment < DynamicSegment #:nodoc:
|
||||
EscapedSlash = CGI.escape("/")
|
||||
def interpolation_chunk
|
||||
"\#{CGI.escape(#{local_name}.to_s).gsub(#{EscapedSlash.inspect}, '/')}"
|
||||
# RESERVED_PCHAR = "#{Segment::RESERVED_PCHAR}/"
|
||||
# UNSAFE_PCHAR = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}]", false, 'N').freeze
|
||||
|
||||
def interpolation_chunk(value_code = "#{local_name}")
|
||||
# "\#{URI.escape(#{value_code}.to_s, ActionController::Routing::PathSegment::UNSAFE_PCHAR)}"
|
||||
"\#{CGI.escape(#{value_code})}"
|
||||
end
|
||||
|
||||
def default
|
||||
|
@ -776,6 +843,10 @@ module ActionController
|
|||
regexp || "(.*)"
|
||||
end
|
||||
|
||||
def optionality_implied?
|
||||
true
|
||||
end
|
||||
|
||||
class Result < ::Array #:nodoc:
|
||||
def to_s() join '/' end
|
||||
def self.new_escaped(strings)
|
||||
|
@ -845,6 +916,14 @@ module ActionController
|
|||
# and requirements.
|
||||
def divide_route_options(segments, options)
|
||||
options = options.dup
|
||||
|
||||
if options[:namespace]
|
||||
options[:controller] = "#{options[:path_prefix]}/#{options[:controller]}"
|
||||
options.delete(:path_prefix)
|
||||
options.delete(:name_prefix)
|
||||
options.delete(:namespace)
|
||||
end
|
||||
|
||||
requirements = (options.delete(:requirements) || {}).dup
|
||||
defaults = (options.delete(:defaults) || {}).dup
|
||||
conditions = (options.delete(:conditions) || {}).dup
|
||||
|
@ -925,7 +1004,7 @@ module ActionController
|
|||
warn "Route segment \"#{segment.to_s}\" cannot be optional because it precedes a required segment. This segment will be required."
|
||||
end
|
||||
segment.is_optional = false
|
||||
elsif allow_optional & segment.respond_to?(:default) && segment.default
|
||||
elsif allow_optional && segment.respond_to?(:default) && segment.default
|
||||
# if a segment has a default, then it is optional
|
||||
segment.is_optional = true
|
||||
end
|
||||
|
@ -938,11 +1017,14 @@ module ActionController
|
|||
path = "/#{path}" unless path[0] == ?/
|
||||
path = "#{path}/" unless path[-1] == ?/
|
||||
|
||||
path = "/#{options[:path_prefix].to_s.gsub(/^\//,'')}#{path}" if options[:path_prefix]
|
||||
|
||||
segments = segments_for_route_path(path)
|
||||
defaults, requirements, conditions = divide_route_options(segments, options)
|
||||
requirements = assign_route_options(segments, defaults, requirements)
|
||||
|
||||
route = Route.new
|
||||
|
||||
route.segments = segments
|
||||
route.requirements = requirements
|
||||
route.conditions = conditions
|
||||
|
@ -952,6 +1034,13 @@ module ActionController
|
|||
route.significant_keys << :action
|
||||
end
|
||||
|
||||
# Routes cannot use the current string interpolation method
|
||||
# if there are user-supplied :requirements as the interpolation
|
||||
# code won't raise RoutingErrors when generating
|
||||
if options.key?(:requirements) || route.requirements.keys.to_set != Routing::ALLOWED_REQUIREMENTS_FOR_OPTIMISATION
|
||||
route.optimise = false
|
||||
end
|
||||
|
||||
if !route.significant_keys.include?(:controller)
|
||||
raise ArgumentError, "Illegal route: the :controller must be specified!"
|
||||
end
|
||||
|
@ -966,34 +1055,46 @@ module ActionController
|
|||
#
|
||||
# Mapper instances have relatively few instance methods, in order to avoid
|
||||
# clashes with named routes.
|
||||
class Mapper #:nodoc:
|
||||
def initialize(set)
|
||||
class Mapper #:doc:
|
||||
def initialize(set) #:nodoc:
|
||||
@set = set
|
||||
end
|
||||
|
||||
# Create an unnamed route with the provided +path+ and +options+. See
|
||||
# SomeHelpfulUrl for an introduction to routes.
|
||||
# ActionController::Routing for an introduction to routes.
|
||||
def connect(path, options = {})
|
||||
@set.add_route(path, options)
|
||||
end
|
||||
|
||||
def named_route(name, path, options = {})
|
||||
# Creates a named route called "root" for matching the root level request.
|
||||
def root(options = {})
|
||||
named_route("root", '', options)
|
||||
end
|
||||
|
||||
def named_route(name, path, options = {}) #:nodoc:
|
||||
@set.add_named_route(name, path, options)
|
||||
end
|
||||
|
||||
def deprecated_named_route(name, deprecated_name, options = {})
|
||||
@set.add_deprecated_named_route(name, deprecated_name)
|
||||
# Enables the use of resources in a module by setting the name_prefix, path_prefix, and namespace for the model.
|
||||
# Example:
|
||||
#
|
||||
# map.namespace(:admin) do |admin|
|
||||
# admin.resources :products,
|
||||
# :has_many => [ :tags, :images, :variants ]
|
||||
# end
|
||||
#
|
||||
# This will create +admin_products_url+ pointing to "admin/products", which will look for an Admin::ProductsController.
|
||||
# It'll also create +admin_product_tags_url+ pointing to "admin/products/#{product_id}/tags", which will look for
|
||||
# Admin::TagsController.
|
||||
def namespace(name, options = {}, &block)
|
||||
if options[:namespace]
|
||||
with_options({:path_prefix => "#{options.delete(:path_prefix)}/#{name}", :name_prefix => "#{options.delete(:name_prefix)}#{name}_", :namespace => "#{options.delete(:namespace)}#{name}/" }.merge(options), &block)
|
||||
else
|
||||
with_options({:path_prefix => name, :name_prefix => "#{name}_", :namespace => "#{name}/" }.merge(options), &block)
|
||||
end
|
||||
end
|
||||
|
||||
# Added deprecation notice for anyone who already added a named route called "root".
|
||||
# It'll be used as a shortcut for map.connect '' in Rails 2.0.
|
||||
def root(*args, &proc)
|
||||
super unless args.length >= 1 && proc.nil?
|
||||
@set.add_named_route("root", *args)
|
||||
end
|
||||
deprecate :root => "(as the the label for a named route) will become a shortcut for map.connect '', so find another name"
|
||||
|
||||
def method_missing(route_name, *args, &proc)
|
||||
def method_missing(route_name, *args, &proc) #:nodoc:
|
||||
super unless args.length >= 1 && proc.nil?
|
||||
@set.add_named_route(route_name, *args)
|
||||
end
|
||||
|
@ -1004,7 +1105,7 @@ module ActionController
|
|||
# named routes.
|
||||
class NamedRouteCollection #:nodoc:
|
||||
include Enumerable
|
||||
|
||||
include ActionController::Routing::Optimisation
|
||||
attr_reader :routes, :helpers
|
||||
|
||||
def initialize
|
||||
|
@ -1017,7 +1118,7 @@ module ActionController
|
|||
|
||||
@module ||= Module.new
|
||||
@module.instance_methods.each do |selector|
|
||||
@module.send :remove_method, selector
|
||||
@module.class_eval { remove_method selector }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -1047,40 +1148,19 @@ module ActionController
|
|||
routes.length
|
||||
end
|
||||
|
||||
def install(destinations = [ActionController::Base, ActionView::Base])
|
||||
Array(destinations).each { |dest| dest.send :include, @module }
|
||||
def reset!
|
||||
old_routes = routes.dup
|
||||
clear!
|
||||
old_routes.each do |name, route|
|
||||
add(name, route)
|
||||
end
|
||||
end
|
||||
|
||||
def define_deprecated_named_route_methods(name, deprecated_name)
|
||||
|
||||
[:url, :path].each do |kind|
|
||||
@module.send :module_eval, <<-end_eval # We use module_eval to avoid leaks
|
||||
|
||||
def #{url_helper_name(deprecated_name, kind)}(*args)
|
||||
|
||||
ActiveSupport::Deprecation.warn(
|
||||
'The named route "#{url_helper_name(deprecated_name, kind)}" uses a format that has been deprecated. ' +
|
||||
'You should use "#{url_helper_name(name, kind)}" instead.', caller
|
||||
)
|
||||
|
||||
send :#{url_helper_name(name, kind)}, *args
|
||||
|
||||
end
|
||||
|
||||
def #{hash_access_name(deprecated_name, kind)}(*args)
|
||||
|
||||
ActiveSupport::Deprecation.warn(
|
||||
'The named route "#{hash_access_name(deprecated_name, kind)}" uses a format that has been deprecated. ' +
|
||||
'You should use "#{hash_access_name(name, kind)}" instead.', caller
|
||||
)
|
||||
|
||||
send :#{hash_access_name(name, kind)}, *args
|
||||
|
||||
end
|
||||
|
||||
end_eval
|
||||
def install(destinations = [ActionController::Base, ActionView::Base], regenerate = false)
|
||||
reset! if regenerate
|
||||
Array(destinations).each do |dest|
|
||||
dest.send! :include, @module
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -1102,50 +1182,55 @@ module ActionController
|
|||
|
||||
def define_hash_access(route, name, kind, options)
|
||||
selector = hash_access_name(name, kind)
|
||||
@module.send :module_eval, <<-end_eval # We use module_eval to avoid leaks
|
||||
@module.module_eval <<-end_eval # We use module_eval to avoid leaks
|
||||
def #{selector}(options = nil)
|
||||
options ? #{options.inspect}.merge(options) : #{options.inspect}
|
||||
end
|
||||
protected :#{selector}
|
||||
end_eval
|
||||
@module.send(:protected, selector)
|
||||
helpers << selector
|
||||
end
|
||||
|
||||
def define_url_helper(route, name, kind, options)
|
||||
selector = url_helper_name(name, kind)
|
||||
|
||||
# The segment keys used for positional paramters
|
||||
segment_keys = route.segments.collect do |segment|
|
||||
segment.key if segment.respond_to? :key
|
||||
end.compact
|
||||
|
||||
hash_access_method = hash_access_name(name, kind)
|
||||
|
||||
@module.send :module_eval, <<-end_eval # We use module_eval to avoid leaks
|
||||
# allow ordered parameters to be associated with corresponding
|
||||
# dynamic segments, so you can do
|
||||
#
|
||||
# foo_url(bar, baz, bang)
|
||||
#
|
||||
# instead of
|
||||
#
|
||||
# foo_url(:bar => bar, :baz => baz, :bang => bang)
|
||||
#
|
||||
# Also allow options hash, so you can do
|
||||
#
|
||||
# foo_url(bar, baz, bang, :sort_by => 'baz')
|
||||
#
|
||||
@module.module_eval <<-end_eval # We use module_eval to avoid leaks
|
||||
def #{selector}(*args)
|
||||
#{generate_optimisation_block(route, kind)}
|
||||
|
||||
opts = if args.empty? || Hash === args.first
|
||||
args.first || {}
|
||||
else
|
||||
# allow ordered parameters to be associated with corresponding
|
||||
# dynamic segments, so you can do
|
||||
#
|
||||
# foo_url(bar, baz, bang)
|
||||
#
|
||||
# instead of
|
||||
#
|
||||
# foo_url(:bar => bar, :baz => baz, :bang => bang)
|
||||
args.zip(#{segment_keys.inspect}).inject({}) do |h, (v, k)|
|
||||
options = args.last.is_a?(Hash) ? args.pop : {}
|
||||
args = args.zip(#{route.segment_keys.inspect}).inject({}) do |h, (v, k)|
|
||||
h[k] = v
|
||||
h
|
||||
end
|
||||
options.merge(args)
|
||||
end
|
||||
|
||||
url_for(#{hash_access_method}(opts))
|
||||
end
|
||||
protected :#{selector}
|
||||
end_eval
|
||||
@module.send(:protected, selector)
|
||||
helpers << selector
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
attr_accessor :routes, :named_routes
|
||||
|
@ -1164,7 +1249,7 @@ module ActionController
|
|||
def draw
|
||||
clear!
|
||||
yield Mapper.new(self)
|
||||
named_routes.install
|
||||
install_helpers
|
||||
end
|
||||
|
||||
def clear!
|
||||
|
@ -1174,6 +1259,11 @@ module ActionController
|
|||
@routes_by_controller = nil
|
||||
end
|
||||
|
||||
def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false)
|
||||
Array(destinations).each { |d| d.module_eval { include Helpers } }
|
||||
named_routes.install(destinations, regenerate_code)
|
||||
end
|
||||
|
||||
def empty?
|
||||
routes.empty?
|
||||
end
|
||||
|
@ -1182,14 +1272,27 @@ module ActionController
|
|||
Routing.use_controllers! nil # Clear the controller cache so we may discover new ones
|
||||
clear!
|
||||
load_routes!
|
||||
named_routes.install
|
||||
install_helpers
|
||||
end
|
||||
|
||||
alias reload load!
|
||||
# reload! will always force a reload whereas load checks the timestamp first
|
||||
alias reload! load!
|
||||
|
||||
def reload
|
||||
if @routes_last_modified && defined?(RAILS_ROOT)
|
||||
mtime = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime
|
||||
# if it hasn't been changed, then just return
|
||||
return if mtime == @routes_last_modified
|
||||
# if it has changed then record the new time and fall to the load! below
|
||||
@routes_last_modified = mtime
|
||||
end
|
||||
load!
|
||||
end
|
||||
|
||||
def load_routes!
|
||||
if defined?(RAILS_ROOT) && defined?(::ActionController::Routing::Routes) && self == ::ActionController::Routing::Routes
|
||||
load File.join("#{RAILS_ROOT}/config/routes.rb")
|
||||
@routes_last_modified = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime
|
||||
else
|
||||
add_route ":controller/:action/:id"
|
||||
end
|
||||
|
@ -1202,11 +1305,9 @@ module ActionController
|
|||
end
|
||||
|
||||
def add_named_route(name, path, options = {})
|
||||
named_routes[name] = add_route(path, options)
|
||||
end
|
||||
|
||||
def add_deprecated_named_route(name, deprecated_name)
|
||||
named_routes.define_deprecated_named_route_methods(name, deprecated_name)
|
||||
# TODO - is options EVER used?
|
||||
name = options[:name_prefix] + name.to_s if options[:name_prefix]
|
||||
named_routes[name.to_sym] = add_route(path, options)
|
||||
end
|
||||
|
||||
def options_as_params(options)
|
||||
|
@ -1229,7 +1330,7 @@ module ActionController
|
|||
|
||||
def build_expiry(options, recall)
|
||||
recall.inject({}) do |expiry, (key, recalled_value)|
|
||||
expiry[key] = (options.key?(key) && options[key] != recalled_value)
|
||||
expiry[key] = (options.key?(key) && options[key].to_param != recalled_value.to_param)
|
||||
expiry
|
||||
end
|
||||
end
|
||||
|
@ -1246,6 +1347,7 @@ module ActionController
|
|||
|
||||
def generate(options, recall = {}, method=:generate)
|
||||
named_route_name = options.delete(:use_route)
|
||||
generate_all = options.delete(:generate_all)
|
||||
if named_route_name
|
||||
named_route = named_routes[named_route_name]
|
||||
options = named_route.parameter_shell.merge(options)
|
||||
|
@ -1287,11 +1389,19 @@ module ActionController
|
|||
action = merged[:action]
|
||||
|
||||
raise RoutingError, "Need controller and action!" unless controller && action
|
||||
|
||||
if generate_all
|
||||
# Used by caching to expire all paths for a resource
|
||||
return routes.collect do |route|
|
||||
route.send!(method, options, merged, expire_on)
|
||||
end.compact
|
||||
end
|
||||
|
||||
# don't use the recalled keys when determining which routes to check
|
||||
routes = routes_by_controller[controller][action][options.keys.sort_by { |x| x.object_id }]
|
||||
|
||||
routes.each do |route|
|
||||
results = route.send(method, options, merged, expire_on)
|
||||
results = route.send!(method, options, merged, expire_on)
|
||||
return results if results && (!results.is_a?(Array) || results.first)
|
||||
end
|
||||
end
|
||||
|
@ -1307,7 +1417,7 @@ module ActionController
|
|||
else
|
||||
required_segments = named_route.segments.select {|seg| (!seg.optional?) && (!seg.is_a?(DividerSegment)) }
|
||||
required_keys_or_values = required_segments.map { |seg| seg.key rescue seg.value } # we want either the key or the value from the segment
|
||||
raise RoutingError, "#{named_route_name}_url failed to generate from #{options.inspect} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: #{required_keys_or_values.inspect} - are they all satisifed?"
|
||||
raise RoutingError, "#{named_route_name}_url failed to generate from #{options.inspect} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: #{required_keys_or_values.inspect} - are they all satisfied?"
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -1318,11 +1428,19 @@ module ActionController
|
|||
end
|
||||
|
||||
def recognize_path(path, environment={})
|
||||
path = CGI.unescape(path)
|
||||
routes.each do |route|
|
||||
result = route.recognize(path, environment) and return result
|
||||
end
|
||||
raise RoutingError, "no route found to match #{path.inspect} with #{environment.inspect}"
|
||||
|
||||
allows = HTTP_METHODS.select { |verb| routes.find { |r| r.recognize(path, :method => verb) } }
|
||||
|
||||
if environment[:method] && !HTTP_METHODS.include?(environment[:method])
|
||||
raise NotImplemented.new(*allows)
|
||||
elsif !allows.empty?
|
||||
raise MethodNotAllowed.new(*allows)
|
||||
else
|
||||
raise RoutingError, "No route matches #{path.inspect} with #{environment.inspect}"
|
||||
end
|
||||
end
|
||||
|
||||
def routes_by_controller
|
||||
|
@ -1368,6 +1486,15 @@ module ActionController
|
|||
end
|
||||
|
||||
Routes = RouteSet.new
|
||||
|
||||
::Inflector.module_eval do
|
||||
def inflections_with_route_reloading(&block)
|
||||
returning(inflections_without_route_reloading(&block)) {
|
||||
ActionController::Routing::Routes.reload! if block_given?
|
||||
}
|
||||
end
|
||||
|
||||
alias_method_chain :inflections, :route_reloading
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue