RoutingData

Simple solution that provides information about routes in your rails app.

Installation

Add this line to your application's Gemfile:

gem 'routing_data'

And then execute:

$ bundle

Or install it yourself as:

$ gem install routing_data

Usage

With RoutingData you have access to url helpers in your models:

RoutingData.url_helpers.root_path
# => "/" 
RoutingData.url_helpers.post_path(1)
# => "/posts/1" 
RoutingData.url_helpers.new_user_path
# => "/users/new"

You can access to lists of controllers and actions in your application (this is helpful for authorization purposes):

RoutingData.controllers_actions_map
# => {:home=>[:index], :blogs=>[:index, :create, :new, :edit, :show, :update, :destroy], :categories=>[:index, :create, :new, :edit, :show, :update, :destroy], :posts=>[:index, :create, :new, :edit, :show, :update, :destroy], :users=>[:index, :create, :new, :edit, :show, :update, :destroy, :show], :"rails/info"=>[:properties]}

RoutingData.controllers
# => [:home, :blogs, :categories, :posts, :users]

RoutingData.actions_for(:users)
# => [:index, :create, :new, :edit, :show, :update, :destroy, :show]

You can also generate json-data about routes and use it in your Javascript:

RoutingData.url_helpers_data
# => {"root"=>{:name=>"root", :path=>"/", :controller=>"home", :action=>"index", :parts=>[]}, "blogs"=>{:name=>"blogs", :path=>"/blogs(.:format)", :controller=>"blogs", :action=>"index", :parts=>[:format]}, "new_blog"=>{:name=>"new_blog", :path=>"/blogs/new(.:format)", :controller=>"blogs", :action=>"new", :parts=>[:format]}, "edit_blog"=>{:name=>"edit_blog", :path=>"/blogs/:id/edit(.:format)", :controller=>"blogs", :action=>"edit", :parts=>[:id, :format]}, "blog"=>{:name=>"blog", :path=>"/blogs/:id(.:format)", :controller=>"blogs", :action=>"show", :parts=>[:id, :format]}, "categories"=>{:name=>"categories", :path=>"/categories(.:format)", :controller=>"categories", :action=>"index", :parts=>[:format]}, "new_category"=>{:name=>"new_category", :path=>"/categories/new(.:format)", :controller=>"categories", :action=>"new", :parts=>[:format]}, "edit_category"=>{:name=>"edit_category", :path=>"/categories/:id/edit(.:format)", :controller=>"categories", :action=>"edit", :parts=>[:id, :format]}, "category"=>{:name=>"category", :path=>"/categories/:id(.:format)", :controller=>"categories", :action=>"show", :parts=>[:id, :format]}, "posts"=>{:name=>"posts", :path=>"/posts(.:format)", :controller=>"posts", :action=>"index", :parts=>[:format]}, "new_post"=>{:name=>"new_post", :path=>"/posts/new(.:format)", :controller=>"posts", :action=>"new", :parts=>[:format]}, "edit_post"=>{:name=>"edit_post", :path=>"/posts/:id/edit(.:format)", :controller=>"posts", :action=>"edit", :parts=>[:id, :format]}, "post"=>{:name=>"post", :path=>"/posts/:id(.:format)", :controller=>"posts", :action=>"show", :parts=>[:id, :format]}, "users"=>{:name=>"users", :path=>"/users(.:format)", :controller=>"users", :action=>"index", :parts=>[:format]}, "new_user"=>{:name=>"new_user", :path=>"/users/new(.:format)", :controller=>"users", :action=>"new", :parts=>[:format]}, "edit_user"=>{:name=>"edit_user", :path=>"/users/:id/edit(.:format)", :controller=>"users", :action=>"edit", :parts=>[:id, :format]}, "user"=>{:name=>"user", :path=>"/users/:id(.:format)", :controller=>"users", :action=>"show", :parts=>[:id, :format]}, "show_user"=>{:name=>"show_user", :path=>"/user/ololo/:user_id(.:format)", :controller=>"users", :action=>"show", :parts=>[:user_id, :format]}, "rails_info_properties"=>{:name=>"rails_info_properties", :path=>"/rails/info/properties(.:format)", :controller=>"rails/info", :action=>"properties", :parts=>[:format]}} 


RoutingData.url_helpers_data(as_json: true)
# => "{\"root\":{\"name\":\"root\",\"path\":\"/\",\"controller\":\"home\",\"action\":\"index\",\"parts\":[]},\"blogs\":{\"name\":\"blogs\",\"path\":\"/blogs(.:format)\",\"controller\":\"blogs\",\"action\":\"index\",\"parts\":[\"format\"]},\"new_blog\":{\"name\":\"new_blog\",\"path\":\"/blogs/new(.:format)\",\"controller\":\"blogs\",\"action\":\"new\",\"parts\":[\"format\"]},\"edit_blog\":{\"name\":\"edit_blog\",\"path\":\"/blogs/:id/edit(.:format)\",\"controller\":\"blogs\",\"action\":\"edit\",\"parts\":[\"id\",\"format\"]},\"blog\":{\"name\":\"blog\",\"path\":\"/blogs/:id(.:format)\",\"controller\":\"blogs\",\"action\":\"show\",\"parts\":[\"id\",\"format\"]},\"categories\":{\"name\":\"categories\",\"path\":\"/categories(.:format)\",\"controller\":\"categories\",\"action\":\"index\",\"parts\":[\"format\"]},\"new_category\":{\"name\":\"new_category\",\"path\":\"/categories/new(.:format)\",\"controller\":\"categories\",\"action\":\"new\",\"parts\":[\"format\"]},\"edit_category\":{\"name\":\"edit_category\",\"path\":\"/categories/:id/edit(.:format)\",\"controller\":\"categories\",\"action\":\"edit\",\"parts\":[\"id\",\"format\"]},\"category\":{\"name\":\"category\",\"path\":\"/categories/:id(.:format)\",\"controller\":\"categories\",\"action\":\"show\",\"parts\":[\"id\",\"format\"]},\"posts\":{\"name\":\"posts\",\"path\":\"/posts(.:format)\",\"controller\":\"posts\",\"action\":\"index\",\"parts\":[\"format\"]},\"new_post\":{\"name\":\"new_post\",\"path\":\"/posts/new(.:format)\",\"controller\":\"posts\",\"action\":\"new\",\"parts\":[\"format\"]},\"edit_post\":{\"name\":\"edit_post\",\"path\":\"/posts/:id/edit(.:format)\",\"controller\":\"posts\",\"action\":\"edit\",\"parts\":[\"id\",\"format\"]},\"post\":{\"name\":\"post\",\"path\":\"/posts/:id(.:format)\",\"controller\":\"posts\",\"action\":\"show\",\"parts\":[\"id\",\"format\"]},\"users\":{\"name\":\"users\",\"path\":\"/users(.:format)\",\"controller\":\"users\",\"action\":\"index\",\"parts\":[\"format\"]},\"new_user\":{\"name\":\"new_user\",\"path\":\"/users/new(.:format)\",\"controller\":\"users\",\"action\":\"new\",\"parts\":[\"format\"]},\"edit_user\":{\"name\":\"edit_user\",\"path\":\"/users/:id/edit(.:format)\",\"controller\":\"users\",\"action\":\"edit\",\"parts\":[\"id\",\"format\"]},\"user\":{\"name\":\"user\",\"path\":\"/users/:id(.:format)\",\"controller\":\"users\",\"action\":\"show\",\"parts\":[\"id\",\"format\"]},\"show_user\":{\"name\":\"show_user\",\"path\":\"/user/ololo/:user_id(.:format)\",\"controller\":\"users\",\"action\":\"show\",\"parts\":[\"user_id\",\"format\"]},\"rails_info_properties\":{\"name\":\"rails_info_properties\",\"path\":\"/rails/info/properties(.:format)\",\"controller\":\"rails/info\",\"action\":\"properties\",\"parts\":[\"format\"]}}"

RoutingData.url_helpers_data(for_controllers: ['home', 'posts'])
# => {"root"=>{:name=>"root", :path=>"/", :controller=>"home", :action=>"index", :parts=>[]}, "posts"=>{:name=>"posts", :path=>"/posts(.:format)", :controller=>"posts", :action=>"index", :parts=>[:format]}, "new_post"=>{:name=>"new_post", :path=>"/posts/new(.:format)", :controller=>"posts", :action=>"new", :parts=>[:format]}, "edit_post"=>{:name=>"edit_post", :path=>"/posts/:id/edit(.:format)", :controller=>"posts", :action=>"edit", :parts=>[:id, :format]}, "post"=>{:name=>"post", :path=>"/posts/:id(.:format)", :controller=>"posts", :action=>"show", :parts=>[:id, :format]}}

RoutingData.url_helpers_data(with_name_in: ['root', 'post', 'user'], as_json: true)
# => "{\"root\":{\"name\":\"root\",\"path\":\"/\",\"controller\":\"home\",\"action\":\"index\",\"parts\":[]},\"post\":{\"name\":\"post\",\"path\":\"/posts/:id(.:format)\",\"controller\":\"posts\",\"action\":\"show\",\"parts\":[\"id\",\"format\"]},\"user\":{\"name\":\"user\",\"path\":\"/users/:id(.:format)\",\"controller\":\"users\",\"action\":\"show\",\"parts\":[\"id\",\"format\"]}}"

In your Javascript you can use RoutingData object and url helpers through it. Just add

//= require routing_data

to app/assets/javascripts/application.js and

  <%= include_url_helpers_data %>

in your views before requiring javascripts.

After this you can work with RoutingData object:

RoutingData // { helpersData: {...}, helpers: {...}, globalizeHelpers() }

RoutingData.helpersData is made from data from RoutingData.url_helpers_data method in Ruby.

RoutingData.helpers contains url helpers with names like in you Ruby on Rails app. For example:

RoutingData.helpers.new_post_path(); // "/posts/new"

RoutingData.helpers.post_path({id: 1}); // "/posts/1"

RoutingData.helpers.post_path({id: 1, format: 'xml'}); // "/posts/1.xml"

You can use RoutingData.globalizeHelpers() to make your url helper methods also available as global functions. For example:

RoutingData.helpers.new_blog_path(); // "/blogs/new"
RoutingData.globalizeHelpers();
new_blog_path(); //"/blogs/new"

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request