From a840c8afbf4e30b7bd9979e8cd70192c65be7a43 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sat, 24 Oct 2009 18:08:54 -0500 Subject: Restore `rake routes` [#3402 state:resolved] --- actionpack/lib/action_dispatch/routing.rb | 1 + .../action_dispatch/routing/deprecated_mapper.rb | 3 +- actionpack/lib/action_dispatch/routing/mapper.rb | 8 ++-- actionpack/lib/action_dispatch/routing/route.rb | 44 ++++++++++++++++++++++ .../lib/action_dispatch/routing/route_set.rb | 11 ++---- railties/lib/rails/tasks/routes.rake | 9 ++--- 6 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 actionpack/lib/action_dispatch/routing/route.rb diff --git a/actionpack/lib/action_dispatch/routing.rb b/actionpack/lib/action_dispatch/routing.rb index 3803929847..b9c377db2c 100644 --- a/actionpack/lib/action_dispatch/routing.rb +++ b/actionpack/lib/action_dispatch/routing.rb @@ -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( / . ? ) diff --git a/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb b/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb index f2a1f10fa7..0564ba9797 100644 --- a/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb +++ b/actionpack/lib/action_dispatch/routing/deprecated_mapper.rb @@ -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: diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index ab4193266a..d6d822842b 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -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 diff --git a/actionpack/lib/action_dispatch/routing/route.rb b/actionpack/lib/action_dispatch/routing/route.rb new file mode 100644 index 0000000000..8990e207c2 --- /dev/null +++ b/actionpack/lib/action_dispatch/routing/route.rb @@ -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 diff --git a/actionpack/lib/action_dispatch/routing/route_set.rb b/actionpack/lib/action_dispatch/routing/route_set.rb index 90d7c208a5..93617e826d 100644 --- a/actionpack/lib/action_dispatch/routing/route_set.rb +++ b/actionpack/lib/action_dispatch/routing/route_set.rb @@ -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 diff --git a/railties/lib/rails/tasks/routes.rake b/railties/lib/rails/tasks/routes.rake index abbf3258c1..2395d73b2f 100644 --- a/railties/lib/rails/tasks/routes.rake +++ b/railties/lib/rails/tasks/routes.rake @@ -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 -- cgit v1.2.3