Socket
Book a DemoInstallSign in
Socket

js_rails_routes

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js_rails_routes

1.2.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

rake js:routes

Gem Build Status Code Climate Test Coverage license

Generate a ES6 module that contains Rails routes.

Description

This gem provides "js:routes" rake task. It generates a ES6 requirable module which exports url helper functions defined in your Rails application.

Suppose the app has following routes:

# == Route Map
#
#       Prefix Verb   URI Pattern                  Controller#Action
#     articles GET    /articles(.:format)          articles#index
#              POST   /articles(.:format)          articles#create
#  new_article GET    /articles/new(.:format)      articles#new
# edit_article GET    /articles/:id/edit(.:format) articles#edit
#      article GET    /articles/:id(.:format)      articles#show
#              PATCH  /articles/:id(.:format)      articles#update
#              PUT    /articles/:id(.:format)      articles#update
#              DELETE /articles/:id(.:format)      articles#destroy
Rails.application.routes.draw do
  resources :articles
end

then rake js:routes generates "app/assets/javascripts/rails-routes.js" as:

// Don't edit manually. `rake js:routes` generates this file.
function process(route, params, keys) {
  var query = [];
  for (var param in params) if (Object.prototype.hasOwnProperty.call(params, param)) {
    if (keys.indexOf(param) === -1) {
      query.push(param + "=" + encodeURIComponent(params[param]));
    }
  }
  return query.length ? route + "?" + query.join("&") : route;
}

export function article_path(params) { return process('/articles/' + params.id + '', params, ['id']); }
export function articles_path(params) { return process('/articles', params, []); }
export function edit_article_path(params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
export function new_article_path(params) { return process('/articles/new', params, []); }

VS.

railsware/js-routes spreads url helpers via global variable.

This gem uses ES6 modules.

Requirement

  • Rails >= 3.2

Usage

Generate routes file.

rake js:routes

Configuration

JSRailsRoutes supports several parameters:

NameTypeDescriptionDefault
include_pathsRegexpPaths match to the regexp are included/.*/
exclude_pathsRegexpPaths match to the regexp are excluded/^$/
include_namesRegexpNames match to the regexp are included/.*/
exclude_namesRegexpNames match to the regexp are excluded/^$/
exclude_enginesRegexpRails engines match to the regexp are excluded/^$/
output_dirStringOutput JS file into the specified directoryRails.root.join("app", "assets", "javascripts")
camelizeSymbolOutput JS file with chosen camelcase type it also avaliable for :lower and :uppernil
targetStringTarget type. "js" or "ts""js"
route_filterProcFully customizable filter on JSRails::Route->(route) { true }
route_set_filterProcFully customizable filter on JSRails::RouteSet->(route_set) { true }

You can configure via JSRailsRoutes.configure.

# Rakefile
JSRailsRoutes.configure do |c|
  c.exclude_paths = %r{^/(rails|sidekiq)}
  c.output_dir = Rails.root.join('client/javascripts')
end

Now rake js:routes ignores paths starting with "/rails" or "/sidekiq".

Command line parameters

You can override the coniguration via command line parameters:

rake js:routes exclude_paths='^/rails'

The command still ignores "/rails" but includes "/sidekiq".

Rename route

You can rename route in route_filter:

# Rakefile
JSRailsRoutes.configure do |c|
  c.route_filter = -> (route) do
    # Remove common prefix if route's name starts with it.
    route.name = route.name[4..-1] if route.name.start_with?('foo_')
    true
  end
end

Install

Your Rails Gemfile:

gem 'js_rails_routes', group: :development

License

MIT

FAQs

Package last updated on 19 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.