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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5194266884b788ff6ffd76ecf6408d7196fc8e4769381c6a63ef19ad49a30571
4
- data.tar.gz: 3fc7761af1ec667dd6f9cb742dd5131439bd42e36e801bc1f21f4d1bfd44e7b8
3
+ metadata.gz: 70fd767b097d502da92cc505b9f7936a675991dd690a8a1e5a960116f53a697f
4
+ data.tar.gz: 94f10933db96a86007a21859e0b566878174002c505fc62831f714bfec7ae4aa
5
5
  SHA512:
6
- metadata.gz: df569079481a998bec69b845602ee4f9ec35d6589fab7a91248608c9bcfeaa53a37ad2f25b35061b4be65ee427a6d188f98c5691526fa26deeca9d285c31d0d5
7
- data.tar.gz: acd55b6c961fa9372b311d96ff6ea9494213f8dbd83d1963f73b8b0b5b16d2f5a1134467ef4f5d3d4459de648e750219f76a7be4faa3950c41ccd916fb53c9bf
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
- ## License
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
- ## Todo
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
- - support all valid route names
40
- - relative_url_root
41
- - camelcase
39
+ ## License
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -25,7 +25,7 @@ namespace :webpacker do
25
25
 
26
26
  desc "Generate routes package"
27
27
  task generate: [:verify_install, :environment] do
28
- Webpacker::Routes.generate(Rails.application.routes)
28
+ Webpacker::Routes.generate(Rails.application)
29
29
  end
30
30
  end
31
31
  end
@@ -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(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))
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 default_url_options = #{default_url_options}
25
+ const #{default_url_options_var} = #{default_url_options}
23
26
  JAVASCRIPT
24
27
 
25
- route_set.named_routes.sort_by(&:first).each do |name, route|
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 #{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)
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!).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
@@ -1,5 +1,5 @@
1
1
  module Webpacker
2
2
  module Routes
3
- VERSION = '0.0.11'
3
+ VERSION = '0.1.0'
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.11
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-14 00:00:00.000000000 Z
11
+ date: 2019-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails