js-routes 1.4.11 → 1.4.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/CHANGELOG.md +7 -1
- data/Readme.md +85 -33
- data/js-routes.gemspec +1 -1
- data/lib/js_routes.rb +3 -2
- data/lib/js_routes/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87926593b4aa4a92ce85269fafd11238dbba0e8354bd4f4cffd03c56a765f519
|
4
|
+
data.tar.gz: ad3ee96de22c9b10c89ad38e96eede4ef1fa040051e37cb75e4697a4ecd366e4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a7309d0f38ab6a923d23571869da7206e0e1aa4541fff1354e15e8fffe3fe634001ee3959dc6033e30044010e63c7e9c910145fdea0f9c0a14f63b391314954
|
7
|
+
data.tar.gz: ca121924d248b98c5429c1b0a4e2052dd22d48b43eb97ca78b6d18caf29f79b2bb51b78a190c7cecbb2cdd20d2c772f3c137a0ddd11fe49cdd8f525ac9b087e0
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,9 +1,15 @@
|
|
1
1
|
## master
|
2
2
|
|
3
|
+
## v1.4.12
|
4
|
+
|
5
|
+
* Improve compatibility with node environment #269.
|
6
|
+
* Change default file location configuration to Webpacker if both Webpacker and Sprockets are loaded
|
7
|
+
|
8
|
+
## v1.4.11
|
9
|
+
|
3
10
|
* Use app/javascript/routes.js as a default file location if app/javascript directory exists
|
4
11
|
* Add `default` export for better experience when used as es6 module
|
5
12
|
|
6
|
-
|
7
13
|
## v1.4.10
|
8
14
|
|
9
15
|
* Require engine only when sprockets is loaded #257.
|
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|
|
@@ -53,7 +124,7 @@ Routes.config(); // current config
|
|
53
124
|
|
54
125
|
##### Generator Options
|
55
126
|
|
56
|
-
Options to configure JavaScript file generator
|
127
|
+
Options to configure JavaScript file generator. These options are only available in Ruby context but not JavaScript.
|
57
128
|
|
58
129
|
* `exclude` - Array of regexps to exclude from routes.
|
59
130
|
* Default: `[]`
|
@@ -77,75 +148,56 @@ Options to configure JavaScript file generator:
|
|
77
148
|
* This option allows to only generate routes for a specific rails engine, that is mounted into routes instead of all Rails app routes
|
78
149
|
* Default: `Rails.application`
|
79
150
|
* `file` - a file location where generated routes are stored
|
80
|
-
* Default: `app/
|
151
|
+
* Default: `app/javascript/routes.js` if setup with Webpacker, otherwise `app/assets/javascripts/routes.js` if setup with Sprockets.
|
81
152
|
|
82
153
|
##### Formatter Options
|
83
154
|
|
84
|
-
Options to configure routes formatting
|
155
|
+
Options to configure routes formatting. These options are available both in Ruby and JavaScript context.
|
85
156
|
|
86
157
|
* `default_url_options` - default parameters used when generating URLs
|
87
|
-
* Option is configurable at JS level with `Routes.configure()`
|
88
158
|
* Example: `{format: "json", trailing_slash: true, protocol: "https", subdomain: "api", host: "example.com", port: 3000}`
|
89
159
|
* Default: `{}`
|
90
|
-
* `prefix` -
|
91
|
-
*
|
92
|
-
* Example: `http://yourdomain.com`. This will cause route helpers to generate full path only.
|
160
|
+
* `prefix` - string that will prepend any generated URL. Usually used when app URL root includes a path component.
|
161
|
+
* Example: `/rails-app`
|
93
162
|
* Default: `Rails.application.config.relative_url_root`
|
94
163
|
* `serializer` - a JS function that serializes a Javascript Hash object into URL paramters like `{a: 1, b: 2} => "a=1&b=2"`.
|
95
164
|
* Default: `nil`. Uses built-in serializer compatible with Rails
|
96
|
-
* Option is configurable at JS level with `Routes.configure()`
|
97
165
|
* Example: `jQuery.param` - use jQuery's serializer algorithm. You can attach serialize function from your favorite AJAX framework.
|
98
166
|
* Example: `function (object) { ... }` - use completely custom serializer of your application.
|
99
167
|
* `special_options_key` - a special key that helps JsRoutes to destinguish serialized model from options hash
|
100
168
|
* This option exists because JS doesn't provide a difference between an object and a hash
|
101
|
-
* Option is configurable at JS level with `Routes.configure()`
|
102
169
|
* Default: `_options`
|
103
170
|
|
104
|
-
###
|
171
|
+
### Advanced Setup
|
105
172
|
|
106
173
|
In case you need multiple route files for different parts of your application, you have to create the files manually.
|
107
174
|
If your application has an `admin` and an `application` namespace for example:
|
108
175
|
|
109
176
|
```
|
110
|
-
# app/
|
177
|
+
# app/javascript/admin/routes.js.erb
|
111
178
|
<%= JsRoutes.generate(namespace: "AdminRoutes", include: /admin/) %>
|
112
|
-
|
113
|
-
# app/assets/javascripts/admin.js.coffee
|
114
|
-
#= require admin/routes
|
115
179
|
```
|
116
180
|
|
117
181
|
```
|
118
|
-
# app/
|
119
|
-
<%= JsRoutes.generate(namespace: "
|
120
|
-
|
121
|
-
# app/assets/javascripts/application.js.coffee
|
122
|
-
#= require application/routes
|
182
|
+
# app/javascript/customer/routes.js.erb
|
183
|
+
<%= JsRoutes.generate(namespace: "CustomerRoutes", exclude: /admin/) %>
|
123
184
|
```
|
124
185
|
|
125
186
|
In order to generate the routes JS code to a string:
|
126
187
|
|
127
|
-
```ruby
|
188
|
+
``` ruby
|
128
189
|
routes_js = JsRoutes.generate(options)
|
129
190
|
```
|
130
191
|
|
131
192
|
If you want to generate the routes files outside of the asset pipeline, you can use `JsRoutes.generate!`:
|
132
193
|
|
133
194
|
``` ruby
|
134
|
-
path = "app/
|
195
|
+
path = "app/javascript"
|
135
196
|
JsRoutes.generate!("#{path}/app_routes.js", namespace: "AppRoutes", exclude: [/^admin_/, /^api_/])
|
136
197
|
JsRoutes.generate!("#{path}/adm_routes.js", namespace: "AdmRoutes", include: /^admin_/)
|
137
198
|
JsRoutes.generate!("#{path}/api_routes.js", namespace: "ApiRoutes", include: /^api_/, default_url_options: {format: "json"})
|
138
199
|
```
|
139
200
|
|
140
|
-
### Rails relative URL root
|
141
|
-
|
142
|
-
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:
|
143
|
-
```
|
144
|
-
RAILS_RELATIVE_URL_ROOT=/Application1 RAILS_ENV=production bundle exec rake assets:precompile
|
145
|
-
```
|
146
|
-
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.
|
147
|
-
|
148
|
-
|
149
201
|
## Usage
|
150
202
|
|
151
203
|
Configuration above will create a nice javascript file with `Routes` object that has all the rails routes available:
|
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
@@ -15,10 +15,11 @@ class JsRoutes
|
|
15
15
|
exclude: [],
|
16
16
|
include: //,
|
17
17
|
file: -> do
|
18
|
+
webpacker_dir = Rails.root.join('app', 'javascript')
|
18
19
|
sprockets_dir = Rails.root.join('app','assets','javascripts')
|
19
20
|
sprockets_file = sprockets_dir.join('routes.js')
|
20
|
-
webpacker_file =
|
21
|
-
Dir.exists?(
|
21
|
+
webpacker_file = webpacker_dir.join('routes.js')
|
22
|
+
!Dir.exists?(webpacker_dir) && defined?(::Sprockets) ? sprockets_file : webpacker_file
|
22
23
|
end,
|
23
24
|
prefix: -> { Rails.application.config.relative_url_root || "" },
|
24
25
|
url_links: false,
|
data/lib/js_routes/version.rb
CHANGED
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.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bogdan Gusiev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-07 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:
|