js-routes 1.4.8 → 1.4.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/CHANGELOG.md +19 -1
- data/Readme.md +119 -57
- data/js-routes.gemspec +1 -1
- data/lib/js_routes.rb +17 -11
- data/lib/js_routes/engine.rb +0 -1
- data/lib/js_routes/version.rb +1 -1
- data/lib/routes.js +21 -14
- data/lib/routes.js.coffee +16 -11
- data/spec/dummy/app/assets/config/manifest.js +2 -0
- data/spec/js_routes/amd_compatibility_spec.rb +3 -8
- data/spec/js_routes/common_js_compatibility_spec.rb +12 -0
- data/spec/js_routes/generated_javascript_spec.rb +0 -5
- metadata +9 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8a952ba9e2f582074e459b770227628ee2cecf37cd50d03978ba9690bac5a94
|
4
|
+
data.tar.gz: 151da0e08b1b47fd6caa3bfbb1a5311ac0a528a9c88bb53b05dea181438823fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 586fc3bcbef2fc96c12b0d53b5f9fd58eb19b8f5987ca7a6dac068ecc6865c363a4207e69afd4d0fe0c6d252d7b0c507428c7257a99cabf6fc34ba0c5afe29ea
|
7
|
+
data.tar.gz: 10276a454e7f07fe6166957c18516214c34853859f4e8645e06105984e80fb18b77016cbc398a1cdd5351eba6f4d38ce78555f34498be04f35e353c341500c75
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,24 @@
|
|
1
1
|
## master
|
2
2
|
|
3
|
-
## v1.4.
|
3
|
+
## v1.4.14
|
4
|
+
|
5
|
+
* Fix compatibility with UMD modules #237 [Comment](https://github.com/railsware/js-routes/issues/237#issuecomment-752754679)
|
6
|
+
|
7
|
+
## v1.4.13
|
8
|
+
|
9
|
+
* Improve compatibility with node environment #269.
|
10
|
+
* Change default file location configuration to Webpacker if both Webpacker and Sprockets are loaded
|
11
|
+
|
12
|
+
## v1.4.11
|
13
|
+
|
14
|
+
* Use app/javascript/routes.js as a default file location if app/javascript directory exists
|
15
|
+
* Add `default` export for better experience when used as es6 module
|
16
|
+
|
17
|
+
## v1.4.10
|
18
|
+
|
19
|
+
* Require engine only when sprockets is loaded #257.
|
20
|
+
|
21
|
+
## v1.4.9
|
4
22
|
|
5
23
|
* Allow to specify null namespace and receive routes as an object without assigning it anywhere #247
|
6
24
|
|
data/Readme.md
CHANGED
@@ -12,9 +12,80 @@ Your Rails Gemfile:
|
|
12
12
|
gem "js-routes"
|
13
13
|
```
|
14
14
|
|
15
|
-
|
15
|
+
## Setup
|
16
16
|
|
17
|
-
|
17
|
+
Run:
|
18
|
+
|
19
|
+
```
|
20
|
+
rake js:routes
|
21
|
+
```
|
22
|
+
|
23
|
+
Make routes available globally in `app/javascript/packs/application.js`:
|
24
|
+
|
25
|
+
``` javascript
|
26
|
+
window.Routes = require('routes');
|
27
|
+
```
|
28
|
+
|
29
|
+
Individual routes can be imported using:
|
30
|
+
|
31
|
+
``` javascript
|
32
|
+
import {edit_post_path, new_post_path} from 'routes';
|
33
|
+
```
|
34
|
+
|
35
|
+
**Note**: that this setup requires `rake js:routes` to be run each time routes file is updated.
|
36
|
+
|
37
|
+
#### Webpacker + automatic updates
|
38
|
+
|
39
|
+
This setup can automatically update your routes without `rake js:routes` being called manually.
|
40
|
+
It requires [rails-erb-loader](https://github.com/usabilityhub/rails-erb-loader) npm package to work.
|
41
|
+
|
42
|
+
Add `erb` loader to webpacker:
|
43
|
+
|
44
|
+
``` sh
|
45
|
+
yarn add rails-erb-loader
|
46
|
+
rm -f app/javascript/routes.js # delete static file if any
|
47
|
+
```
|
48
|
+
|
49
|
+
Create webpack ERB config `config/webpack/loaders/erb.js`:
|
50
|
+
|
51
|
+
``` javascript
|
52
|
+
module.exports = {
|
53
|
+
test: /\.js\.erb$/,
|
54
|
+
enforce: 'pre',
|
55
|
+
exclude: /node_modules/,
|
56
|
+
use: [{
|
57
|
+
loader: 'rails-erb-loader',
|
58
|
+
options: {
|
59
|
+
runner: (/^win/.test(process.platform) ? 'ruby ' : '') + 'bin/rails runner'
|
60
|
+
}
|
61
|
+
}]
|
62
|
+
}
|
63
|
+
```
|
64
|
+
|
65
|
+
Enable `erb` extension in `config/webpacker/environment.js`:
|
66
|
+
|
67
|
+
```
|
68
|
+
const erb = require('./loaders/erb')
|
69
|
+
environment.loaders.append('erb', erb)
|
70
|
+
```
|
71
|
+
|
72
|
+
Create routes file `app/javascript/routes.js.erb`:
|
73
|
+
|
74
|
+
``` erb
|
75
|
+
<%= JsRoutes.generate() %>
|
76
|
+
```
|
77
|
+
|
78
|
+
Use routes wherever you need them `app/javascript/packs/application.js`:
|
79
|
+
|
80
|
+
``` javascript
|
81
|
+
window.Routes = require('routes.js.erb');
|
82
|
+
```
|
83
|
+
|
84
|
+
#### Sprockets (Deprecated)
|
85
|
+
|
86
|
+
If you are using [Sprockets](https://github.com/rails/sprockets-rails) you may configure js-routes in the following way.
|
87
|
+
|
88
|
+
Require JsRoutes in `app/assets/javascripts/application.js` or other bundle
|
18
89
|
|
19
90
|
``` js
|
20
91
|
//= require js-routes
|
@@ -32,7 +103,7 @@ This cache is not flushed on server restart in development environment.
|
|
32
103
|
|
33
104
|
### Configuration
|
34
105
|
|
35
|
-
You can configure JsRoutes in two main ways. Either with an initializer (e.g. `config/initializers/
|
106
|
+
You can configure JsRoutes in two main ways. Either with an initializer (e.g. `config/initializers/js_routes.rb`):
|
36
107
|
|
37
108
|
``` ruby
|
38
109
|
JsRoutes.setup do |config|
|
@@ -40,7 +111,7 @@ JsRoutes.setup do |config|
|
|
40
111
|
end
|
41
112
|
```
|
42
113
|
|
43
|
-
Or dynamically in JavaScript, although
|
114
|
+
Or dynamically in JavaScript, although only [Formatter Options](#formatter-options) are supported (see below)
|
44
115
|
|
45
116
|
``` js
|
46
117
|
Routes.configure({
|
@@ -49,93 +120,84 @@ Routes.configure({
|
|
49
120
|
Routes.config(); // current config
|
50
121
|
```
|
51
122
|
|
52
|
-
Available
|
123
|
+
#### Available Options
|
124
|
+
|
125
|
+
##### Generator Options
|
126
|
+
|
127
|
+
Options to configure JavaScript file generator. These options are only available in Ruby context but not JavaScript.
|
53
128
|
|
54
|
-
* `default_url_options` - default parameters used when generating URLs
|
55
|
-
* Option is configurable at JS level with `Routes.configure()`
|
56
|
-
* Example: {:format => "json", :trailing\_slash => true, :protocol => "https", :subdomain => "api", :host => "example.com", :port => 3000}
|
57
|
-
* Default: {}
|
58
129
|
* `exclude` - Array of regexps to exclude from routes.
|
59
|
-
* Default: []
|
130
|
+
* Default: `[]`
|
60
131
|
* The regexp applies only to the name before the `_path` suffix, eg: you want to match exactly `settings_path`, the regexp should be `/^settings$/`
|
61
132
|
* `include` - Array of regexps to include in routes.
|
62
|
-
* Default: []
|
133
|
+
* Default: `[]`
|
63
134
|
* The regexp applies only to the name before the `_path` suffix, eg: you want to match exactly `settings_path`, the regexp should be `/^settings$/`
|
64
135
|
* `namespace` - global object used to access routes.
|
65
136
|
* Supports nested namespace like `MyProject.routes`
|
66
137
|
* Default: `Routes`
|
67
|
-
* `
|
68
|
-
*
|
69
|
-
|
70
|
-
*
|
71
|
-
* `
|
72
|
-
* Default: false
|
73
|
-
* `url_links` (version >= 0.8.9) - Generate `*_url` helpers (in addition to the default `*_path` helpers).
|
74
|
-
* Example: true
|
75
|
-
* Default: false
|
138
|
+
* `camel_case` - Generate camel case route names.
|
139
|
+
* Default: `false`
|
140
|
+
* `url_links` - Generate `*_url` helpers (in addition to the default `*_path` helpers).
|
141
|
+
* Example: `true`
|
142
|
+
* Default: `false`
|
76
143
|
* 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`.
|
77
|
-
* `compact`
|
78
|
-
* Default: false
|
144
|
+
* `compact` - Remove `_path` suffix in path routes(`*_url` routes stay untouched if they were enabled)
|
145
|
+
* Default: `false`
|
79
146
|
* Sample route call when option is set to true: Routes.users() => `/users`
|
80
|
-
* `serializer` (version >= 1.1.0) - Puts a JS function here that serializes a Javascript Hash object into URL paramters: `{a: 1, b: 2} => "a=1&b=2"`.
|
81
|
-
* Default: `nil`. Uses built-in serializer
|
82
|
-
* Option is configurable at JS level with `Routes.configure()`
|
83
|
-
* Example: `jQuery.param` - use jQuery's serializer algorithm. You can attach serialize function from your favorite AJAX framework.
|
84
|
-
* Example: `MyApp.custom_serialize` - use completely custom serializer of your application.
|
85
|
-
|
86
|
-
* `special_options_key` - a special key that helps JsRoutes to destinguish serialized model from options hash
|
87
|
-
* This option is required because JS doesn't provide a difference between an object and a hash
|
88
|
-
* Option is configurable at JS level with `Routes.configure()`
|
89
|
-
* Default: `_options`
|
90
147
|
* `application` - a key to specify which rails engine you want to generate routes too.
|
91
148
|
* This option allows to only generate routes for a specific rails engine, that is mounted into routes instead of all Rails app routes
|
92
149
|
* Default: `Rails.application`
|
150
|
+
* `file` - a file location where generated routes are stored
|
151
|
+
* Default: `app/javascript/routes.js` if setup with Webpacker, otherwise `app/assets/javascripts/routes.js` if setup with Sprockets.
|
152
|
+
|
153
|
+
##### Formatter Options
|
154
|
+
|
155
|
+
Options to configure routes formatting. These options are available both in Ruby and JavaScript context.
|
93
156
|
|
94
|
-
|
157
|
+
* `default_url_options` - default parameters used when generating URLs
|
158
|
+
* Example: `{format: "json", trailing_slash: true, protocol: "https", subdomain: "api", host: "example.com", port: 3000}`
|
159
|
+
* Default: `{}`
|
160
|
+
* `prefix` - string that will prepend any generated URL. Usually used when app URL root includes a path component.
|
161
|
+
* Example: `/rails-app`
|
162
|
+
* Default: `Rails.application.config.relative_url_root`
|
163
|
+
* `serializer` - a JS function that serializes a Javascript Hash object into URL paramters like `{a: 1, b: 2} => "a=1&b=2"`.
|
164
|
+
* Default: `nil`. Uses built-in serializer compatible with Rails
|
165
|
+
* Example: `jQuery.param` - use jQuery's serializer algorithm. You can attach serialize function from your favorite AJAX framework.
|
166
|
+
* Example: `function (object) { ... }` - use completely custom serializer of your application.
|
167
|
+
* `special_options_key` - a special key that helps JsRoutes to destinguish serialized model from options hash
|
168
|
+
* This option exists because JS doesn't provide a difference between an object and a hash
|
169
|
+
* Default: `_options`
|
170
|
+
|
171
|
+
### Advanced Setup
|
95
172
|
|
96
173
|
In case you need multiple route files for different parts of your application, you have to create the files manually.
|
97
174
|
If your application has an `admin` and an `application` namespace for example:
|
98
175
|
|
99
176
|
```
|
100
|
-
# app/
|
177
|
+
# app/javascript/admin/routes.js.erb
|
101
178
|
<%= JsRoutes.generate(namespace: "AdminRoutes", include: /admin/) %>
|
102
|
-
|
103
|
-
# app/assets/javascripts/admin.js.coffee
|
104
|
-
#= require admin/routes
|
105
179
|
```
|
106
180
|
|
107
181
|
```
|
108
|
-
# app/
|
109
|
-
<%= JsRoutes.generate(namespace: "
|
110
|
-
|
111
|
-
# app/assets/javascripts/application.js.coffee
|
112
|
-
#= require application/routes
|
182
|
+
# app/javascript/customer/routes.js.erb
|
183
|
+
<%= JsRoutes.generate(namespace: "CustomerRoutes", exclude: /admin/) %>
|
113
184
|
```
|
114
185
|
|
115
186
|
In order to generate the routes JS code to a string:
|
116
187
|
|
117
|
-
```ruby
|
188
|
+
``` ruby
|
118
189
|
routes_js = JsRoutes.generate(options)
|
119
190
|
```
|
120
191
|
|
121
192
|
If you want to generate the routes files outside of the asset pipeline, you can use `JsRoutes.generate!`:
|
122
193
|
|
123
194
|
``` ruby
|
124
|
-
path = "app/
|
125
|
-
JsRoutes.generate!("#{path}/app_routes.js", :
|
126
|
-
JsRoutes.generate!("#{path}/adm_routes.js", :
|
127
|
-
JsRoutes.generate!("#{path}/api_routes.js", :
|
195
|
+
path = "app/javascript"
|
196
|
+
JsRoutes.generate!("#{path}/app_routes.js", namespace: "AppRoutes", exclude: [/^admin_/, /^api_/])
|
197
|
+
JsRoutes.generate!("#{path}/adm_routes.js", namespace: "AdmRoutes", include: /^admin_/)
|
198
|
+
JsRoutes.generate!("#{path}/api_routes.js", namespace: "ApiRoutes", include: /^api_/, default_url_options: {format: "json"})
|
128
199
|
```
|
129
200
|
|
130
|
-
### Rails relative URL root
|
131
|
-
|
132
|
-
If you've installed your application in a sub-path or sub-URI of your server instead of at the root, you need to set the `RAILS_RELATIVE_URL_ROOT` environment variable to the correct path prefix for your application when you precompile assets. Eg., if your application's base URL is "https://appl.example.com/Application1", the command to precompile assets would be:
|
133
|
-
```
|
134
|
-
RAILS_RELATIVE_URL_ROOT=/Application1 RAILS_ENV=production bundle exec rake assets:precompile
|
135
|
-
```
|
136
|
-
The environment variable is only needed for precompilation of assets, at any other time (eg. when assets are compiled on-the-fly as in the development environment) Rails will set the relative URL root correctly on it's own.
|
137
|
-
|
138
|
-
|
139
201
|
## Usage
|
140
202
|
|
141
203
|
Configuration above will create a nice javascript file with `Routes` object that has all the rails routes available:
|
@@ -206,7 +268,7 @@ Routes.company_project_path({company_id: 1, id: 2, _options: true}) // => "/comp
|
|
206
268
|
|
207
269
|
## What about security?
|
208
270
|
|
209
|
-
JsRoutes itself
|
271
|
+
JsRoutes itself does not have security holes. It makes URLs
|
210
272
|
without access protection more reachable by potential attacker.
|
211
273
|
In order to prevent this use `:exclude` option for sensitive urls like `/admin_/`
|
212
274
|
|
data/js-routes.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |s|
|
|
21
21
|
s.summary = %q{Brings Rails named routes to javascript}
|
22
22
|
|
23
23
|
s.add_runtime_dependency(%q<railties>, [">= 4"])
|
24
|
-
s.
|
24
|
+
s.add_development_dependency(%q<sprockets-rails>)
|
25
25
|
s.add_development_dependency(%q<rspec>, [">= 3.0.0"])
|
26
26
|
s.add_development_dependency(%q<bundler>, [">= 1.1.0"])
|
27
27
|
s.add_development_dependency(%q<coffee-script>, [">= 0"])
|
data/lib/js_routes.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'uri'
|
2
|
-
|
2
|
+
if defined?(::Rails) && defined?(::Sprockets::Railtie)
|
3
|
+
require 'js_routes/engine'
|
4
|
+
end
|
3
5
|
require 'js_routes/version'
|
4
6
|
|
5
7
|
class JsRoutes
|
@@ -8,13 +10,17 @@ class JsRoutes
|
|
8
10
|
# OPTIONS
|
9
11
|
#
|
10
12
|
|
11
|
-
DEFAULT_PATH = File.join('app','assets','javascripts','routes.js')
|
12
|
-
|
13
13
|
DEFAULTS = {
|
14
|
-
namespace: "Routes",
|
14
|
+
namespace: -> { defined?(Webpacker) ? nil : "Routes" },
|
15
15
|
exclude: [],
|
16
16
|
include: //,
|
17
|
-
file:
|
17
|
+
file: -> do
|
18
|
+
webpacker_dir = Rails.root.join('app', 'javascript')
|
19
|
+
sprockets_dir = Rails.root.join('app','assets','javascripts')
|
20
|
+
sprockets_file = sprockets_dir.join('routes.js')
|
21
|
+
webpacker_file = webpacker_dir.join('routes.js')
|
22
|
+
!Dir.exists?(webpacker_dir) && defined?(::Sprockets) ? sprockets_file : webpacker_file
|
23
|
+
end,
|
18
24
|
prefix: -> { Rails.application.config.relative_url_root || "" },
|
19
25
|
url_links: false,
|
20
26
|
camel_case: false,
|
@@ -23,7 +29,7 @@ class JsRoutes
|
|
23
29
|
serializer: nil,
|
24
30
|
special_options_key: "_options",
|
25
31
|
application: -> { Rails.application }
|
26
|
-
}
|
32
|
+
} #:nodoc:
|
27
33
|
|
28
34
|
NODE_TYPES = {
|
29
35
|
GROUP: 1,
|
@@ -34,11 +40,11 @@ class JsRoutes
|
|
34
40
|
LITERAL: 6,
|
35
41
|
SLASH: 7,
|
36
42
|
DOT: 8
|
37
|
-
}
|
43
|
+
} #:nodoc:
|
38
44
|
|
39
|
-
LAST_OPTIONS_KEY = "options".freeze
|
40
|
-
FILTERED_DEFAULT_PARTS = [:controller, :action]
|
41
|
-
URL_OPTIONS = [:protocol, :domain, :host, :port, :subdomain]
|
45
|
+
LAST_OPTIONS_KEY = "options".freeze #:nodoc:
|
46
|
+
FILTERED_DEFAULT_PARTS = [:controller, :action] #:nodoc:
|
47
|
+
URL_OPTIONS = [:protocol, :domain, :host, :port, :subdomain] #:nodoc:
|
42
48
|
|
43
49
|
class Configuration < Struct.new(*DEFAULTS.keys)
|
44
50
|
def initialize(attributes = nil)
|
@@ -215,7 +221,7 @@ class JsRoutes
|
|
215
221
|
parent_spec = parent_route.try(:path).try(:spec)
|
216
222
|
route_arguments = route_js_arguments(route, parent_spec)
|
217
223
|
url_link = generate_url_link(name, route_arguments)
|
218
|
-
|
224
|
+
<<-JS.strip!
|
219
225
|
// #{name.join('.')} => #{parent_spec}#{route.path.spec}
|
220
226
|
// function(#{build_params(route.required_parts)})
|
221
227
|
#{route_name}: Utils.route(#{route_arguments})#{",\n" + url_link if url_link.length > 0}
|
data/lib/js_routes/engine.rb
CHANGED
@@ -47,7 +47,6 @@ class Engine < ::Rails::Engine
|
|
47
47
|
when -> (v) { v2.match?('', v) },
|
48
48
|
-> (v) { vgte3.match?('', v) }
|
49
49
|
|
50
|
-
# Other rails version, assumed newer
|
51
50
|
Rails.application.config.assets.configure do |config|
|
52
51
|
config.register_preprocessor(
|
53
52
|
"application/javascript",
|
data/lib/js_routes/version.rb
CHANGED
data/lib/routes.js
CHANGED
@@ -4,12 +4,10 @@ Based on Rails RAILS_VERSION routes of APP_CLASS
|
|
4
4
|
*/
|
5
5
|
|
6
6
|
(function() {
|
7
|
-
var DeprecatedGlobbingBehavior, NodeTypes, ParameterMissing, ReservedOptions, SpecialOptionsKey, UriEncoderSegmentRegex, Utils,
|
7
|
+
var DeprecatedGlobbingBehavior, NodeTypes, ParameterMissing, ReservedOptions, SpecialOptionsKey, UriEncoderSegmentRegex, Utils, error, result,
|
8
8
|
hasProp = {}.hasOwnProperty,
|
9
9
|
slice = [].slice;
|
10
10
|
|
11
|
-
root = typeof exports !== "undefined" && exports !== null ? exports : this;
|
12
|
-
|
13
11
|
ParameterMissing = function(message, fileName, lineNumber) {
|
14
12
|
var instance;
|
15
13
|
instance = new Error(message, fileName, lineNumber);
|
@@ -437,8 +435,8 @@ Based on Rails RAILS_VERSION routes of APP_CLASS
|
|
437
435
|
return this._classToTypeCache;
|
438
436
|
},
|
439
437
|
get_object_type: function(obj) {
|
440
|
-
if (
|
441
|
-
return
|
438
|
+
if (this.jQuery && (this.jQuery.type != null)) {
|
439
|
+
return this.jQuery.type(obj);
|
442
440
|
}
|
443
441
|
if (obj == null) {
|
444
442
|
return "" + obj;
|
@@ -497,25 +495,34 @@ Based on Rails RAILS_VERSION routes of APP_CLASS
|
|
497
495
|
routes.config = function() {
|
498
496
|
return Utils.config();
|
499
497
|
};
|
500
|
-
Object.defineProperty(routes, 'defaults', {
|
501
|
-
get: function() {
|
502
|
-
throw new Error(NAMESPACE + ".defaults is removed. Use " + NAMESPACE + ".configure() instead.");
|
503
|
-
},
|
504
|
-
set: function(value) {}
|
505
|
-
});
|
506
498
|
routes.default_serializer = function(object, prefix) {
|
507
499
|
return Utils.default_serializer(object, prefix);
|
508
500
|
};
|
509
|
-
return
|
501
|
+
return Object.assign({
|
502
|
+
"default": routes
|
503
|
+
}, routes);
|
510
504
|
}
|
511
505
|
};
|
512
506
|
|
507
|
+
result = Utils.make();
|
508
|
+
|
513
509
|
if (typeof define === "function" && define.amd) {
|
514
510
|
define([], function() {
|
515
|
-
return
|
511
|
+
return result;
|
516
512
|
});
|
513
|
+
} else if (typeof module !== "undefined" && module !== null) {
|
514
|
+
try {
|
515
|
+
module.exports = result;
|
516
|
+
} catch (error1) {
|
517
|
+
error = error1;
|
518
|
+
if (error.name !== 'TypeError') {
|
519
|
+
throw error;
|
520
|
+
}
|
521
|
+
}
|
517
522
|
} else {
|
518
|
-
|
523
|
+
Utils.namespace(this, NAMESPACE, result);
|
519
524
|
}
|
520
525
|
|
526
|
+
return result;
|
527
|
+
|
521
528
|
}).call(this);
|
data/lib/routes.js.coffee
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
File generated by js-routes GEM_VERSION
|
3
3
|
Based on Rails RAILS_VERSION routes of APP_CLASS
|
4
4
|
###
|
5
|
-
root = (exports ? this)
|
6
5
|
|
7
6
|
ParameterMissing = (message, fileName, lineNumber) ->
|
8
7
|
instance = new Error(message, fileName, lineNumber)
|
@@ -363,7 +362,7 @@ Utils =
|
|
363
362
|
@_classToTypeCache["[object #{name}]"] = name.toLowerCase()
|
364
363
|
@_classToTypeCache
|
365
364
|
get_object_type: (obj) ->
|
366
|
-
return
|
365
|
+
return this.jQuery.type(obj) if this.jQuery and this.jQuery.type?
|
367
366
|
return "#{obj}" unless obj?
|
368
367
|
(if typeof obj is "object" or typeof obj is "function" then @_classToType()[Object::toString.call(obj)] or "object" else typeof obj)
|
369
368
|
|
@@ -393,19 +392,25 @@ Utils =
|
|
393
392
|
routes = ROUTES
|
394
393
|
routes.configure = (config) -> Utils.configure(config)
|
395
394
|
routes.config = -> Utils.config()
|
396
|
-
Object.defineProperty routes, 'defaults',
|
397
|
-
get: ->
|
398
|
-
throw new Error("#{NAMESPACE}.defaults is removed. Use #{NAMESPACE}.configure() instead.")
|
399
|
-
set: (value) ->
|
400
|
-
|
401
395
|
routes.default_serializer = (object, prefix) ->
|
402
396
|
Utils.default_serializer(object, prefix)
|
403
|
-
|
404
|
-
|
397
|
+
Object.assign({default: routes}, routes)
|
398
|
+
|
399
|
+
result = Utils.make()
|
405
400
|
|
406
401
|
# Set up Routes appropriately for the environment.
|
407
402
|
if typeof define is "function" and define.amd
|
408
403
|
# AMD
|
409
|
-
define [], ->
|
404
|
+
define [], -> result
|
405
|
+
else if module?
|
406
|
+
# CommonJS
|
407
|
+
try
|
408
|
+
module.exports = result
|
409
|
+
catch error
|
410
|
+
unless error.name == 'TypeError'
|
411
|
+
throw error
|
410
412
|
else
|
411
|
-
|
413
|
+
# Browser globals
|
414
|
+
Utils.namespace(this, NAMESPACE, result)
|
415
|
+
|
416
|
+
return result
|
@@ -27,16 +27,11 @@ EOF
|
|
27
27
|
evaljs(JsRoutes.generate({}))
|
28
28
|
end
|
29
29
|
|
30
|
-
it "should working from global scope" do
|
31
|
-
expect(evaljs("Routes.inboxes_path()")).to eq(test_routes.inboxes_path())
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should working from define function" do
|
35
|
-
expect(evaljs("Routes.inboxes_path()")).to eq(evaljs("GlobalCheck['js-routes'].inboxes_path()"))
|
36
|
-
end
|
37
|
-
|
38
30
|
it "should working from require" do
|
39
31
|
expect(evaljs("require(['js-routes'], function(r){ return r.inboxes_path(); })")).to eq(test_routes.inboxes_path())
|
40
32
|
end
|
41
33
|
|
34
|
+
it "should define default export for es6 modules" do
|
35
|
+
expect(evaljs("require(['js-routes'], function(r){ return r.default.inboxes_path(); })")).to eq(test_routes.inboxes_path())
|
36
|
+
end
|
42
37
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe JsRoutes, "compatibility with CommonJS (node)" do
|
4
|
+
before(:each) do
|
5
|
+
evaljs("module = { exports: null }")
|
6
|
+
evaljs(JsRoutes.generate({}))
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should define module exports" do
|
10
|
+
expect(evaljs("module.exports.inboxes_path()")).to eq(test_routes.inboxes_path())
|
11
|
+
end
|
12
|
+
end
|
@@ -60,11 +60,6 @@ describe JsRoutes do
|
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should not generate file before initialization" do
|
63
|
-
# This method is alread fixed in Rails master
|
64
|
-
# But in 3.2 stable we need to hack it like this
|
65
|
-
if Rails.application.instance_variable_get("@initialized")
|
66
|
-
pending
|
67
|
-
end
|
68
63
|
expect(File.exists?(name)).to be_falsey
|
69
64
|
end
|
70
65
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js-routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bogdan Gusiev
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -31,7 +31,7 @@ dependencies:
|
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
|
-
type: :
|
34
|
+
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
@@ -172,9 +172,11 @@ files:
|
|
172
172
|
- lib/routes.js
|
173
173
|
- lib/routes.js.coffee
|
174
174
|
- lib/tasks/js_routes.rake
|
175
|
+
- spec/dummy/app/assets/config/manifest.js
|
175
176
|
- spec/dummy/app/assets/javascripts/.gitkeep
|
176
177
|
- spec/dummy/config/routes.rb
|
177
178
|
- spec/js_routes/amd_compatibility_spec.rb
|
179
|
+
- spec/js_routes/common_js_compatibility_spec.rb
|
178
180
|
- spec/js_routes/default_serializer_spec.rb
|
179
181
|
- spec/js_routes/generated_javascript_spec.rb
|
180
182
|
- spec/js_routes/options_spec.rb
|
@@ -186,7 +188,7 @@ homepage: http://github.com/railsware/js-routes
|
|
186
188
|
licenses:
|
187
189
|
- MIT
|
188
190
|
metadata: {}
|
189
|
-
post_install_message:
|
191
|
+
post_install_message:
|
190
192
|
rdoc_options: []
|
191
193
|
require_paths:
|
192
194
|
- lib
|
@@ -201,9 +203,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
201
203
|
- !ruby/object:Gem::Version
|
202
204
|
version: '0'
|
203
205
|
requirements: []
|
204
|
-
|
205
|
-
|
206
|
-
signing_key:
|
206
|
+
rubygems_version: 3.2.0
|
207
|
+
signing_key:
|
207
208
|
specification_version: 4
|
208
209
|
summary: Brings Rails named routes to javascript
|
209
210
|
test_files: []
|