Restore rake routes [#3402 state:resolved]

This commit is contained in:
Joshua Peek
2009-10-24 18:08:54 -05:00
parent a0049a6b42
commit a840c8afbf
6 changed files with 57 additions and 19 deletions

View File

@@ -262,6 +262,7 @@ module ActionDispatch
module Routing
autoload :DeprecatedMapper, 'action_dispatch/routing/deprecated_mapper'
autoload :Mapper, 'action_dispatch/routing/mapper'
autoload :Route, 'action_dispatch/routing/route'
autoload :RouteSet, 'action_dispatch/routing/route_set'
SEPARATORS = %w( / . ? )

View File

@@ -132,7 +132,6 @@ module ActionDispatch
path = optionalize_trailing_dynamic_segments(path, requirements, defaults)
glob = $1.to_sym if path =~ /\/\*(\w+)$/
path = ::Rack::Mount::Utils.normalize_path(path)
path = ::Rack::Mount::Strexp.compile(path, requirements, %w( / . ? ))
if glob && !defaults[glob].blank?
raise ActionController::RoutingError, "paths cannot have non-empty default values"
@@ -145,7 +144,7 @@ module ActionDispatch
conditions[:request_method] = method if method
conditions[:path_info] = path if path
@set.add_route(app, conditions, defaults, name)
@set.add_route(app, conditions, requirements, defaults, name)
end
def optionalize_trailing_dynamic_segments(path, requirements, defaults) #:nodoc:

View File

@@ -252,9 +252,11 @@ module ActionDispatch
constraints = (@scope[:constraints] || {}).merge(constraints)
options.each { |k, v| constraints[k] = v if v.is_a?(Regexp) }
conditions[:path_info] = Rack::Mount::Strexp.compile(path, constraints, %w( / . ? ))
conditions[:path_info] = path
requirements = constraints.dup
segment_keys = Rack::Mount::RegexpWithNamedGroups.new(conditions[:path_info]).names
path_regexp = Rack::Mount::Strexp.compile(path, constraints, SEPARATORS)
segment_keys = Rack::Mount::RegexpWithNamedGroups.new(path_regexp).names
constraints.reject! { |k, v| segment_keys.include?(k.to_s) }
conditions.merge!(constraints)
@@ -286,7 +288,7 @@ module ActionDispatch
end
app = Constraints.new(app, blocks) if blocks.any?
@set.add_route(app, conditions, defaults, options[:as])
@set.add_route(app, conditions, requirements, defaults, options[:as])
self
end

View File

@@ -0,0 +1,44 @@
module ActionDispatch
module Routing
class Route #:nodoc:
attr_reader :app, :conditions, :defaults, :name
attr_reader :path, :requirements
def initialize(app, conditions = {}, requirements = {}, defaults = {}, name = nil)
@app = app
@defaults = defaults
@name = name
@requirements = requirements.merge(defaults)
@requirements.delete(:controller) if @requirements[:controller].is_a?(Regexp)
@requirements.delete_if { |k, v|
v == Regexp.compile("[^#{SEPARATORS.join}]+")
}
if path = conditions[:path_info]
@path = path
conditions[:path_info] = ::Rack::Mount::Strexp.compile(path, requirements, SEPARATORS)
end
@conditions = conditions.inject({}) { |h, (k, v)|
h[k] = Rack::Mount::RegexpWithNamedGroups.new(v)
h
}
end
def verb
if verb = conditions[:verb]
verb.to_s.upcase
end
end
def segment_keys
@segment_keys ||= conditions[:path_info].names.compact.map { |key| key.to_sym }
end
def to_ary
[@app, @conditions, @defaults, @name]
end
end
end
end

View File

@@ -47,11 +47,6 @@ module ActionDispatch
end
end
module RouteExtensions
def segment_keys
conditions[:path_info].names.compact.map { |key| key.to_sym }
end
end
# A NamedRouteCollection instance is a collection of named routes, and also
# maintains an anonymous module that can be used to install helpers for the
@@ -290,9 +285,9 @@ module ActionDispatch
routes_changed_at
end
def add_route(app, conditions = {}, defaults = {}, name = nil)
route = @set.add_route(app, conditions, defaults, name)
route.extend(RouteExtensions)
def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil)
route = Route.new(app, conditions, requirements, defaults, name)
@set.add_route(*route)
named_routes[name] = route if name
routes << route
route

View File

@@ -3,16 +3,13 @@ task :routes => :environment do
all_routes = ENV['CONTROLLER'] ? ActionController::Routing::Routes.routes.select { |route| route.defaults[:controller] == ENV['CONTROLLER'] } : ActionController::Routing::Routes.routes
routes = all_routes.collect do |route|
name = ActionController::Routing::Routes.named_routes.routes.index(route).to_s
verb = route.conditions[:method].to_s.upcase
segs = route.segments.inject("") { |str,s| str << s.to_s }
segs.chop! if segs.length > 1
reqs = route.requirements.empty? ? "" : route.requirements.inspect
{:name => name, :verb => verb, :segs => segs, :reqs => reqs}
{:name => name, :verb => route.verb.to_s, :path => route.path, :reqs => reqs}
end
name_width = routes.collect {|r| r[:name]}.collect {|n| n.length}.max
verb_width = routes.collect {|r| r[:verb]}.collect {|v| v.length}.max
segs_width = routes.collect {|r| r[:segs]}.collect {|s| s.length}.max
path_width = routes.collect {|r| r[:path]}.collect {|s| s.length}.max
routes.each do |r|
puts "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:segs].ljust(segs_width)} #{r[:reqs]}"
puts "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
end
end