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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ca0f6f4f31f67057a1ae5f4d839b98564d323dfa
4
- data.tar.gz: 9966df2ec91cc5f7ec03dd6ca082a01a3c73f4d4
3
+ metadata.gz: aba31712b8f2349a61d9905e490c794f12aaf364
4
+ data.tar.gz: dd77a182d347b01babd5ff79711416e611b11d4e
5
5
  SHA512:
6
- metadata.gz: 08885b9714f50b48795aab6d79f5ce0e8e32de2ce1c245865a158c14011be5b38024a4d99f7024574b0c431f11907aee16fb5cfc73c34499cc9c845a939ed322
7
- data.tar.gz: 2143f80a2c1090ce16b4493ccb196a62d0c889930ff1ec5e6fe2ad6fb149b2491e7d4a716f3843f3b5e93285d2cf779a48843f56dc75fbbaeb7441722c48bc6b
6
+ metadata.gz: 29ce55b6233855a315b6ff410c26367fc633efc5d0fcb37222d6931537f1949a0713326176d10fa43e54b22852538cea1d57538ceea9c225ebe46fa3876c528a
7
+ data.tar.gz: 0ae2281dcca1288f0af67bdd80169bf46e6b4adfe33d6b35bfd2f786455ca82fafda0ae402ed44639c78c9a8bf690d4f7b618ae99ae6e36cac1c08723be7b03b
data/.rubocop.yml CHANGED
@@ -11,6 +11,9 @@ Metrics/BlockLength:
11
11
  Style/Documentation:
12
12
  Enabled: false
13
13
 
14
+ Style/IndentHeredoc:
15
+ Enabled: false
16
+
14
17
  Style/SymbolArray:
15
18
  Enabled: false
16
19
 
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
- export function article_path(params) { return '/articles/' + params.id + ''; }
41
- export function articles_path(params) { return '/articles'; }
42
- export function edit_article_path(params) { return '/articles/' + params.id + '/edit'; }
43
- export function new_article_path(params) { return '/articles/new'; }
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
- route_path.sub!(COMPARE_REGEXP, "' + params.#{$1} + '#{$2}") while route_path =~ COMPARE_REGEXP
41
- "export function #{route_name}_path(params) { return '#{route_path}'; }"
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
@@ -1,3 +1,3 @@
1
1
  module JSRailsRoutes
2
- VERSION = '0.4.0'.freeze
2
+ VERSION = '0.5.0'.freeze
3
3
  end
@@ -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")[1..-1]
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")[1..-1]
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")[1..-1]
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")[1..-1]
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.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-05 00:00:00.000000000 Z
11
+ date: 2017-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails