forked from mtrpcic/js-routes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js.rake
47 lines (41 loc) · 1.52 KB
/
js.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
desc "Generate a JavaScript file that contains your Rails routes"
namespace :js do
task :routes, :filename, :needs => :environment do |t, args|
filename = args[:filename].blank? ? "rails_routes.js" : args[:filename]
save_path = "#{RAILS_ROOT}/public/javascripts/#{filename}"
if Rails.version >= "3.0.0"
routes = generate_routes_for_rails_3
else
routes = generate_routes_for_rails_2
end
javascript = ""
routes.each do |route|
javascript << generate_method(route[:name], route[:path]) + "\n"
end
File.open(save_path, "w") { |f| f.write(javascript) }
puts "Routes saved to #{save_path}."
end
end
def generate_method(name, path)
compare = /:(.*?)(\/|$)/
path.sub!(compare, "' + params.#{$1} + '#{$2}") while path =~ compare
return "function #{name}_path(params){ return '#{path}'}"
end
def generate_routes_for_rails_2
processed_routes = []
ActionController::Routing::Routes.routes.each do |route|
name = ActionController::Routing::Routes.named_routes.routes.index(route)
segs = route.segments.inject("") {|str, s| str << s.to_s}
segs.chop! if segs.length > 1
processed_routes << {:name => name, :path => segs.split("(")[0]} unless name.nil?
end
return processed_routes
end
def generate_routes_for_rails_3
Rails.application.reload_routes!
processed_routes = []
Rails.application.routes.routes.each do |route|
processed_routes << {:name => route.name + "_path", :path => route.path.split("(")[0]} unless route.name.nil?
end
return processed_routes
end