js_from_routes 1.0.1 โ 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/README.md +40 -11
- data/lib/js_from_routes/template.js.erb +22 -0
- data/lib/js_from_routes/version.rb +1 -1
- data/spec/js_from_routes/js_from_routes_spec.rb +1 -1
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8202e9323cb738556cdc4e126c7c343eaae6b01d52a72bdff00eb93b7ab925af
|
4
|
+
data.tar.gz: 3caa18595ef98a85e8dd7f162e0f530d708a464c2bcc1fd823b4cfbaea947af7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3bba34efe8b9a91114708326e556dcdfe7549231d61aeb366bb16759471615f8aedb87692eab8420b0039695b359e02da5be4f974c604a535a4ae19efa131ecc
|
7
|
+
data.tar.gz: f54793177eb17df4e42548775f1dbd935b366f838d49488947100f606008eefc48d96b6ad38c0611d95bc5d267c52a24aead758856ddfdd0c87adc3b745e46bf
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
## JsFromRoutes 1.0.2 (2021-03-10) ##
|
2
|
+
|
3
|
+
* Ensure a default `template.js.erb` ships with the gem.
|
4
|
+
|
5
|
+
|
6
|
+
## JsFromRoutes 1.0.1 (2020-06-21) ##
|
7
|
+
|
8
|
+
* Expose `export_setting` in the `Route` API, to support custom workflows.
|
9
|
+
|
10
|
+
|
11
|
+
## JsFromRoutes 1.0.0 (2020-06-21) ##
|
12
|
+
|
13
|
+
* Initial Release.
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ JS From Rails Routes
|
|
4
4
|
<a href="https://travis-ci.org/ElMassimo/js_from_routes"><img alt="Build Status" src="https://travis-ci.org/ElMassimo/js_from_routes.svg"/></a>
|
5
5
|
<a href="http://inch-ci.org/github/ElMassimo/js_from_routes"><img alt="Inline docs" src="http://inch-ci.org/github/ElMassimo/js_from_routes.svg"/></a>
|
6
6
|
<a href="https://codeclimate.com/github/ElMassimo/js_from_routes"><img alt="Maintainability" src="https://codeclimate.com/github/ElMassimo/js_from_routes/badges/gpa.svg"/></a>
|
7
|
-
<a href="https://codeclimate.com/github/ElMassimo/js_from_routes"><img alt="Test Coverage" src="https://codeclimate.com/github/ElMassimo/
|
7
|
+
<a href="https://codeclimate.com/github/ElMassimo/js_from_routes"><img alt="Test Coverage" src="https://codeclimate.com/github/ElMassimo/js_from_routes/badges/coverage.svg"/></a>
|
8
8
|
<a href="https://rubygems.org/gems/js_from_routes"><img alt="Gem Version" src="https://img.shields.io/gem/v/js_from_routes.svg?colorB=e9573f"/></a>
|
9
9
|
<a href="https://github.com/ElMassimo/js_from_routes/blob/master/LICENSE.txt"><img alt="License" src="https://img.shields.io/badge/license-MIT-428F7E.svg"/></a>
|
10
10
|
</p>
|
@@ -14,6 +14,8 @@ _JS from Routes_ helps you by automatically generating path and API helpers from
|
|
14
14
|
Rails route definitions, allowing you to save development effort and focus on
|
15
15
|
the things that matter.
|
16
16
|
|
17
|
+
Check out [this pull request](https://github.com/ElMassimo/pingcrm-vite/pull/2) to get a sense of how flexible it can be.
|
18
|
+
|
17
19
|
### Why? ๐ค
|
18
20
|
|
19
21
|
Path helpers in Rails are useful, and make it easier to build urls, avoiding
|
@@ -26,10 +28,12 @@ With this library, it's possible the enjoy the same benefits in JS:
|
|
26
28
|
an error that is easier to detect than a 404.
|
27
29
|
- We can embed the the HTTP verb in the helper. Changing the verb in the route causes the JS
|
28
30
|
code to be regenerated, no need to update the consumer!
|
31
|
+
|
32
|
+
Read more about it in the [blog announcement](https://maximomussini.com/posts/js-from-routes/).
|
29
33
|
|
30
34
|
### Installation ๐ฟ
|
31
35
|
|
32
|
-
Add this line to your application's Gemfile:
|
36
|
+
Add this line to your application's Gemfile in the `development` group:
|
33
37
|
|
34
38
|
```ruby
|
35
39
|
gem 'js_from_routes'
|
@@ -71,16 +75,37 @@ you can use a rake task instead:
|
|
71
75
|
bin/rake js_from_routes:generate
|
72
76
|
```
|
73
77
|
|
78
|
+
which can generate code such as:
|
79
|
+
|
80
|
+
```js
|
81
|
+
import { formatUrl } from '@helpers/UrlHelper'
|
82
|
+
import { request } from '@services/ApiService'
|
83
|
+
|
84
|
+
export default {
|
85
|
+
downloadPath: options =>
|
86
|
+
formatUrl('/video_clips/:id/download', options),
|
87
|
+
|
88
|
+
get: options =>
|
89
|
+
request('get', '/video_clips/:id', options),
|
90
|
+
|
91
|
+
update: options =>
|
92
|
+
request('patch', '/video_clips/:id', options),
|
93
|
+
}
|
94
|
+
```
|
95
|
+
|
74
96
|
#### 3. Use the generated code in your JS application
|
75
97
|
|
76
|
-
This can happen in many [different ways](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/app/javascript/Videos.vue#L10), but to illustrate using the example above
|
98
|
+
This can happen in many [different ways](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/app/javascript/Videos.vue#L10), but to illustrate using the example above, in combination with [`axios`](https://github.com/axios/axios) or `fetch`:
|
77
99
|
|
78
100
|
```js
|
79
101
|
import VideoClipsRequests from '@requests/VideoClipsRequests'
|
80
102
|
|
81
|
-
VideoClipsRequests.get({ id: 'oHg5SJYRHA0' }).then(
|
103
|
+
VideoClipsRequests.get({ id: 'oHg5SJYRHA0' }).then(data => { this.video = data })
|
82
104
|
|
83
|
-
const
|
105
|
+
const newVideo = { ...this.video, format: '.mp4' }
|
106
|
+
VideoClipsRequests.update(newVideo)
|
107
|
+
|
108
|
+
const path = VideoClipsRequests.downloadPath(newVideo)
|
84
109
|
```
|
85
110
|
|
86
111
|
Check the [examples](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/app/javascript/Videos.vue) for ideas on how to [use it](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/app/javascript/Videos.vue), and how you can [configure](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/config/webpack/aliases.js#L11) Webpack to your convenience.
|
@@ -97,7 +122,7 @@ The following [settings](https://github.com/ElMassimo/js_from_routes/blob/master
|
|
97
122
|
##### [`file_suffix`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L77), default: `Requests.js`
|
98
123
|
|
99
124
|
This suffix is added by default to all generated files. You can modify it to
|
100
|
-
if you prefer a different convention.
|
125
|
+
if you prefer a different convention, or if you use it to generate TypeScript.
|
101
126
|
|
102
127
|
##### [`helper_mappings`](https://github.com/ElMassimo/js_from_routes/blob/master/lib/js_from_routes/generator.rb#L80)
|
103
128
|
|
@@ -118,8 +143,10 @@ You will probably want to use a custom template, such as:
|
|
118
143
|
|
119
144
|
```ruby
|
120
145
|
# config/initializers/js_from_routes.rb
|
121
|
-
|
122
|
-
config
|
146
|
+
if Rails.env.development?
|
147
|
+
JsFromRoutes.config do |config|
|
148
|
+
config.template_path = Rails.root.join('app', 'views', 'custom_js_from_routes.js.erb')
|
149
|
+
end
|
123
150
|
end
|
124
151
|
```
|
125
152
|
|
@@ -144,9 +171,11 @@ request methods or path helpers ๐
|
|
144
171
|
|
145
172
|
### Take this idea ๐ก
|
146
173
|
|
147
|
-
|
174
|
+
While the original use cases intended to generate code that targes a custom `ApiService`,
|
175
|
+
it can be tweaked to generate TypeScript, [target jQuery](https://gist.github.com/ElMassimo/cab56e64e20ff797f3054b661a883646),
|
176
|
+
or use it only to generate [path helpers](https://github.com/ElMassimo/js_from_routes/blob/master/spec/support/sample_app/app/javascript/requests/UserPreferencesRequests.js#L11-L15).
|
177
|
+
|
178
|
+
There are plenty of other opportunities for automatic code generation, such as keeping
|
148
179
|
enums in sync between Ruby and JS.
|
149
180
|
|
150
181
|
Let me know if you come up with new or creative ways to use this technique ๐
|
151
|
-
|
152
|
-
|
@@ -0,0 +1,22 @@
|
|
1
|
+
//
|
2
|
+
// DO NOT MODIFY: This file was automatically generated by JsFromRoutes.
|
3
|
+
<%
|
4
|
+
if routes.any?(&:path_only?)
|
5
|
+
%>import { formatUrl } from '@/helpers/UrlHelper'<%
|
6
|
+
end
|
7
|
+
%><% if routes.any?(&:path_only?) && routes.any?(&:request_method?) %><%= "\n" %><% end %><%
|
8
|
+
if routes.any?(&:request_method?)
|
9
|
+
%>import { request } from '@/services/ApiService'<%
|
10
|
+
end
|
11
|
+
%>
|
12
|
+
|
13
|
+
export default {
|
14
|
+
<% routes.each_with_index do |route, index| %>
|
15
|
+
<% if index > 0 %><%= "\n" %><% end
|
16
|
+
%> <%= route.helper %>: options =>
|
17
|
+
<% if route.path_only?
|
18
|
+
%>formatUrl(<% else
|
19
|
+
%>request('<%= route.verb %>', <% end
|
20
|
+
%>'<%= route.path %>', options),
|
21
|
+
<% end %>
|
22
|
+
}
|
@@ -7,7 +7,7 @@ describe JsFromRoutes do
|
|
7
7
|
|
8
8
|
let(:output_dir) { Pathname.new File.expand_path('../support/generated', __dir__) }
|
9
9
|
let(:sample_dir) { Rails.root.join('app', 'javascript', 'requests') }
|
10
|
-
let(:different_template_path) { File.expand_path('../support/
|
10
|
+
let(:different_template_path) { File.expand_path('../support/jquery_template.js.erb', __dir__) }
|
11
11
|
let(:controllers_with_exported_routes) { %w[Comments UserPreferences VideoClips] }
|
12
12
|
|
13
13
|
def file_for(dir, name)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js_from_routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mรกximo Mussini
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -62,10 +62,12 @@ extensions: []
|
|
62
62
|
extra_rdoc_files:
|
63
63
|
- README.md
|
64
64
|
files:
|
65
|
+
- CHANGELOG.md
|
65
66
|
- README.md
|
66
67
|
- lib/js_from_routes.rb
|
67
68
|
- lib/js_from_routes/generator.rb
|
68
69
|
- lib/js_from_routes/railtie.rb
|
70
|
+
- lib/js_from_routes/template.js.erb
|
69
71
|
- lib/js_from_routes/version.rb
|
70
72
|
- spec/js_from_routes/js_from_routes_spec.rb
|
71
73
|
- spec/spec_helper.rb
|
@@ -96,7 +98,7 @@ homepage: https://github.com/ElMassimo/js_from_routes
|
|
96
98
|
licenses:
|
97
99
|
- MIT
|
98
100
|
metadata: {}
|
99
|
-
post_install_message:
|
101
|
+
post_install_message:
|
100
102
|
rdoc_options: []
|
101
103
|
require_paths:
|
102
104
|
- lib
|
@@ -111,8 +113,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
113
|
- !ruby/object:Gem::Version
|
112
114
|
version: '0'
|
113
115
|
requirements: []
|
114
|
-
rubygems_version: 3.1.
|
115
|
-
signing_key:
|
116
|
+
rubygems_version: 3.1.4
|
117
|
+
signing_key:
|
116
118
|
specification_version: 4
|
117
119
|
summary: Generate JS automatically from Rails routes.
|
118
120
|
test_files:
|