Last Updated: February 25, 2016
·
1.739K
· vrinek

`rake routes` for Backbone

In case you are developing a Backbone on Rails type of application, one of the things that you will have duplicates of are your routes. Rails provides you with a rake routes command that will display all known routes.

The equivalent of rake routes for Backbone can be achieved with:

_.map(Backbone.history.handlers, function(i){return i.route})

With this, you have a list of regular expressions to try and match to your rails routes.

Example script to make the comparison (needs to be adapted to your needs):

backbone_routes = [
  /^projects\/([^\/]+)$/,
  /^projects\/([^\/]+)\/details$/,
  /^admin$/
]

rails_routes = Rails.application.routes.routes.to_a.map do |route|
  route.path.spec.to_s.sub(/\([^\)]+\)/, '')
end

backbone_routes.each do |regexp|
  matching_route = rails_routes.find { |route| route =~ regexp }
  puts matching_route || "NO MATCH FOUND - #{regexp.inspect}"
end

One thing to watch for when copying the Backbone routes (from the browser's JavaScript console) is that [^/] has to be substituted with [^\/] in Ruby to work.