webpacker-routes 0.0.11 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -6
- data/lib/tasks/webpacker/routes_tasks.rake +1 -1
- data/lib/webpacker/routes.rb +16 -9
- data/lib/webpacker/routes/railtie.rb +7 -1
- data/lib/webpacker/routes/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70fd767b097d502da92cc505b9f7936a675991dd690a8a1e5a960116f53a697f
|
4
|
+
data.tar.gz: 94f10933db96a86007a21859e0b566878174002c505fc62831f714bfec7ae4aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80c6521e15d0d474fb04a2d3927de21771410f7d3681bc24cca4cd1b8f195d7bd22cd15bcb4ecdc2c996446bdb7148924b6034aef28656fd318e1580ef708d64
|
7
|
+
data.tar.gz: 73ce8b39b57f12a296eef660af0e5ac0e5e17a79c03b144fccb4fd15c965119ef97cbb58601d83e1e70a070e2af9bf27ae58e8ce926d28944d76774797b32cc4
|
data/README.md
CHANGED
@@ -31,11 +31,10 @@ To generate routes manually, run:
|
|
31
31
|
$ bundle exec rails webpacker:routes:generate
|
32
32
|
```
|
33
33
|
|
34
|
-
|
35
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
34
|
+
### Options
|
36
35
|
|
37
|
-
|
36
|
+
- `config.webpacker.routes.default_url_options` - defaults used for generating urls. These are merged with `Rails.application.default_url_options`. Default: `{}`
|
37
|
+
- `config.webpacker.routes.camel_case` - convert route names to camel case. Default: `false`
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
- camelcase
|
39
|
+
## License
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/webpacker/routes.rb
CHANGED
@@ -4,7 +4,7 @@ require "webpacker/routes/railtie"
|
|
4
4
|
module Webpacker
|
5
5
|
class Configuration
|
6
6
|
def routes_path
|
7
|
-
source_path.join('routes')
|
7
|
+
fetch(:routes_path) || source_path.join('routes')
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
@@ -13,26 +13,33 @@ module Webpacker
|
|
13
13
|
IGNORED_OPTIONS = %i[controller action]
|
14
14
|
|
15
15
|
class << self
|
16
|
-
def generate(
|
17
|
-
|
18
|
-
|
16
|
+
def generate(app)
|
17
|
+
config = app.config.webpacker.routes
|
18
|
+
var_name = -> (name) { config.camel_case ? name.camelize(:lower) : name }
|
19
|
+
default_url_options = js(app.default_url_options.merge(config.default_url_options).except(*IGNORED_OPTIONS))
|
20
|
+
default_url_options_var = var_name.call('default_url_options')
|
19
21
|
|
22
|
+
File.atomic_write(Webpacker.config.routes_path.join('index.js')) do |file|
|
20
23
|
file.write(<<-JAVASCRIPT.strip_heredoc)
|
21
24
|
import { urlFor, pathFor } from 'webpacker-routes'
|
22
|
-
const
|
25
|
+
const #{default_url_options_var} = #{default_url_options}
|
23
26
|
JAVASCRIPT
|
24
27
|
|
25
|
-
|
28
|
+
app.routes.named_routes.sort_by(&:first).each do |name, route|
|
26
29
|
raise `Invalid route name for javascript: ${name}` unless JAVASCRIPT_VARIABLE_NAME_REGEX =~ name
|
27
30
|
|
28
31
|
spec = js(route.path.spec.to_s)
|
29
32
|
segment_keys = js(route.segment_keys.uniq)
|
30
33
|
options = js(route.defaults.except(*IGNORED_OPTIONS))
|
31
34
|
|
35
|
+
spec_var = var_name.call("#{name}_spec")
|
36
|
+
url_var = var_name.call("#{name}_url")
|
37
|
+
path_var = var_name.call("#{name}_path")
|
38
|
+
|
32
39
|
file.write(<<-JAVASCRIPT.strip_heredoc)
|
33
|
-
const #{
|
34
|
-
export const #{
|
35
|
-
export const #{
|
40
|
+
const #{spec_var} = [#{spec}, #{segment_keys}, { ...#{default_url_options_var}, ...#{options} }]
|
41
|
+
export const #{url_var} = (...args) => urlFor(#{spec_var}, ...args)
|
42
|
+
export const #{path_var} = (...args) => pathFor(#{spec_var}, ...args)
|
36
43
|
JAVASCRIPT
|
37
44
|
end
|
38
45
|
end
|
@@ -1,8 +1,14 @@
|
|
1
|
+
require 'webpacker/railtie'
|
2
|
+
|
1
3
|
module Webpacker
|
2
4
|
module Routes
|
3
5
|
class Engine < ::Rails::Engine
|
6
|
+
config.webpacker.routes = ActiveSupport::OrderedOptions.new
|
7
|
+
config.webpacker.routes.default_url_options = {}
|
8
|
+
config.webpacker.routes.camel_case = false
|
9
|
+
|
4
10
|
config.after_initialize do |app|
|
5
|
-
generate = -> { Webpacker::Routes.generate(app.tap(&:reload_routes!)
|
11
|
+
generate = -> { Webpacker::Routes.generate(app.tap(&:reload_routes!)) }
|
6
12
|
if Rails::VERSION::MAJOR >= 5
|
7
13
|
app.reloader.to_run(&generate)
|
8
14
|
else
|
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
|
4
|
+
version: 0.1.0
|
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-
|
11
|
+
date: 2019-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|