js_rails_routes 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/README.md +14 -4
- data/lib/js_rails_routes/generator.rb +19 -3
- data/lib/js_rails_routes/version.rb +1 -1
- data/spec/js_rails_routes/generator_spec.rb +4 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aba31712b8f2349a61d9905e490c794f12aaf364
|
4
|
+
data.tar.gz: dd77a182d347b01babd5ff79711416e611b11d4e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29ce55b6233855a315b6ff410c26367fc633efc5d0fcb37222d6931537f1949a0713326176d10fa43e54b22852538cea1d57538ceea9c225ebe46fa3876c528a
|
7
|
+
data.tar.gz: 0ae2281dcca1288f0af67bdd80169bf46e6b4adfe33d6b35bfd2f786455ca82fafda0ae402ed44639c78c9a8bf690d4f7b618ae99ae6e36cac1c08723be7b03b
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
|
5
5
|
This change log adheres to [keepachangelog.com](http://keepachangelog.com).
|
6
6
|
|
7
|
+
## [0.5.0] - 2017-04-08
|
8
|
+
### Added
|
9
|
+
- Support additional parameters
|
10
|
+
|
7
11
|
## [0.4.0] - 2017-04-05
|
8
12
|
### Added
|
9
13
|
- Add `include_names` and `exclude_names` option
|
@@ -31,6 +35,7 @@ This change log adheres to [keepachangelog.com](http://keepachangelog.com).
|
|
31
35
|
### Added
|
32
36
|
- Implement "js:rails:routes" task
|
33
37
|
|
38
|
+
[0.5.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.4.0...v0.5.0
|
34
39
|
[0.4.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.3.0...v0.4.0
|
35
40
|
[0.3.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.2.1...v0.3.0
|
36
41
|
[0.2.1]: https://github.com/yuku-t/js_rails_routes/compare/v0.2.0...v0.2.1
|
data/README.md
CHANGED
@@ -37,10 +37,20 @@ then `rake js:routes` generates "app/assets/javascripts/rails-routes.js" as:
|
|
37
37
|
|
38
38
|
```js
|
39
39
|
// Don't edit manually. `rake js:routes` generates this file.
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
function process(route, params, keys) {
|
41
|
+
var query = [];
|
42
|
+
for (var param in params) if (params.hasOwnProperty(param)) {
|
43
|
+
if (keys.indexOf(param) === -1) {
|
44
|
+
query.push(param + "=" + encodeURIComponent(params[param]));
|
45
|
+
}
|
46
|
+
}
|
47
|
+
return query.length ? route + "?" + query.join("&") : route;
|
48
|
+
}
|
49
|
+
|
50
|
+
export function article_path(params) { return process('/articles/' + params.id + '', params, ['id']); }
|
51
|
+
export function articles_path(params) { return process('/articles', params, []); }
|
52
|
+
export function edit_article_path(params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
|
53
|
+
export function new_article_path(params) { return process('/articles/new', params, []); }
|
44
54
|
```
|
45
55
|
|
46
56
|
## VS.
|
@@ -4,6 +4,18 @@ module JSRailsRoutes
|
|
4
4
|
class Generator
|
5
5
|
COMPARE_REGEXP = %r{:(.*?)(/|$)}
|
6
6
|
|
7
|
+
PROCESS_FUNC = <<-JAVASCRIPT.freeze
|
8
|
+
function process(route, params, keys) {
|
9
|
+
var query = [];
|
10
|
+
for (var param in params) if (params.hasOwnProperty(param)) {
|
11
|
+
if (keys.indexOf(param) === -1) {
|
12
|
+
query.push(param + "=" + encodeURIComponent(params[param]));
|
13
|
+
}
|
14
|
+
}
|
15
|
+
return query.length ? route + "?" + query.join("&") : route;
|
16
|
+
}
|
17
|
+
JAVASCRIPT
|
18
|
+
|
7
19
|
include Singleton
|
8
20
|
|
9
21
|
attr_accessor :include_paths, :exclude_paths, :include_names, :exclude_names, :path
|
@@ -18,7 +30,7 @@ module JSRailsRoutes
|
|
18
30
|
end
|
19
31
|
|
20
32
|
def generate(task)
|
21
|
-
lines = ["// Don't edit manually. `rake #{task}` generates this file."]
|
33
|
+
lines = ["// Don't edit manually. `rake #{task}` generates this file.", PROCESS_FUNC]
|
22
34
|
lines += routes.map do |route_name, route_path|
|
23
35
|
handle_route(route_name, route_path) if match?(route_name, route_path)
|
24
36
|
end.compact
|
@@ -37,8 +49,12 @@ module JSRailsRoutes
|
|
37
49
|
end
|
38
50
|
|
39
51
|
def handle_route(route_name, route_path)
|
40
|
-
|
41
|
-
|
52
|
+
keys = []
|
53
|
+
while route_path =~ COMPARE_REGEXP
|
54
|
+
keys.push("'#{$1}'")
|
55
|
+
route_path.sub!(COMPARE_REGEXP, "' + params.#{$1} + '#{$2}")
|
56
|
+
end
|
57
|
+
"export function #{route_name}_path(params) { return process('#{route_path}', params, [#{keys.join(',')}]); }"
|
42
58
|
end
|
43
59
|
|
44
60
|
def routes
|
@@ -30,7 +30,7 @@ RSpec.describe JSRailsRoutes::Generator do
|
|
30
30
|
|
31
31
|
it 'writes paths matching with the parameter' do
|
32
32
|
expect(generator).to receive(:write).with(a_kind_of(String)) do |arg|
|
33
|
-
paths = arg.split("\n")[
|
33
|
+
paths = arg.split("\n")[(2 + described_class::PROCESS_FUNC.split("\n").size)..-1]
|
34
34
|
expect(paths).not_to be_empty
|
35
35
|
expect(paths).to all(match(include_paths))
|
36
36
|
end
|
@@ -49,7 +49,7 @@ RSpec.describe JSRailsRoutes::Generator do
|
|
49
49
|
|
50
50
|
it 'writes paths not matching with the parameter' do
|
51
51
|
expect(generator).to receive(:write).with(a_kind_of(String)) do |arg|
|
52
|
-
paths = arg.split("\n")[
|
52
|
+
paths = arg.split("\n")[(2 + described_class::PROCESS_FUNC.split("\n").size)..-1]
|
53
53
|
expect(paths).not_to be_empty
|
54
54
|
paths.each do |path|
|
55
55
|
expect(path).to_not match(exclude_paths)
|
@@ -70,7 +70,7 @@ RSpec.describe JSRailsRoutes::Generator do
|
|
70
70
|
|
71
71
|
it 'writes paths matching with the parameter' do
|
72
72
|
expect(generator).to receive(:write).with(a_kind_of(String)) do |arg|
|
73
|
-
paths = arg.split("\n")[
|
73
|
+
paths = arg.split("\n")[(2 + described_class::PROCESS_FUNC.split("\n").size)..-1]
|
74
74
|
expect(paths).not_to be_empty
|
75
75
|
expect(paths).to all(match(include_names))
|
76
76
|
end
|
@@ -89,7 +89,7 @@ RSpec.describe JSRailsRoutes::Generator do
|
|
89
89
|
|
90
90
|
it 'writes paths not matching with the parameter' do
|
91
91
|
expect(generator).to receive(:write).with(a_kind_of(String)) do |arg|
|
92
|
-
paths = arg.split("\n")[
|
92
|
+
paths = arg.split("\n")[(2 + described_class::PROCESS_FUNC.split("\n").size)..-1]
|
93
93
|
expect(paths).not_to be_empty
|
94
94
|
paths.each do |path|
|
95
95
|
expect(path).to_not match(exclude_names)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js_rails_routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuku Takahashi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|