forked from ayamomiji/routes-query
/
routes-query.rake
76 lines (58 loc) · 2.28 KB
/
routes-query.rake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
namespace :routes do
desc 'Default routes task with more query options. Q=any C=controller A=action N=name P=path'
task :query => :environment do
Rails.application.reload_routes!
all_routes = Rails.application.routes.routes
path_to_name = {}
all_routes.each do |route|
path_to_name[route.path] = route.name.to_s if route.name.to_s.present?
end
if ENV['C']
all_routes = all_routes.select{ |route| route.defaults[:controller] =~ Regexp.new(ENV['C'], 'i') }
end
if ENV['A']
all_routes = all_routes.select{ |route| route.defaults[:action] =~ Regexp.new(ENV['A'], 'i') }
end
if ENV['N']
all_routes = all_routes.select{ |route| route.name =~ Regexp.new(ENV['N'], 'i') }
end
if ENV['P']
all_routes = all_routes.select{ |route| route.path =~ Regexp.new(ENV['P'], 'i') }
end
if ENV['Q']
ENV['Q'].split.each do |query|
regexp = Regexp.new(query, 'i')
all_routes = all_routes.select do |route|
route.defaults[:controller] =~ regexp or
route.defaults[:action] =~ regexp or
route.name =~ regexp or
route.path =~ regexp
end
end
end
routes = all_routes.collect do |route|
reqs = route.requirements.dup
reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
if reqs[:controller]
reqs[:C] = reqs[:controller]
reqs.delete :controller
end
if reqs[:action]
reqs[:A] = reqs[:action]
reqs.delete :action
end
reqs = reqs.empty? ? "" : reqs.map { |k, v| "#{k}: #{v}" }.join(', ')
route_name = route.name.to_s
route_name = path_to_name[route.path] || '(none)' if route_name.blank?
{:name => route_name, :verb => route.verb.to_s, :path => route.path, :reqs => reqs}
end
routes.reject! { |r| r[:path] =~ %r{/rails/info/properties} } # Skip the route if it's internal info route
name_width = routes.map{ |r| r[:name].length }.max
verb_width = routes.map{ |r| r[:verb].length }.max
path_width = routes.map{ |r| r[:path].length }.max
routes.each do |r|
puts "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
end
end
end
task :rq => 'routes:query'