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
|
end
|
||||||
|
|
||||||
def test_cases_broken_by_routes
|
def test_cases_broken_by_routes
|
||||||
# assert_routing('web/show/Page+With+Spaces',
|
assert_routing('web/show/Page+With+Spaces',
|
||||||
# :controller => 'wiki', :web => 'web', :action => 'show', :id => 'Page With Spaces')
|
:controller => 'wiki', :web => 'web', :action => 'show', :id => 'Page With Spaces')
|
||||||
# assert_routing('web/show/HomePage%2Fsomething_else',
|
# assert_routing('web/show/HomePage%2Fsomething_else',
|
||||||
# :controller => 'wiki', :web => 'web', :action => 'show', :id => 'HomePage/something_else')
|
# :controller => 'wiki', :web => 'web', :action => 'show', :id => 'HomePage/something_else')
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
require 'cgi'
|
require 'cgi'
|
||||||
|
require 'uri'
|
||||||
|
require 'action_controller/polymorphic_routes'
|
||||||
|
require 'action_controller/routing_optimisation'
|
||||||
|
|
||||||
class Object
|
class Object
|
||||||
def to_param
|
def to_param
|
||||||
|
@ -51,7 +54,7 @@ module ActionController
|
||||||
#
|
#
|
||||||
# The routing module provides URL rewriting in native Ruby. It's a way to
|
# The routing module provides URL rewriting in native Ruby. It's a way to
|
||||||
# redirect incoming requests to controllers and actions. This replaces
|
# 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.
|
# Routes are defined in routes.rb in your RAILS_ROOT/config directory.
|
||||||
#
|
#
|
||||||
# Consider the following route, installed by Rails when you generate your
|
# Consider the following route, installed by Rails when you generate your
|
||||||
|
@ -60,13 +63,13 @@ module ActionController
|
||||||
# map.connect ':controller/:action/:id'
|
# map.connect ':controller/:action/:id'
|
||||||
#
|
#
|
||||||
# This route states that it expects requests to consist of a
|
# 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
|
# Suppose you get an incoming request for <tt>/blog/edit/22</tt>, you'll end up
|
||||||
# with:
|
# with:
|
||||||
#
|
#
|
||||||
# params = { :controller => 'blog',
|
# params = { :controller => 'blog',
|
||||||
# :action => 'edit'
|
# :action => 'edit',
|
||||||
# :id => '22'
|
# :id => '22'
|
||||||
# }
|
# }
|
||||||
#
|
#
|
||||||
|
@ -91,16 +94,16 @@ module ActionController
|
||||||
# Not all routes are created equally. Routes have priority defined by the
|
# 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
|
# 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
|
# 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:
|
# In practice this works out nicely:
|
||||||
#
|
#
|
||||||
# ActionController::Routing::Routes.draw do |map|
|
# ActionController::Routing::Routes.draw do |map|
|
||||||
# map.with_options :controller => 'blog' do |blog|
|
# map.with_options :controller => 'blog' do |blog|
|
||||||
# blog.show '', :action => 'list'
|
# blog.show '', :action => 'list'
|
||||||
# end
|
# end
|
||||||
# map.connect ':controller/:action/:view
|
# map.connect ':controller/:action/:view'
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# In this case, invoking blog controller (with an URL like '/blog/')
|
# In this case, invoking blog controller (with an URL like '/blog/')
|
||||||
|
@ -108,20 +111,20 @@ module ActionController
|
||||||
#
|
#
|
||||||
# == Defaults routes and default parameters
|
# == Defaults routes and default parameters
|
||||||
#
|
#
|
||||||
# Setting a default route is straightforward in Rails because by appending a
|
# Setting a default route is straightforward in Rails - you simply append a
|
||||||
# Hash to the end of your mapping you can set default parameters.
|
# Hash at the end of your mapping to set any default parameters.
|
||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
# ActionController::Routing:Routes.draw do |map|
|
# ActionController::Routing:Routes.draw do |map|
|
||||||
# map.connect ':controller/:action/:id', :controller => 'blog'
|
# map.connect ':controller/:action/:id', :controller => 'blog'
|
||||||
# end
|
# 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.
|
# This means visiting '/' would invoke the blog controller.
|
||||||
#
|
#
|
||||||
# More formally, you can define defaults in a route with the +:defaults+ key.
|
# 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
|
# == Named routes
|
||||||
#
|
#
|
||||||
|
@ -140,7 +143,7 @@ module ActionController
|
||||||
#
|
#
|
||||||
# redirect_to show_item_path(:id => 25)
|
# 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
|
# # In routes.rb
|
||||||
# map.root :controller => 'blogs'
|
# map.root :controller => 'blogs'
|
||||||
|
@ -173,40 +176,60 @@ module ActionController
|
||||||
# :controller => 'articles',
|
# :controller => 'articles',
|
||||||
# :action => 'find_by_date',
|
# :action => 'find_by_date',
|
||||||
# :year => /\d{4}/,
|
# :year => /\d{4}/,
|
||||||
# :month => /\d{1,2}/,
|
# :month => /\d{1,2}/,
|
||||||
# :day => /\d{1,2}/
|
# :day => /\d{1,2}/
|
||||||
#
|
#
|
||||||
# # Using the route above, the url below maps to:
|
# # Using the route above, the url below maps to:
|
||||||
# # params = {:year => '2005', :month => '11', :day => '06'}
|
# # params = {:year => '2005', :month => '11', :day => '06'}
|
||||||
# # http://localhost:3000/articles/2005/11/06
|
# # http://localhost:3000/articles/2005/11/06
|
||||||
#
|
#
|
||||||
# == Regular Expressions and parameters
|
# == 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',
|
# map.geocode 'geocode/:postalcode', :controller => 'geocode',
|
||||||
# :action => 'show', :postalcode => /\d{5}(-\d{4})?/
|
# :action => 'show', :postalcode => /\d{5}(-\d{4})?/
|
||||||
#
|
#
|
||||||
# or more formally:
|
# or, more formally:
|
||||||
#
|
#
|
||||||
# map.geocode 'geocode/:postalcode', :controller => 'geocode',
|
# map.geocode 'geocode/:postalcode', :controller => 'geocode',
|
||||||
# :action => 'show',
|
# :action => 'show', :requirements => { :postalcode => /\d{5}(-\d{4})?/ }
|
||||||
# :requirements { :postalcode => /\d{5}(-\d{4})?/ }
|
|
||||||
#
|
#
|
||||||
# == Route globbing
|
# == 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?'
|
# 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
|
# == Reloading routes
|
||||||
#
|
#
|
||||||
# You can reload routes if you feel you must:
|
# 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
|
# == Testing Routes
|
||||||
#
|
#
|
||||||
|
@ -229,7 +252,7 @@ module ActionController
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# Note the subtle difference between the two: +assert_routing+ tests that
|
# 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.
|
# breaks into parameters properly.
|
||||||
#
|
#
|
||||||
# In tests you can simply pass the URL or named route to +get+ or +post+.
|
# In tests you can simply pass the URL or named route to +get+ or +post+.
|
||||||
|
@ -245,13 +268,26 @@ module ActionController
|
||||||
# #...
|
# #...
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
|
# == View a list of all your routes
|
||||||
|
#
|
||||||
|
# Run <tt>rake routes</tt>.
|
||||||
|
#
|
||||||
module Routing
|
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
|
# The root paths which may contain controller files
|
||||||
mattr_accessor :controller_paths
|
mattr_accessor :controller_paths
|
||||||
self.controller_paths = []
|
self.controller_paths = []
|
||||||
|
|
||||||
|
# A helper module to hold URL related helpers.
|
||||||
|
module Helpers
|
||||||
|
include PolymorphicRoutes
|
||||||
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def with_controllers(names)
|
def with_controllers(names)
|
||||||
prior_controllers = @possible_controllers
|
prior_controllers = @possible_controllers
|
||||||
|
@ -317,12 +353,25 @@ module ActionController
|
||||||
end
|
end
|
||||||
|
|
||||||
class Route #:nodoc:
|
class Route #:nodoc:
|
||||||
attr_accessor :segments, :requirements, :conditions
|
attr_accessor :segments, :requirements, :conditions, :optimise
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@segments = []
|
@segments = []
|
||||||
@requirements = {}
|
@requirements = {}
|
||||||
@conditions = {}
|
@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
|
end
|
||||||
|
|
||||||
# Write and compile a +generate+ method for this Route.
|
# Write and compile a +generate+ method for this Route.
|
||||||
|
@ -373,6 +422,7 @@ module ActionController
|
||||||
end
|
end
|
||||||
requirement_conditions * ' && ' unless requirement_conditions.empty?
|
requirement_conditions * ' && ' unless requirement_conditions.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
def generation_structure
|
def generation_structure
|
||||||
segments.last.string_structure segments[0..-2]
|
segments.last.string_structure segments[0..-2]
|
||||||
end
|
end
|
||||||
|
@ -411,7 +461,7 @@ module ActionController
|
||||||
def recognition_extraction
|
def recognition_extraction
|
||||||
next_capture = 1
|
next_capture = 1
|
||||||
extraction = segments.collect do |segment|
|
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
|
next_capture += Regexp.new(segment.regexp_chunk).number_of_captures
|
||||||
x
|
x
|
||||||
end
|
end
|
||||||
|
@ -459,6 +509,7 @@ module ActionController
|
||||||
elements << value.to_query(key)
|
elements << value.to_query(key)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
elements.empty? ? '' : "?#{elements.sort * '&'}"
|
elements.empty? ? '' : "?#{elements.sort * '&'}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -510,7 +561,7 @@ module ActionController
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches_controller_and_action?(controller, action)
|
def matches_controller_and_action?(controller, action)
|
||||||
unless @matching_prepared
|
unless defined? @matching_prepared
|
||||||
@controller_requirement = requirement_for(:controller)
|
@controller_requirement = requirement_for(:controller)
|
||||||
@action_requirement = requirement_for(:action)
|
@action_requirement = requirement_for(:action)
|
||||||
@matching_prepared = true
|
@matching_prepared = true
|
||||||
|
@ -539,6 +590,9 @@ module ActionController
|
||||||
end
|
end
|
||||||
|
|
||||||
class Segment #:nodoc:
|
class Segment #:nodoc:
|
||||||
|
# RESERVED_PCHAR = ':@&=+$,;'
|
||||||
|
# UNSAFE_PCHAR = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}]", false, 'N').freeze
|
||||||
|
|
||||||
attr_accessor :is_optional
|
attr_accessor :is_optional
|
||||||
alias_method :optional?, :is_optional
|
alias_method :optional?, :is_optional
|
||||||
|
|
||||||
|
@ -560,6 +614,10 @@ module ActionController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def interpolation_chunk
|
||||||
|
CGI.escape(value)
|
||||||
|
end
|
||||||
|
|
||||||
# Return a string interpolation statement for this segment and those before it.
|
# Return a string interpolation statement for this segment and those before it.
|
||||||
def interpolation_statement(prior_segments)
|
def interpolation_statement(prior_segments)
|
||||||
chunks = prior_segments.collect { |s| s.interpolation_chunk }
|
chunks = prior_segments.collect { |s| s.interpolation_chunk }
|
||||||
|
@ -603,11 +661,11 @@ module ActionController
|
||||||
end
|
end
|
||||||
|
|
||||||
def interpolation_chunk
|
def interpolation_chunk
|
||||||
raw? ? value : CGI.escape(value)
|
raw? ? value : super
|
||||||
end
|
end
|
||||||
|
|
||||||
def regexp_chunk
|
def regexp_chunk
|
||||||
chunk = Regexp.escape value
|
chunk = Regexp.escape(value)
|
||||||
optional? ? Regexp.optionalize(chunk) : chunk
|
optional? ? Regexp.optionalize(chunk) : chunk
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -683,8 +741,8 @@ module ActionController
|
||||||
s << "\n#{expiry_statement}"
|
s << "\n#{expiry_statement}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def interpolation_chunk
|
def interpolation_chunk(value_code = "#{local_name}")
|
||||||
"\#{CGI.escape(#{local_name}.to_s)}"
|
"\#{CGI.escape(#{value_code}.to_s)}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def string_structure(prior_segments)
|
def string_structure(prior_segments)
|
||||||
|
@ -715,10 +773,17 @@ module ActionController
|
||||||
optional? ? Regexp.optionalize(pattern) : pattern
|
optional? ? Regexp.optionalize(pattern) : pattern
|
||||||
end
|
end
|
||||||
def match_extraction(next_capture)
|
def match_extraction(next_capture)
|
||||||
hangon = (default ? "|| #{default.inspect}" : "if match[#{next_capture}]")
|
# All non code-related keys (such as :id, :slug) are URI-unescaped as
|
||||||
|
# path parameters.
|
||||||
# All non code-related keys (such as :id, :slug) have to be unescaped as other CGI params
|
default_value = default ? default.inspect : nil
|
||||||
"params[:#{key}] = match[#{next_capture}] #{hangon}"
|
%[
|
||||||
|
value = if (m = match[#{next_capture}])
|
||||||
|
CGI.unescape(m)
|
||||||
|
else
|
||||||
|
#{default_value}
|
||||||
|
end
|
||||||
|
params[:#{key}] = value if value
|
||||||
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
def optionality_implied?
|
def optionality_implied?
|
||||||
|
@ -733,10 +798,9 @@ module ActionController
|
||||||
"(?i-:(#{(regexp || Regexp.union(*possible_names)).source}))"
|
"(?i-:(#{(regexp || Regexp.union(*possible_names)).source}))"
|
||||||
end
|
end
|
||||||
|
|
||||||
# Don't CGI.escape the controller name, since it may have slashes in it,
|
# Don't CGI.escape the controller name since it may contain slashes.
|
||||||
# like admin/foo.
|
def interpolation_chunk(value_code = "#{local_name}")
|
||||||
def interpolation_chunk
|
"\#{#{value_code}.to_s}"
|
||||||
"\#{#{local_name}.to_s}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Make sure controller names like Admin/Content are correctly normalized to
|
# Make sure controller names like Admin/Content are correctly normalized to
|
||||||
|
@ -755,9 +819,12 @@ module ActionController
|
||||||
end
|
end
|
||||||
|
|
||||||
class PathSegment < DynamicSegment #:nodoc:
|
class PathSegment < DynamicSegment #:nodoc:
|
||||||
EscapedSlash = CGI.escape("/")
|
# RESERVED_PCHAR = "#{Segment::RESERVED_PCHAR}/"
|
||||||
def interpolation_chunk
|
# UNSAFE_PCHAR = Regexp.new("[^#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}]", false, 'N').freeze
|
||||||
"\#{CGI.escape(#{local_name}.to_s).gsub(#{EscapedSlash.inspect}, '/')}"
|
|
||||||
|
def interpolation_chunk(value_code = "#{local_name}")
|
||||||
|
# "\#{URI.escape(#{value_code}.to_s, ActionController::Routing::PathSegment::UNSAFE_PCHAR)}"
|
||||||
|
"\#{CGI.escape(#{value_code})}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def default
|
def default
|
||||||
|
@ -776,6 +843,10 @@ module ActionController
|
||||||
regexp || "(.*)"
|
regexp || "(.*)"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def optionality_implied?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
class Result < ::Array #:nodoc:
|
class Result < ::Array #:nodoc:
|
||||||
def to_s() join '/' end
|
def to_s() join '/' end
|
||||||
def self.new_escaped(strings)
|
def self.new_escaped(strings)
|
||||||
|
@ -845,6 +916,14 @@ module ActionController
|
||||||
# and requirements.
|
# and requirements.
|
||||||
def divide_route_options(segments, options)
|
def divide_route_options(segments, options)
|
||||||
options = options.dup
|
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
|
requirements = (options.delete(:requirements) || {}).dup
|
||||||
defaults = (options.delete(:defaults) || {}).dup
|
defaults = (options.delete(:defaults) || {}).dup
|
||||||
conditions = (options.delete(:conditions) || {}).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."
|
warn "Route segment \"#{segment.to_s}\" cannot be optional because it precedes a required segment. This segment will be required."
|
||||||
end
|
end
|
||||||
segment.is_optional = false
|
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
|
# if a segment has a default, then it is optional
|
||||||
segment.is_optional = true
|
segment.is_optional = true
|
||||||
end
|
end
|
||||||
|
@ -938,11 +1017,14 @@ module ActionController
|
||||||
path = "/#{path}" unless path[0] == ?/
|
path = "/#{path}" unless path[0] == ?/
|
||||||
path = "#{path}/" unless path[-1] == ?/
|
path = "#{path}/" unless path[-1] == ?/
|
||||||
|
|
||||||
|
path = "/#{options[:path_prefix].to_s.gsub(/^\//,'')}#{path}" if options[:path_prefix]
|
||||||
|
|
||||||
segments = segments_for_route_path(path)
|
segments = segments_for_route_path(path)
|
||||||
defaults, requirements, conditions = divide_route_options(segments, options)
|
defaults, requirements, conditions = divide_route_options(segments, options)
|
||||||
requirements = assign_route_options(segments, defaults, requirements)
|
requirements = assign_route_options(segments, defaults, requirements)
|
||||||
|
|
||||||
route = Route.new
|
route = Route.new
|
||||||
|
|
||||||
route.segments = segments
|
route.segments = segments
|
||||||
route.requirements = requirements
|
route.requirements = requirements
|
||||||
route.conditions = conditions
|
route.conditions = conditions
|
||||||
|
@ -952,6 +1034,13 @@ module ActionController
|
||||||
route.significant_keys << :action
|
route.significant_keys << :action
|
||||||
end
|
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)
|
if !route.significant_keys.include?(:controller)
|
||||||
raise ArgumentError, "Illegal route: the :controller must be specified!"
|
raise ArgumentError, "Illegal route: the :controller must be specified!"
|
||||||
end
|
end
|
||||||
|
@ -966,34 +1055,46 @@ module ActionController
|
||||||
#
|
#
|
||||||
# Mapper instances have relatively few instance methods, in order to avoid
|
# Mapper instances have relatively few instance methods, in order to avoid
|
||||||
# clashes with named routes.
|
# clashes with named routes.
|
||||||
class Mapper #:nodoc:
|
class Mapper #:doc:
|
||||||
def initialize(set)
|
def initialize(set) #:nodoc:
|
||||||
@set = set
|
@set = set
|
||||||
end
|
end
|
||||||
|
|
||||||
# Create an unnamed route with the provided +path+ and +options+. See
|
# 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 = {})
|
def connect(path, options = {})
|
||||||
@set.add_route(path, options)
|
@set.add_route(path, options)
|
||||||
end
|
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)
|
@set.add_named_route(name, path, options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def deprecated_named_route(name, deprecated_name, options = {})
|
# Enables the use of resources in a module by setting the name_prefix, path_prefix, and namespace for the model.
|
||||||
@set.add_deprecated_named_route(name, deprecated_name)
|
# 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
|
end
|
||||||
|
|
||||||
# Added deprecation notice for anyone who already added a named route called "root".
|
def method_missing(route_name, *args, &proc) #:nodoc:
|
||||||
# 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)
|
|
||||||
super unless args.length >= 1 && proc.nil?
|
super unless args.length >= 1 && proc.nil?
|
||||||
@set.add_named_route(route_name, *args)
|
@set.add_named_route(route_name, *args)
|
||||||
end
|
end
|
||||||
|
@ -1004,7 +1105,7 @@ module ActionController
|
||||||
# named routes.
|
# named routes.
|
||||||
class NamedRouteCollection #:nodoc:
|
class NamedRouteCollection #:nodoc:
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
include ActionController::Routing::Optimisation
|
||||||
attr_reader :routes, :helpers
|
attr_reader :routes, :helpers
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
|
@ -1017,7 +1118,7 @@ module ActionController
|
||||||
|
|
||||||
@module ||= Module.new
|
@module ||= Module.new
|
||||||
@module.instance_methods.each do |selector|
|
@module.instance_methods.each do |selector|
|
||||||
@module.send :remove_method, selector
|
@module.class_eval { remove_method selector }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1047,40 +1148,19 @@ module ActionController
|
||||||
routes.length
|
routes.length
|
||||||
end
|
end
|
||||||
|
|
||||||
def install(destinations = [ActionController::Base, ActionView::Base])
|
def reset!
|
||||||
Array(destinations).each { |dest| dest.send :include, @module }
|
old_routes = routes.dup
|
||||||
|
clear!
|
||||||
|
old_routes.each do |name, route|
|
||||||
|
add(name, route)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def define_deprecated_named_route_methods(name, deprecated_name)
|
def install(destinations = [ActionController::Base, ActionView::Base], regenerate = false)
|
||||||
|
reset! if regenerate
|
||||||
[:url, :path].each do |kind|
|
Array(destinations).each do |dest|
|
||||||
@module.send :module_eval, <<-end_eval # We use module_eval to avoid leaks
|
dest.send! :include, @module
|
||||||
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -1102,50 +1182,55 @@ module ActionController
|
||||||
|
|
||||||
def define_hash_access(route, name, kind, options)
|
def define_hash_access(route, name, kind, options)
|
||||||
selector = hash_access_name(name, kind)
|
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)
|
def #{selector}(options = nil)
|
||||||
options ? #{options.inspect}.merge(options) : #{options.inspect}
|
options ? #{options.inspect}.merge(options) : #{options.inspect}
|
||||||
end
|
end
|
||||||
|
protected :#{selector}
|
||||||
end_eval
|
end_eval
|
||||||
@module.send(:protected, selector)
|
|
||||||
helpers << selector
|
helpers << selector
|
||||||
end
|
end
|
||||||
|
|
||||||
def define_url_helper(route, name, kind, options)
|
def define_url_helper(route, name, kind, options)
|
||||||
selector = url_helper_name(name, kind)
|
selector = url_helper_name(name, kind)
|
||||||
|
|
||||||
# The segment keys used for positional paramters
|
# 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)
|
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)
|
def #{selector}(*args)
|
||||||
|
#{generate_optimisation_block(route, kind)}
|
||||||
|
|
||||||
opts = if args.empty? || Hash === args.first
|
opts = if args.empty? || Hash === args.first
|
||||||
args.first || {}
|
args.first || {}
|
||||||
else
|
else
|
||||||
# allow ordered parameters to be associated with corresponding
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
||||||
# dynamic segments, so you can do
|
args = args.zip(#{route.segment_keys.inspect}).inject({}) do |h, (v, k)|
|
||||||
#
|
|
||||||
# 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)|
|
|
||||||
h[k] = v
|
h[k] = v
|
||||||
h
|
h
|
||||||
end
|
end
|
||||||
|
options.merge(args)
|
||||||
end
|
end
|
||||||
|
|
||||||
url_for(#{hash_access_method}(opts))
|
url_for(#{hash_access_method}(opts))
|
||||||
end
|
end
|
||||||
|
protected :#{selector}
|
||||||
end_eval
|
end_eval
|
||||||
@module.send(:protected, selector)
|
|
||||||
helpers << selector
|
helpers << selector
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :routes, :named_routes
|
attr_accessor :routes, :named_routes
|
||||||
|
@ -1164,7 +1249,7 @@ module ActionController
|
||||||
def draw
|
def draw
|
||||||
clear!
|
clear!
|
||||||
yield Mapper.new(self)
|
yield Mapper.new(self)
|
||||||
named_routes.install
|
install_helpers
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear!
|
def clear!
|
||||||
|
@ -1174,6 +1259,11 @@ module ActionController
|
||||||
@routes_by_controller = nil
|
@routes_by_controller = nil
|
||||||
end
|
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?
|
def empty?
|
||||||
routes.empty?
|
routes.empty?
|
||||||
end
|
end
|
||||||
|
@ -1182,14 +1272,27 @@ module ActionController
|
||||||
Routing.use_controllers! nil # Clear the controller cache so we may discover new ones
|
Routing.use_controllers! nil # Clear the controller cache so we may discover new ones
|
||||||
clear!
|
clear!
|
||||||
load_routes!
|
load_routes!
|
||||||
named_routes.install
|
install_helpers
|
||||||
end
|
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!
|
def load_routes!
|
||||||
if defined?(RAILS_ROOT) && defined?(::ActionController::Routing::Routes) && self == ::ActionController::Routing::Routes
|
if defined?(RAILS_ROOT) && defined?(::ActionController::Routing::Routes) && self == ::ActionController::Routing::Routes
|
||||||
load File.join("#{RAILS_ROOT}/config/routes.rb")
|
load File.join("#{RAILS_ROOT}/config/routes.rb")
|
||||||
|
@routes_last_modified = File.stat("#{RAILS_ROOT}/config/routes.rb").mtime
|
||||||
else
|
else
|
||||||
add_route ":controller/:action/:id"
|
add_route ":controller/:action/:id"
|
||||||
end
|
end
|
||||||
|
@ -1202,11 +1305,9 @@ module ActionController
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_named_route(name, path, options = {})
|
def add_named_route(name, path, options = {})
|
||||||
named_routes[name] = add_route(path, options)
|
# TODO - is options EVER used?
|
||||||
end
|
name = options[:name_prefix] + name.to_s if options[:name_prefix]
|
||||||
|
named_routes[name.to_sym] = add_route(path, options)
|
||||||
def add_deprecated_named_route(name, deprecated_name)
|
|
||||||
named_routes.define_deprecated_named_route_methods(name, deprecated_name)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def options_as_params(options)
|
def options_as_params(options)
|
||||||
|
@ -1229,7 +1330,7 @@ module ActionController
|
||||||
|
|
||||||
def build_expiry(options, recall)
|
def build_expiry(options, recall)
|
||||||
recall.inject({}) do |expiry, (key, recalled_value)|
|
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
|
expiry
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1246,6 +1347,7 @@ module ActionController
|
||||||
|
|
||||||
def generate(options, recall = {}, method=:generate)
|
def generate(options, recall = {}, method=:generate)
|
||||||
named_route_name = options.delete(:use_route)
|
named_route_name = options.delete(:use_route)
|
||||||
|
generate_all = options.delete(:generate_all)
|
||||||
if named_route_name
|
if named_route_name
|
||||||
named_route = named_routes[named_route_name]
|
named_route = named_routes[named_route_name]
|
||||||
options = named_route.parameter_shell.merge(options)
|
options = named_route.parameter_shell.merge(options)
|
||||||
|
@ -1287,11 +1389,19 @@ module ActionController
|
||||||
action = merged[:action]
|
action = merged[:action]
|
||||||
|
|
||||||
raise RoutingError, "Need controller and action!" unless controller && 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
|
# 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 = routes_by_controller[controller][action][options.keys.sort_by { |x| x.object_id }]
|
||||||
|
|
||||||
routes.each do |route|
|
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)
|
return results if results && (!results.is_a?(Array) || results.first)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1307,7 +1417,7 @@ module ActionController
|
||||||
else
|
else
|
||||||
required_segments = named_route.segments.select {|seg| (!seg.optional?) && (!seg.is_a?(DividerSegment)) }
|
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
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1318,11 +1428,19 @@ module ActionController
|
||||||
end
|
end
|
||||||
|
|
||||||
def recognize_path(path, environment={})
|
def recognize_path(path, environment={})
|
||||||
path = CGI.unescape(path)
|
|
||||||
routes.each do |route|
|
routes.each do |route|
|
||||||
result = route.recognize(path, environment) and return result
|
result = route.recognize(path, environment) and return result
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def routes_by_controller
|
def routes_by_controller
|
||||||
|
@ -1368,6 +1486,15 @@ module ActionController
|
||||||
end
|
end
|
||||||
|
|
||||||
Routes = RouteSet.new
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue