webpacker-routes 0.0.10 → 0.0.11

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
  SHA256:
3
- metadata.gz: 347e51b5067401b0315a50953592a477b54ab588ba6405c5b9a281f19bb5abbb
4
- data.tar.gz: f9aaa63022d82a3758bb3416ffc8888c2d14ee6f1fffd1e30ffcf0fec1bc1719
3
+ metadata.gz: 5194266884b788ff6ffd76ecf6408d7196fc8e4769381c6a63ef19ad49a30571
4
+ data.tar.gz: 3fc7761af1ec667dd6f9cb742dd5131439bd42e36e801bc1f21f4d1bfd44e7b8
5
5
  SHA512:
6
- metadata.gz: 853dbe59e91370cc7d699d34d00de2803eb32f9130c72d65e77f772d37b3bd7d1712442c3653fa114b3241bd06cc272de188a50470d2cf7addc3ea5dbef58c6c
7
- data.tar.gz: ec9ce0004b11fd5af8f0e1cc5fa9b57fb40cef8bee4a489b1a3b4416019836f13c148a9ee6db2003f1570240d6b2be4ee1e8e13df443dd195dc8c86d8ce33b7f
6
+ metadata.gz: df569079481a998bec69b845602ee4f9ec35d6589fab7a91248608c9bcfeaa53a37ad2f25b35061b4be65ee427a6d188f98c5691526fa26deeca9d285c31d0d5
7
+ data.tar.gz: acd55b6c961fa9372b311d96ff6ea9494213f8dbd83d1963f73b8b0b5b16d2f5a1134467ef4f5d3d4459de648e750219f76a7be4faa3950c41ccd916fb53c9bf
data/README.md CHANGED
@@ -36,7 +36,6 @@ The gem is available as open source under the terms of the [MIT License](https:/
36
36
 
37
37
  ## Todo
38
38
 
39
- - default_url_options (especially host for urls)
40
39
  - support all valid route names
41
40
  - relative_url_root
42
41
  - camelcase
@@ -10,27 +10,39 @@ module Webpacker
10
10
 
11
11
  module Routes
12
12
  JAVASCRIPT_VARIABLE_NAME_REGEX = /\A[_$a-z][_$a-z0-9]*\z/i
13
+ IGNORED_OPTIONS = %i[controller action]
13
14
 
14
- def self.generate(route_set)
15
- File.atomic_write(Webpacker.config.routes_path.join('index.js')) do |file|
16
- file.write(<<-JAVASCRIPT.strip_heredoc)
17
- import { urlFor, pathFor } from 'webpacker-routes'
18
- JAVASCRIPT
19
-
20
- route_set.named_routes.sort_by(&:first).each do |name, route|
21
- raise `Invalid route name for javascript: ${name}` unless JAVASCRIPT_VARIABLE_NAME_REGEX =~ name
22
-
23
- spec = route.path.spec.to_s.to_json
24
- segment_keys = route.segment_keys.uniq.to_json
25
- defaults = route.defaults.except(:controller, :action).to_json
15
+ class << self
16
+ def generate(route_set)
17
+ File.atomic_write(Webpacker.config.routes_path.join('index.js')) do |file|
18
+ default_url_options = js(route_set.default_url_options.except(*IGNORED_OPTIONS))
26
19
 
27
20
  file.write(<<-JAVASCRIPT.strip_heredoc)
28
- const #{name}_spec = [#{spec}, #{segment_keys}, #{defaults}]
29
- export const #{name}_url = (...args) => urlFor(#{name}_spec, ...args)
30
- export const #{name}_path = (...args) => pathFor(#{name}_spec, ...args)
21
+ import { urlFor, pathFor } from 'webpacker-routes'
22
+ const default_url_options = #{default_url_options}
31
23
  JAVASCRIPT
24
+
25
+ route_set.named_routes.sort_by(&:first).each do |name, route|
26
+ raise `Invalid route name for javascript: ${name}` unless JAVASCRIPT_VARIABLE_NAME_REGEX =~ name
27
+
28
+ spec = js(route.path.spec.to_s)
29
+ segment_keys = js(route.segment_keys.uniq)
30
+ options = js(route.defaults.except(*IGNORED_OPTIONS))
31
+
32
+ file.write(<<-JAVASCRIPT.strip_heredoc)
33
+ const #{name}_spec = [#{spec}, #{segment_keys}, { ...default_url_options, ...#{options} }]
34
+ export const #{name}_url = (...args) => urlFor(#{name}_spec, ...args)
35
+ export const #{name}_path = (...args) => pathFor(#{name}_spec, ...args)
36
+ JAVASCRIPT
37
+ end
32
38
  end
33
39
  end
40
+
41
+ private
42
+
43
+ def js(obj)
44
+ ERB::Util.json_escape(obj.to_json)
45
+ end
34
46
  end
35
47
  end
36
48
  end
@@ -1,5 +1,5 @@
1
1
  module Webpacker
2
2
  module Routes
3
- VERSION = '0.0.10'
3
+ VERSION = '0.0.11'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webpacker-routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Harsha
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-13 00:00:00.000000000 Z
11
+ date: 2019-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails