GithubHelp home page GithubHelp logo

railsware / js-routes Goto Github PK

View Code? Open in Web Editor NEW
1.6K 45.0 144.0 1.01 MB

Brings Rails named routes to javascript

Home Page: http://railsware.github.io/js-routes/

License: MIT License

Ruby 65.77% JavaScript 15.86% TypeScript 18.37%

js-routes's Introduction

JsRoutes

CI

Generates javascript file that defines all Rails named routes as javascript helpers

Intallation

Your Rails Gemfile:

gem "js-routes"

Setup

There are several possible ways to setup JsRoutes:

  • Quick and easy - Recommended
    • Uses Rack Middleware to automatically update routes locally
    • Automatically generates routes files on javascript build
    • Works great for a simple Rails application
  • Advanced Setup
    • Allows very custom setups
    • Automatic updates need to be customized
  • Webpacker ERB Loader - Legacy
    • Requires ESM module system (the default)
    • Doesn't support typescript definitions
  • Sprockets - Legacy
    • Deprecated and not recommended for modern apps

Quick Start

Setup Rack Middleware to automatically generate and maintain routes.js file and corresponding Typescript definitions routes.d.ts:

Use a Generator

Run a command:

rails generate js_routes:middleware

Setup Manually

Add the following to config/environments/development.rb:

  config.middleware.use(JsRoutes::Middleware)

Use it in any JS file:

import {post_path} from '../routes';

alert(post_path(1))

Upgrade js building process to update js-routes files in Rakefile:

task "javascript:build" => "js:routes:typescript"
# For setups without jsbundling-rails
task "assets:precompile" => "js:routes:typescript"

Add js-routes files to .gitignore:

/app/javascript/routes.js
/app/javascript/routes.d.ts

Webpacker ERB loader

IMPORTANT: the setup doesn't support IDE autocompletion with Typescript

Use a Generator

Run a command:

./bin/rails generate js_routes:webpacker

Setup manually

The routes files can be automatically updated without rake task being called manually. It requires rails-erb-loader npm package to work.

Add erb loader to webpacker:

yarn add rails-erb-loader
rm -f app/javascript/routes.js # delete static file if any

Create webpack ERB config config/webpack/loaders/erb.js:

module.exports = {
  module: {
    rules: [
      {
        test: /\.erb$/,
        enforce: 'pre',
        loader: 'rails-erb-loader'
      },
    ]
  }
};

Enable erb extension in config/webpack/environment.js:

const erb = require('./loaders/erb')
environment.loaders.append('erb', erb)

Create routes file app/javascript/routes.js.erb:

<%= JsRoutes.generate() %>

Use routes wherever you need them:

import {post_path} from 'routes.js.erb';

alert(post_path(2));

Advanced Setup

IMPORTANT: that this setup requires the JS routes file to be updates manually

Routes file can be generated with a rake task:

rake js:routes 
# OR for typescript support
rake js:routes:typescript

In case you need multiple route files for different parts of your application, you have to create the files manually. If your application has an admin and an application namespace for example:

IMPORTANT: Requires Webpacker ERB Loader setup.

// app/javascript/admin/routes.js.erb
<%= JsRoutes.generate(include: /admin/) %>
// app/javascript/customer/routes.js.erb
<%= JsRoutes.generate(exclude: /admin/) %>

You can manipulate the generated helper manually by injecting ruby into javascript:

export const routes = <%= JsRoutes.generate(module_type: nil, namespace: nil) %>

If you want to generate the routes files manually with custom options, you can use JsRoutes.generate!:

path = Rails.root.join("app/javascript")

JsRoutes.generate!(
  "#{path}/app_routes.js", exclude: [/^admin_/, /^api_/]
)
JsRoutes.generate!(
"#{path}/adm_routes.js", include: /^admin_/
)
JsRoutes.generate!(
  "#{path}/api_routes.js", include: /^api_/, default_url_options: {format: "json"}
)

Typescript Definitions

JsRoutes has typescript support out of the box.

Restrictions:

  • Only available if module_type is set to ESM (strongly recommended and default).
  • Webpacker Automatic Updates are not available because typescript compiler can not be configured to understand .erb extensions.

For the basic setup of typscript definitions see Quick Start setup. More advanced setup would involve calling manually:

JsRoutes.definitions! # to output to file
# or 
JsRoutes.definitions # to output to string

Even more advanced setups can be achieved by setting module_type to DTS inside configuration which will cause any JsRoutes instance to generate defintions instead of routes themselves.

Sprockets (Deprecated)

If you are using Sprockets you may configure js-routes in the following way.

Setup the initializer (e.g. config/initializers/js_routes.rb):

JsRoutes.setup do |config|
  config.module_type = nil
  config.namespace = 'Routes'
end

Require JsRoutes in app/assets/javascripts/application.js or other bundle

//= require js-routes

Also in order to flush asset pipeline cache sometimes you might need to run:

rake tmp:cache:clear

This cache is not flushed on server restart in development environment.

Important: If routes.js file is not updated after some configuration change you need to run this rake task again.

Configuration

You can configure JsRoutes in two main ways. Either with an initializer (e.g. config/initializers/js_routes.rb):

JsRoutes.setup do |config|
  config.option = value
end

Or dynamically in JavaScript, although only Formatter Options are supported:

import {configure, config} from 'routes'

configure({
  option: value
});
config(); // current config

Available Options

Generator Options

Options to configure JavaScript file generator. These options are only available in Ruby context but not JavaScript.

  • module_type - JavaScript module type for generated code. Article
    • Options: ESM, UMD, CJS, AMD, DTS, nil.
    • Default: ESM
    • nil option can be used in case you don't want generated code to export anything.
  • documentation - specifies if each route should be annotated with JSDoc comment
    • Default: true
  • exclude - Array of regexps to exclude from routes.
    • Default: []
    • The regexp applies only to the name before the _path suffix, eg: you want to match exactly settings_path, the regexp should be /^settings$/
  • include - Array of regexps to include in routes.
    • Default: []
    • The regexp applies only to the name before the _path suffix, eg: you want to match exactly settings_path, the regexp should be /^settings$/
  • namespace - global object used to access routes.
    • Only available if module_type option is set to nil.
    • Supports nested namespace like MyProject.routes
    • Default: nil
  • camel_case - specifies if route helpers should be generated in camel case instead of underscore case.
    • Default: false
  • url_links - specifies if *_url helpers should be generated (in addition to the default *_path helpers).
    • Default: false
    • Note: generated URLs will first use the protocol, host, and port options specified in the route definition. Otherwise, the URL will be based on the option specified in the default_url_options config. If no default option has been set, then the URL will fallback to the current URL based on window.location.
  • compact - Remove _path suffix in path routes(*_url routes stay untouched if they were enabled)
    • Default: false
    • Sample route call when option is set to true: users() // => /users
  • application - a key to specify which rails engine you want to generate routes too.
    • This option allows to only generate routes for a specific rails engine, that is mounted into routes instead of all Rails app routes
    • Default: Rails.application
  • file - a file location where generated routes are stored
    • Default: app/javascript/routes.js if setup with Webpacker, otherwise app/assets/javascripts/routes.js if setup with Sprockets.

Formatter Options

Options to configure routes formatting. These options are available both in Ruby and JavaScript context.

  • default_url_options - default parameters used when generating URLs
    • Example: {format: "json", trailing_slash: true, protocol: "https", subdomain: "api", host: "example.com", port: 3000}
    • Default: {}
  • prefix - string that will prepend any generated URL. Usually used when app URL root includes a path component.
    • Example: /rails-app
    • Default: Rails.application.config.relative_url_root
  • serializer - a JS function that serializes a Javascript Hash object into URL paramters like {a: 1, b: 2} => "a=1&b=2".
    • Default: nil. Uses built-in serializer compatible with Rails
    • Example: jQuery.param - use jQuery's serializer algorithm. You can attach serialize function from your favorite AJAX framework.
    • Example: function (object) { ... } - use completely custom serializer of your application.
  • special_options_key - a special key that helps JsRoutes to destinguish serialized model from options hash
    • This option exists because JS doesn't provide a difference between an object and a hash
    • Default: _options

Usage

Configuration above will create a nice javascript file with Routes object that has all the rails routes available:

import {
  user_path, user_project_path, company_path
} as Routes from 'routes';

users_path() 
  // => "/users"

user_path(1) 
  // => "/users/1"
  
user_path(1, {format: 'json'}) 
  // => "/users/1.json"

user_path(1, {anchor: 'profile'}) 
  // => "/users/1#profile"

new_user_project_path(1, {format: 'json'}) 
  // => "/users/1/projects/new.json"

user_project_path(1,2, {q: 'hello', custom: true}) 
  // => "/users/1/projects/2?q=hello&custom=true"

user_project_path(1,2, {hello: ['world', 'mars']}) 
  // => "/users/1/projects/2?hello%5B%5D=world&hello%5B%5D=mars"

var google = {id: 1, name: "Google"};
company_path(google) 
  // => "/companies/1"

var google = {id: 1, name: "Google", to_param: "google"};
company_path(google) 
  // => "/companies/google"

In order to make routes helpers available globally:

import * as Routes from '../routes';
jQuery.extend(window, Routes)

Get spec of routes and required params

Possible to get spec of route by function toString:

import {user_path, users_path}  from '../routes'

users_path.toString() // => "/users(.:format)"
user_path.toString() // => "/users/:id(.:format)"

Route function also contain method requiredParams inside which returns required param names array:

users_path.requiredParams() // => []
user_path.requiredParams() // => ['id']

Rails Compatibility

JsRoutes tries to replicate the Rails routing API as closely as possible. If you find any incompatibilities (outside of what is described below), please open an issue.

Object and Hash distinction issue

Sometimes the destinction between JS Hash and Object can not be found by JsRoutes. In this case you would need to pass a special key to help:

import {company_project_path} from '../routes'

company_project_path({company_id: 1, id: 2}) // => Not enough parameters
company_project_path({company_id: 1, id: 2, _options: true}) // => "/companies/1/projects/2"

What about security?

JsRoutes itself does not have security holes. It makes URLs without access protection more reachable by potential attacker. If that is an issue for you, you may use one of the following solutions:

ESM Tree shaking

Make sure module_type is set to ESM (the default). Modern JS bundlers like Webpack can statically determine which ESM exports are used, and remove the unused exports to reduce bundle size. This is known as Tree Shaking.

JS files can use named imports to import only required routes into the file, like:

import {
  inbox_path,
  inboxes_path,
  inbox_message_path,
  inbox_attachment_path,
  user_path,
} from '../routes'

JS files can also use star imports (import * as) for tree shaking, as long as only explicit property accesses are used.

import * as routes from '../routes';

console.log(routes.inbox_path); // OK, only `inbox_path` is included in the bundle

console.log(Object.keys(routes)); // forces bundler to include all exports, breaking tree shaking

Exclude option

Split your routes into multiple files related to each section of your website like:

// admin-routes.js.erb
<%= JsRoutes.generate(include: /^admin_/) %>
// app-routes.js.erb
<%= JsRoutes.generate(exclude: /^admin_/) %>

Advantages over alternatives

There are some alternatives available. Most of them has only basic feature and don't reach the level of quality I accept. Advantages of this one are:

  • Rails 4,5,6 support
  • ESM Tree shaking support
  • Rich options set
  • Full rails compatibility
  • Support Rails #to_param convention for seo optimized paths
  • Well tested

Thanks to contributors

Have fun

License

FOSSA Status

js-routes's People

Contributors

aaronjensen avatar alexaitken avatar bogdan avatar dark-panda avatar dougalcorn avatar ehelms avatar fb3 avatar fletcherm avatar flood4life avatar ihara2525 avatar kieranklaassen avatar kl-7 avatar krzysiek1507 avatar larryzhao avatar le0pard avatar levraipixel avatar madogiwa0124 avatar manuelmeurer avatar marshall-lee avatar mattisg avatar mjtko avatar pikachuexe avatar rosenfeld avatar rspace avatar swistaczek avatar teamon avatar tknarr avatar v1nayv avatar valff avatar vixp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-routes's Issues

needed to add :require => 'jsroutes' in my Gemfile

This is a really useful little library. Just wanted to let you know that I followed your installation instructions in the Readme, but I was getting the error uninitialized constant JsRoutes (NameError) . I changed my Gemfile entry to gem 'js-routes', :require => 'jsroutes' and now it works great!

Route Globbing

Route globbing is not supported properly in js-routes for Rails 3.2. NodeType.STAR should be an equivalent case to NodeType.GROUP; currently it's commented out. You can easily fix by adding above line 116 in lib/routes.js. This will allow wildcards to be supported without throwing an error.

Nested namespace in config options does not work

As described I added an initializer like this:

JsRoutes.setup do |config|
config.namespace = "app.routes"
end

But if I try to access the routes with the given namespace, I'll always get an 'undefined' error.
Checking this with firebug turned out, that the routes are available only under 'app'. Is there something broken ?

Asset pipeline approach autoreload

There is a problem with asset pipeline approach in production environment:

$ rake db:migrate RAILS_ENV=production
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
can't modify immutable index
/usr/lib/ruby/gems/1.8/gems/sprockets-2.0.0/lib/sprockets/index.rb:59:in `expire_index!'
/usr/lib/ruby/gems/1.8/gems/sprockets-2.0.0/lib/sprockets/processing.rb:91:in `register_preprocessor'
/home/bogdan/.bundler/ruby/1.8/js-routes-97d5c24f109f/lib/js_routes/engine.rb:8
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1.rc2/lib/active_support/lazy_load_hooks.rb:34:in `call'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1.rc2/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1.rc2/lib/active_support/lazy_load_hooks.rb:43:in `run_load_hooks'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1.rc2/lib/active_support/lazy_load_hooks.rb:42:in `each'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1.rc2/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
/usr/lib/ruby/gems/1.8/gems/railties-3.1.1.rc2/lib/rails/application/finisher.rb:56
/usr/lib/ruby/gems/1.8/gems/railties-3.1.1.rc2/lib/rails/initializable.rb:29:in `instance_exec'
/usr/lib/ruby/gems/1.8/gems/railties-3.1.1.rc2/lib/rails/initializable.rb:29:in `run'
/usr/lib/ruby/gems/1.8/gems/railties-3.1.1.rc2/lib/rails/initializable.rb:54:in `run_initializers'
/usr/lib/ruby/gems/1.8/gems/railties-3.1.1.rc2/lib/rails/initializable.rb:53:in `each'
/usr/lib/ruby/gems/1.8/gems/railties-3.1.1.rc2/lib/rails/initializable.rb:53:in `run_initializers'
/usr/lib/ruby/gems/1.8/gems/railties-3.1.1.rc2/lib/rails/application.rb:96:in `initialize!'
/usr/lib/ruby/gems/1.8/gems/railties-3.1.1.rc2/lib/rails/railtie/configurable.rb:30:in `send'
/usr/lib/ruby/gems/1.8/gems/railties-3.1.1.rc2/lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/bogdan/makabu/railsware/mailtrap/config/environment.rb:5
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1.rc2/lib/active_support/dependencies.rb:240:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1.rc2/lib/active_support/dependencies.rb:240:in `require'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1.rc2/lib/active_support/dependencies.rb:223:in `load_dependency'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1.rc2/lib/active_support/dependencies.rb:640:in `new_constants_in'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1.rc2/lib/active_support/dependencies.rb:223:in `load_dependency'
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1.rc2/lib/active_support/dependencies.rb:240:in `require'
/usr/lib/ruby/gems/1.8/gems/railties-3.1.1.rc2/lib/rails/application.rb:83:in `require_environment!'
/usr/lib/ruby/gems/1.8/gems/railties-3.1.1.rc2/lib/rails/application.rb:193:in `initialize_tasks'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:205:in `call'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:205:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:200:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:200:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:158:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:176:in `invoke_prerequisites'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:174:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:174:in `invoke_prerequisites'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:157:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/task.rb:144:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:112:in `invoke_task'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:90:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:90:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:90:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:84:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:62:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/application.rb:59:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2/bin/rake:32
/usr/bin/rake:19:in `load'
/usr/bin/rake:19

@mjtko: how can we turn off the asset our auto update code in this environment?

URL Prefix not working from initializer

I have two rails 4 apps deployed to nginx/unicorn using capistrano 3.

In production, I have them running as company.com/app1/ and company.com/app2/, so I am using js-routes to handle the URL prefix in javascript.

As described in the readme, I setup config/initializers/jsroutes.rb with:

JsRoutes.setup do |config|
  config.prefix = ENV['URL_PREFIX']
#  config.prefix = '/app2'
end

However when I referenced the routes in javascript, the routes did not include the prefix. I also tried hardcoding the prefix vs the ENV (see commented line above).

When I discovered @alex88's pull request #103, I followed @le0pard's advice and tried replacing my initializer with app/assets/javascripts/routes.js.erb:

<%= JsRoutes.generate({prefix: ENV['URL_PREFIX']}) %>

And suddenly it is working.

Does anyone know why? Does the readme need to be updated to reflect this as well?

config.include and config.exclude does not work correctly with rails engine.

The js routes are generated concatenated using the rails engine name and the route name using '_' on this line https://github.com/railsware/js-routes/blob/master/lib/js_routes.rb#L178
Example: engine_root_path

But if i have a config.include=/^engine_root$/ with the regex in underscores it does not work because the engine name and the route name are concatenated with empty string instead of underscore
https://github.com/railsware/js-routes/blob/master/lib/js_routes.rb#L149

It works only when i have config.include=/^engineroot$/

Nondeterministic behavior causing asset pipeline problems?

Hi, I'm seeing a problem while including js-routes in the asset pipeline in a Rails 3.2.3. We have two app servers which are precompiling assets. The signatures do not match between the compiled js on the two app servers and it looks like there is a difference in the order of the named routes that are being generated in the compiled javascript. I haven't looked too deeply into the code yet, but is it possible that this is a bug? I will investigate further tomorrow morning, but please point me in the right direction if this sounds possible.
Thanks,
Alex

Handling Prefix from Thin

I am working on a project that just switched to using Thin and we use the prefix option of Thin to define a URL namespace. In other words, when we start Thin up we specify a url prefix that gets prepended to all URLs. Is this currently supported? Would you consider supporting it or accepting a potential patch to support it? Our project is very excited to start using something like this and yours looks like the best and most robust implementation around.

Thanks.

JS Routes file cached

If I change the namespace in the js routes initializer and restart the server the assets are cached and the namespace doesn't show up in the js routes file. However, if I delete the cache in rails project and restart the server it regenerates the routes file. Is this intended behavior?

Feature request: Trailing slash urls

Having set trailing_slash: true in rails config action_controller.default_url_options doesn't make also the js routes library generate urls with trailing slash, is that something that can be done in another way?

Routes constraints issue

All routes with constraints are not available on js side.
How can I add missing routes in to Routes namespace?
All my missing routes from engines

mount Products::Hosting::Engine => '/products', constraints: Core::BrandAccessibilityConstraint.new

Next Gem Version

Just wanted to quickly check on when you are planning to push out the next version of the gem.

How to only generate the routes specified?

I just want to generate two routes user_path and settings_path, how could I do that?

I tried to add include to the initializer but it still generates all the routes, here is my initializer:

JsRoutes.setup do |config|
  config.include = [/user_path/, /settings_path/]
end

Paths missing from routes.js

I have the following routes file:

DSSRM::Application.routes.draw do
  get "site/welcome", :format => false, :defaults => { :format => 'html' }
  get "site/logout"
  get "site/access_denied"
  get "site/faq"
  get "site/contact"
  get "site/request_access"
  get "site/about"

  # Note: 'search' queries external databases. For an internal search, use index action with GET parameter 'q=...'
  get "people/search/:term", :controller => "people", :action => "search"
  post "people/import/:loginid", :controller => "people", :action => "import"

  resources :applications
  resources :entities
  resources :people
  resources :groups
  resources :ous
  resources :roles
  resources :majors
  resources :titles
  resources :affiliations
  resources :classifications

  namespace "admin" do
    get "dialogs/impersonate"
    get "dialogs/ip_whitelist"
    get "ops/impersonate/:loginid", :controller => "ops", :action => "impersonate"
    get "ops/unimpersonate"
    get "ad_path_check", :controller => "ops", :action => "ad_path_check"

    resources :api_whitelisted_ips
    resources :api_keys
  end

  root :to => 'site#welcome'
end

Everything seems to work fine except the people/import and people/search paths - they're simply missing - the word 'import' or 'search' appears nowhere in the generated routes.js.

If I take the parameter out and change the lines to this:

get "people/search", :controller => "people", :action => "search"
post "people/import", :controller => "people", :action => "import"

and re-generate the routes.js file, the paths are written to routes.js correctly.

Am I doing something wrong or is this a bug?

two suggestions

Before I forget, thank you very much for the gem.

  1. the default for prefix needs to look at either relative_url_root or possibly SCRIPT_NAME. There are issues with Rails (e.g. rails/rails#5122 and others).

  2. In your readme, remind dumb people like me to clear tmp/cache when adding an initializer. I added your gem, rendered a page, looked and poked at the javascript produced, and decided that I needed an initializer like you suggest. Re-rendered the page but nothing changed. I thought assets were cached only for production and it took me forever to figure out to clear tmp/cache.

Thank you again

JsRoutes not reading initializer file on heroku

Hi, since rails 4 has removed the initialize_on_precompile is there to load the custom initializer on assets:precompile running during heroku deployment?

The routes it generates doesn't have the settings I've set and I think it's because the initializer isn't loaded.

version bump after pull

I'm not sure I can upgrade my project to use your default gem until you do a version bump.

Reserved words in route segments

I have the unignored jammit route in my routes.rb.

  jammit  /assets/:package.:extension(.:format)  ...

It has path segment named 'package'. YUI Compressor fails to compress route.js with jammit route because it contains function with parameter named 'package'. It is the reserved word in javascript. I think, it is good idea to add sanitization of function parameter names to routes.js generator.

`initialize_on_precompile` is not used anymore.

The configuration setting, assets.initialize_on_precompile is removed in Rails 4 but is a prerequisite check in JsRoutes.assert_usable_configuration!. Error can be circumvented by adding the config, but this setting is effectively redundant.

Url with query parameters and anchor is reversed and invalid

Let's say I'm using the following helper to produce a route:

Routes.root_path({"page": "17",  "anchor":  "paragraph_7"});

It'll generate a url that looks like this:

"/#paragraph_7?page=17"

This URL seems to me to be invalid, and does not work as expected in my user case. When I request this URL on my local rails app, I see the following in my server log:

Started GET "/" for 127.0.0.1 at 2013-07-09 11:41:49 -0500
Processing by HomeController#index as HTML
User Load #snip

Notice the lack of the params hash, which is usually logged here. If I access the same URL, but reorder it as "/?page=17#paragraph_7" then I get the expected log:

Started GET "/?page=17" for 127.0.0.1 at 2013-07-09 11:42:23 -0500
Processing by HomeController#index as HTML
Parameters: {"page"=>"17"}
User Load #snip

As you can see, the server is never receiving the "page" parameter when the anchor tag is specified before it. The way URLs are parsed, the anchor tag must be placed last in the URL. I'll refer to this forum post which explains adequately.
http://www.webmasterworld.com/forum21/8405.htm

I don't think that this is a rails issue, but rather invalid HTTP. It could vary by browser, but I'm using latest version of Chrome. I'll be happy to try a fix and submit a pull request, but I would like someone to validate that this is an issue and not expected for some reason. And that I"m not using it wrong.

config.assets.initialize_on_precompile = false

Hi there,

I have been forced to remove js-routes from my app, as when trying to deploy on Digital Ocean with docker, I needed to set config.assets.initialize_on_precompile = false, and js-routes didn't allow me to do it.

Any idea how can I add it back?

I am using Rails 3.2.16

Error in combination with Devise

I experienced an error when trying to use JS Routes in combination with Devise: https://gist.github.com/1214173
Devise wasn't fully configured yet. I doubt this can be fixed.
The solution was to call JsRoutes.generate! in application.rb in an after_initialize block:

config.after_initialize do
  JsRoutes.generate!
end

Maybe add this to the README?

Support jQuery-style parameter arrays

jQuery's $.param function supports parameter arrays created by .serializeArray as shown here:

http://api.jquery.com/serializeArray/

[
  {
    name: "a",
    value: "1"
  },
  {
    name: "b",
    value: "2"
  },
  {
    name: "c",
    value: "3"
  },
  {
    name: "d",
    value: "4"
  },
  {
    name: "e",
    value: "5"
  }
]

Running this through $.param creates the following:

"a=1&b=2&c=3&d=4&e=5"

JsRoutes currently raises the following exception when trying a similar array:

Error: Url parameters should be a javascript hash

It would be nice if JsRoutes could support this style of parameter array. JavaScript hashes aren't ordered, hence the serialized array that jQuery provides, which does provide a specific order.

js-routes-0.7.1 doesn't starts with Rails 3.2

.../gems/js-routes-0.7.1/lib/js_routes.rb:115:in `optional_params': undefined method `source' for nil:NilClass (NoMethodError)
.../gems/js-routes-0.7.1/lib/js_routes.rb:129:in `build_params'
.../gems/js-routes-0.7.1/lib/js_routes.rb:100:in `build_js'
.../gems/js-routes-0.7.1/lib/js_routes.rb:87:in `block in js_routes'
.../gems/js-routes-0.7.1/lib/js_routes.rb:83:in `each'
.../gems/js-routes-0.7.1/lib/js_routes.rb:83:in `map'
.../gems/js-routes-0.7.1/lib/js_routes.rb:83:in `js_routes'
.../gems/js-routes-0.7.1/lib/js_routes.rb:65:in `generate'
.../gems/js-routes-0.7.1/lib/js_routes.rb:74:in `block (2 levels) in generate!'
.../gems/js-routes-0.7.1/lib/js_routes.rb:73:in `open'
.../gems/js-routes-0.7.1/lib/js_routes.rb:73:in `block in generate!'
.../gems/activesupport-3.2.0/lib/active_support/lazy_load_hooks.rb:34:in `call'
.../gems/activesupport-3.2.0/lib/active_support/lazy_load_hooks.rb:34:in `execute_hook'
.../gems/activesupport-3.2.0/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
.../gems/activesupport-3.2.0/lib/active_support/lazy_load_hooks.rb:42:in `each'
.../gems/activesupport-3.2.0/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
.../gems/railties-3.2.0/lib/rails/application/finisher.rb:60:in `block in <module:Finisher>'
.../gems/railties-3.2.0/lib/rails/initializable.rb:30:in `instance_exec'
.../gems/railties-3.2.0/lib/rails/initializable.rb:30:in `run'
.../gems/railties-3.2.0/lib/rails/initializable.rb:55:in `block in run_initializers'
.../gems/railties-3.2.0/lib/rails/initializable.rb:54:in `each'
.../gems/railties-3.2.0/lib/rails/initializable.rb:54:in `run_initializers'
.../gems/railties-3.2.0/lib/rails/application.rb:136:in `initialize!'
.../gems/railties-3.2.0/lib/rails/railtie/configurable.rb:30:in `method_missing'
../pd-mobile/config/environment.rb:5:in `<top (required)>'

How to set defaults (e.g. for locale)

Hello!

We have a locale namespace, and now we always have to submit the locale parameter when using Routes.some_path(I18n.locale). Is there a way to circumvent this?

Thanks you

Don't use parameter keys that are undefined or null.

To avoid the need to write many tedious if blocks before adding parameters, I would like the path helpers to exclude parameters that have a null or undefined value.

I'm currently needing to write a lot of statements like this:

var params = {};

if (gon.someFIeld) {
  params.someField = gon.someField;
}

Routes.some_path(params);

I would like to just write this:

Routes.some_path({ someField: gon.someField });

and if gon.someField is undefined, someField isn't added to the query string of the generated path.

Alternate Usage With Assets Pipeline

Instead of using the config/initializer/jsroutes.rb file you could create app/assets/javascripts/routes.js.erb that mimics what is in the gems' assets file.

<%= JsRoutes.assert_usable_configuration! && JsRoutes.generate({
  :exclude => [/^(new_|edit_){0,1}admin\_/] # skips routes starting with new_admin_, edit_admin_ and admin_
})
%>

Feature request: Route callbacks

Hello,

I'm not sure if this fits into the scope of this project at all but I think it would be neat if there was a tasteful way to wrap code logic inside of routes.

For code specific to a given route. Dunno, maybe that's something that should just be done on a per project basis but I feel like it fits.
At an admittedly unthoughtout implementation may look something like this...?

Routes.my_cool_page_route(function() {
   //go
});

I mean no, it's not robust enough to work in every situation but I always liked this feature in the WordPress theme known as "roots" where they have this main.js file that you can add basic per-page JavaScript to and only have it execute when that route is matched.

Dunno, just a thought.

Cannot get it to work

I'm having trouble getting this to work.

I have a Rails v4.1.0.rc1 app with an application namespace and an admin namespace, so I want to generate AppRoutes and AdminRoutes.

I use js-routes v0.9.7 and this is my initializer (just the AdminRoutes for now)

# config/initializers/js_routes.rb
require 'js_routes'

JsRoutes.generate! \
  file:      File.join('app', 'assets', 'javascripts', 'admin_routes.js'),
  namespace: 'AdminRoutes',
  include:   /admin/

Should I then use = require js-routes or = require admin_routes?

When I use = require js-routes, AdminRoutes is undefined and Routes contains all routes, not just the admin ones: http://screencast.com/t/uKGKH945aT0n

When I use = require admin_routes, Routes is undefined and AdminRoutes just contains options but no routes: http://screencast.com/t/6pHezoqU7

Both cases are not what I expect.
The file app/assets/javascripts/admin_routes.js is generated in both cases but apart from the boilerplate code doesn't contain any routes.
Very confusing all that. What am I doing wrong?

Strange things with Routes accessibility

I did as you wrote in readme:

  • echo "gem 'js-routes'" > gemfile && bundle
  • rake js:routes
  • + "= require js-routes" in application.js

I did "jQuery.extend(window, window.Routes)" in my application.js, because your call to extend seems unable to find Routes object.

The problem is, I cannot directly use Routes methods in my behavior files, only with quantification. What am I doing wrong?

rails = 3.2.9

rake js:routes is broken

Hi,

I just updated my js-routes install and get the following error when calling rake js:routes:

** Invoke js:routes (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute js:routes
rake aborted!
wrong number of arguments (0 for 1)
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/js-routes-0.8.0/lib/js_routes.rb:55:in `generate!'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/js-routes-0.8.0/lib/tasks/js_routes.rake:6:in `block (2 levels) in <top (required)>'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/simonc/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/bin/rake:19:in `load'
/Users/simonc/.rvm/gems/ruby-1.9.3-p125@gestionnaire/bin/rake:19:in `<main>'
Tasks: TOP => js:routes

I looked at the code and it seams the task is calling JsRoutes.generate! with no argument when the method is defined as def generate!(file_name, opts = {}).

Optional argument support is broken

Hi,

I think you made some recent changes that broke the support of optional arguments in routes.
Take, for example:
match 'foo/(:id)' => "foo#foo", :as => :foo
then
Routes.foo_path({id:42}) -> "/foo/42"
but
Routes.foo_path() -> ""

I've made a minimal example that I can send you if needed

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.