js_rails_routes 0.7.1 → 0.8.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
- SHA1:
3
- metadata.gz: 1768a1c21e72db96adfed68e4e68cf5ba15a2ad2
4
- data.tar.gz: cecbb2cbd58e60f4e0449dcd12b8c903d1aa65bc
2
+ SHA256:
3
+ metadata.gz: a39fd7ffc8b9d7c33679acfc90ac9df033880e9795ab6c3a312c038a8ca22313
4
+ data.tar.gz: 30defa0fff6b4a834106f109f9f07e2122c74ed69a6137b64cf0362968dfd50d
5
5
  SHA512:
6
- metadata.gz: d599a65ae0bb2396c780f13c9cfee5a9431fd8c7e4c5ff1b5d6319c0f8650ea8db8db20b504f22e67c03357181b44d5c9a9a3aefe5cc56c5009120ea691a0fde
7
- data.tar.gz: 06c1288cbf4107240254dbef471fca09b1d81a60301cf6c0a5a2b6ccb5a07eecdf3d731ab3628b3c265c930b623aefc951e887f71b2b936ccc0d410e784de272
6
+ metadata.gz: 446e25f72d627356b9b592ef2cc368ac5f6d16f02f1b6274f66d11ef35471307df3ab29b16a2a53e2cc383a18054b6ba39ec7d19cf1c5cfd984e4d9c7b424033
7
+ data.tar.gz: 12e2923b66465a669d2ab162a56ff041d573c4a7d605ba7e5bc0931ca22fb0b8b3b661263872d833ff623a64cb8bcb1b32cb89725d1a9ef5f34e27a55d49bcfe
@@ -6,9 +6,17 @@ This change log adheres to [keepachangelog.com](http://keepachangelog.com).
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.8.0] - 2018-08-21
10
+ ### Added
11
+ - Support TypeScript
12
+ - Add `route_filter` and `route_set_filter` options
13
+
9
14
  ## [0.7.1] - 2018-07-26
10
15
  - Refactor whole code base
11
16
 
17
+ ### Fixed
18
+ - Enable to math engine name in case-sensitive way.
19
+
12
20
  ## [0.7.0] - 2018-07-11
13
21
  ### Added
14
22
  - Support `camelize` option
@@ -29,7 +37,7 @@ This change log adheres to [keepachangelog.com](http://keepachangelog.com).
29
37
 
30
38
  ## [0.4.0] - 2017-04-05
31
39
  ### Added
32
- - Add `include_names` and `exclude_names` option
40
+ - Add `include_names` and `exclude_names` options
33
41
 
34
42
  ### Changed
35
43
  - Rename `includes` and `excludes` as `include_paths` and `exclude_paths` respectively
@@ -54,7 +62,8 @@ This change log adheres to [keepachangelog.com](http://keepachangelog.com).
54
62
  ### Added
55
63
  - Implement "js:rails:routes" task
56
64
 
57
- [Unreleased]: https://github.com/yuku-t/js_rails_routes/compare/v0.7.1...HEAD
65
+ [Unreleased]: https://github.com/yuku-t/js_rails_routes/compare/v0.8.0...HEAD
66
+ [0.8.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.7.1...v0.8.0
58
67
  [0.7.1]: https://github.com/yuku-t/js_rails_routes/compare/v0.7.0...v0.7.1
59
68
  [0.7.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.6.0...v0.7.0
60
69
  [0.6.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.5.0...v0.6.0
data/README.md CHANGED
@@ -75,15 +75,18 @@ rake js:routes
75
75
 
76
76
  JSRailsRoutes supports several parameters:
77
77
 
78
- Name | Type | Description | Default
79
- ------------------|-----------|---------------------------------------------------------------------------------------|----------------------------------------
80
- `include_paths` | `Regexp` | Paths match to the regexp are included | `/.*/`
81
- `exclude_paths` | `Regexp` | Paths match to the regexp are excluded | `/^$/`
82
- `include_names` | `Regexp` | Names match to the regexp are included | `/.*/`
83
- `exclude_names` | `Regexp` | Names match to the regexp are excluded | `/^$/`
84
- `exclude_engines` | `Regexp` | Rails engines match to the regexp are excluded | `/^$/`
85
- `output_dir` | `String` | Output JS file into the specified directory | `Rails.root.join("app", "assets", "javascripts")`
86
- `camelize` | `Symbol` | Output JS file with chosen camelcase type it also avaliable for `:lower` and `:upper` | `nil`
78
+ Name | Type | Description | Default
79
+ -------------------|-----------|---------------------------------------------------------------------------------------|----------------------------------------
80
+ `include_paths` | `Regexp` | Paths match to the regexp are included | `/.*/`
81
+ `exclude_paths` | `Regexp` | Paths match to the regexp are excluded | `/^$/`
82
+ `include_names` | `Regexp` | Names match to the regexp are included | `/.*/`
83
+ `exclude_names` | `Regexp` | Names match to the regexp are excluded | `/^$/`
84
+ `exclude_engines` | `Regexp` | Rails engines match to the regexp are excluded | `/^$/`
85
+ `output_dir` | `String` | Output JS file into the specified directory | `Rails.root.join("app", "assets", "javascripts")`
86
+ `camelize` | `Symbol` | Output JS file with chosen camelcase type it also avaliable for `:lower` and `:upper` | `nil`
87
+ `target` | `String` | Target type. `"js"` or `"ts"` | `"js"`
88
+ `route_filter` | `Proc` | Fully customizable filter on `JSRails::Route` | `->(route) { true }`
89
+ `route_set_filter` | `Proc` | Fully customizable filter on `JSRails::RouteSet` | `->(route_set) { true }`
87
90
 
88
91
  You can configure via `JSRailsRoutes.configure`.
89
92
 
@@ -5,6 +5,7 @@ require 'js_rails_routes/configuration'
5
5
  require 'js_rails_routes/generator'
6
6
  require 'js_rails_routes/version'
7
7
  require 'js_rails_routes/language/javascript'
8
+ require 'js_rails_routes/language/typescript'
8
9
 
9
10
  module JSRailsRoutes
10
11
  PARAM_REGEXP = %r{:(.*?)(/|$)}
@@ -24,8 +25,8 @@ module JSRailsRoutes
24
25
  end
25
26
 
26
27
  # @param task [String]
27
- def generate_javascript(task)
28
- builder = Builder.new(Language::JavaScript.new)
28
+ def generate(task)
29
+ builder = Builder.new(JSRailsRoutes.language)
29
30
  Generator.new(builder).generate(task)
30
31
  end
31
32
 
@@ -44,4 +45,16 @@ module JSRailsRoutes
44
45
  @sandbox = nil
45
46
  end
46
47
  end
48
+
49
+ # @return [JSRailsRoutes::Language::Base]
50
+ def language
51
+ case config.target
52
+ when 'js'
53
+ Language::JavaScript.new
54
+ when 'ts'
55
+ Language::TypeScript.new
56
+ else
57
+ raise NotImplementedError, config.target
58
+ end
59
+ end
47
60
  end
@@ -4,6 +4,13 @@ require 'js_rails_routes/route_set'
4
4
 
5
5
  module JSRailsRoutes
6
6
  class Builder
7
+ Artifact = Struct.new(:engine_name, :ext, :body) do
8
+ # @return [String]
9
+ def file_name
10
+ "#{engine_name.gsub('::Engine', '').underscore.tr('/', '-')}-routes.#{ext}"
11
+ end
12
+ end
13
+
7
14
  # @return [Array<JSRailsRoutes::RouteSet>]
8
15
  attr_reader :route_set_list
9
16
 
@@ -17,10 +24,10 @@ module JSRailsRoutes
17
24
  @route_set_list = route_set_list
18
25
  end
19
26
 
20
- # @return [Hash{String => String}]
27
+ # @return [Array<Artifact>]
21
28
  def build
22
- route_set_list.each_with_object({}) do |route_set, memo|
23
- memo[route_set.name] = language.handle_route_set(route_set)
29
+ route_set_list.map do |route_set|
30
+ Artifact.new(route_set.name, language.ext, language.handle_route_set(route_set))
24
31
  end
25
32
  end
26
33
  end
@@ -8,7 +8,10 @@ module JSRailsRoutes
8
8
  :exclude_names,
9
9
  :exclude_engines,
10
10
  :output_dir,
11
- :camelize
11
+ :camelize,
12
+ :target,
13
+ :route_filter,
14
+ :route_set_filter
12
15
 
13
16
  def initialize
14
17
  self.include_paths = /.*/
@@ -18,6 +21,9 @@ module JSRailsRoutes
18
21
  self.exclude_engines = /^$/
19
22
  self.camelize = nil
20
23
  self.output_dir = Rails.root.join('app', 'assets', 'javascripts')
24
+ self.target = 'js'
25
+ self.route_filter = ->(_route) { true }
26
+ self.route_set_filter = ->(_route_set) { true }
21
27
  end
22
28
 
23
29
  # @param env [Hash{String=>String}]
@@ -27,6 +33,7 @@ module JSRailsRoutes
27
33
  end
28
34
  self.output_dir = env['output_dir'] if env['output_dir']
29
35
  self.camelize = env['camelize'].presence.to_sym if env['camelize']
36
+ self.target = env['target'] if env['target']
30
37
  end
31
38
  end
32
39
  end
@@ -15,9 +15,9 @@ module JSRailsRoutes
15
15
  # @param task [String]
16
16
  # @return [Hash{String => String}]
17
17
  def generate(task)
18
- builder.build.each do |name, body|
19
- file_name = File.join(config.output_dir, "#{convert(name)}-routes.js")
20
- file_body = "// Don't edit manually. `rake #{task}` generates this file.\n#{body}"
18
+ builder.build.each do |artifact|
19
+ file_name = File.join(config.output_dir, artifact.file_name)
20
+ file_body = "// Don't edit manually. `rake #{task}` generates this file.\n#{artifact.body}"
21
21
  writable.write(file_name, file_body)
22
22
  end
23
23
  end
@@ -30,11 +30,5 @@ module JSRailsRoutes
30
30
  def config
31
31
  JSRailsRoutes.config
32
32
  end
33
-
34
- # @param engine_name [String]
35
- # @return [String]
36
- def convert(engine_name)
37
- engine_name.gsub('::Engine', '').underscore.tr('/', '-')
38
- end
39
33
  end
40
34
  end
@@ -9,6 +9,11 @@ module JSRailsRoutes
9
9
  raise NotImplementedError
10
10
  end
11
11
 
12
+ # @return [String]
13
+ def ext
14
+ raise NotImplementedError
15
+ end
16
+
12
17
  private
13
18
 
14
19
  # @return [JSRailsRoutes::Configuration]
@@ -20,7 +20,7 @@ module JSRailsRoutes
20
20
 
21
21
  # @note Implementation for {JSRailsRoutes::Language::Base#generate}
22
22
  def handle_route_set(routes)
23
- routes.each_with_object([PROCESS_FUNC]) do |route, lines|
23
+ routes.each_with_object([self.class::PROCESS_FUNC]) do |route, lines|
24
24
  lines.push(handle_route(route))
25
25
  end.join("\n") + "\n"
26
26
  end
@@ -33,6 +33,11 @@ module JSRailsRoutes
33
33
  "export function #{name}(params) { return process('#{path}', params, [#{keys.join(',')}]); }"
34
34
  end
35
35
 
36
+ # @note Implementation for {JSRailsRoutes::Language::Base#ext}
37
+ def ext
38
+ 'js'
39
+ end
40
+
36
41
  private
37
42
 
38
43
  # @param route_path [String]
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'js_rails_routes/route'
4
+ require 'js_rails_routes/language/javascript'
5
+
6
+ module JSRailsRoutes
7
+ module Language
8
+ class TypeScript < JavaScript
9
+ PROCESS_FUNC = <<~TYPESCRIPT
10
+ type Params = Record<string, string | number>
11
+ function process(route: string, params: Params, keys: string[]): string {
12
+ var query = [];
13
+ for (var param in params) if (params.hasOwnProperty(param)) {
14
+ if (keys.indexOf(param) === -1) {
15
+ query.push(param + "=" + encodeURIComponent(params[param].toString()));
16
+ }
17
+ }
18
+ return query.length ? route + "?" + query.join("&") : route;
19
+ }
20
+ TYPESCRIPT
21
+
22
+ # @param route [JSRailsRoutes::Route]
23
+ # @return [String]
24
+ def handle_route(route)
25
+ path, keys = parse(route.path)
26
+ name = function_name(route.name)
27
+ "export function #{name}(params: Params) { return process('#{path}', params, [#{keys.join(',')}]); }"
28
+ end
29
+
30
+ # @note Implementation for {JSRailsRoutes::Language::Base#ext}
31
+ def ext
32
+ 'ts'
33
+ end
34
+ end
35
+ end
36
+ end
@@ -9,8 +9,12 @@ module JSRailsRoutes
9
9
  # @return [String]
10
10
  attr_reader :path
11
11
 
12
+ # @return [ActionDispatch::Journey::Route]
13
+ attr_reader :route
14
+
12
15
  # @param route [ActionDispatch::Journey::Route]
13
16
  def initialize(route)
17
+ @route = route
14
18
  @name = route.name
15
19
  @path = route.path.spec.to_s.split('(').first
16
20
  end
@@ -21,7 +25,7 @@ module JSRailsRoutes
21
25
  return false if config.exclude_paths =~ path
22
26
  return false if config.include_names !~ name
23
27
  return false if config.exclude_names =~ name
24
- true
28
+ config.route_filter.call(self)
25
29
  end
26
30
 
27
31
  private
@@ -25,6 +25,9 @@ module JSRailsRoutes
25
25
  # @return [String]
26
26
  attr_reader :name
27
27
 
28
+ # @return [ActionDispatch::Routing::RouteSet]
29
+ attr_reader :route_set
30
+
28
31
  # @return [Array<JSRailsRoutes::Route>]
29
32
  attr_reader :routes
30
33
 
@@ -32,6 +35,7 @@ module JSRailsRoutes
32
35
  # @param routes [ActionDispatch::Routing::RouteSet]
33
36
  def initialize(name, routes)
34
37
  @name = name
38
+ @route_set = route_set
35
39
  @routes = routes.routes
36
40
  .select(&:name)
37
41
  .map { |route| Route.new(route) }
@@ -40,7 +44,7 @@ module JSRailsRoutes
40
44
 
41
45
  # @return [Boolean]
42
46
  def match?
43
- name !~ config.exclude_engines && routes.present?
47
+ name !~ config.exclude_engines && routes.present? && config.route_set_filter.call(self)
44
48
  end
45
49
 
46
50
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSRailsRoutes
4
- VERSION = '0.7.1'
4
+ VERSION = '0.8.0'
5
5
  end
@@ -4,7 +4,7 @@ desc 'Generate a ES6 module that contains Rails routes'
4
4
  namespace :js do
5
5
  task routes: :environment do |task|
6
6
  JSRailsRoutes.config.configure_with_env_vars
7
- JSRailsRoutes.generate_javascript(task)
7
+ JSRailsRoutes.generate(task)
8
8
  puts "Routes saved into #{JSRailsRoutes.config.output_dir}."
9
9
  end
10
10
  end
@@ -5,7 +5,7 @@ RSpec.describe JSRailsRoutes::Builder do
5
5
 
6
6
  include_context 'run in a sandbox'
7
7
 
8
- let(:language) { instance_double('JSRailsRoutes::Language::Base', handle_route_set: body) }
8
+ let(:language) { instance_double('JSRailsRoutes::Language::Base', handle_route_set: body, ext: %w[js ts].sample) }
9
9
  let(:body) { 'hello' }
10
10
  let(:route_set_list) { [rails_route_set, engine_route_set] }
11
11
 
@@ -30,8 +30,11 @@ RSpec.describe JSRailsRoutes::Builder do
30
30
  describe '#build' do
31
31
  subject { builder.build }
32
32
 
33
- it 'returns a hash between engine name and its content' do
34
- is_expected.to match(rails_route_set.name => body, engine_route_set.name => body)
33
+ it 'returns an array of artifacts' do
34
+ is_expected.to contain_exactly(
35
+ an_object_having_attributes(engine_name: rails_route_set.name, body: body),
36
+ an_object_having_attributes(engine_name: engine_route_set.name, body: body)
37
+ )
35
38
  expect(language).to have_received(:handle_route_set).twice
36
39
  end
37
40
  end
@@ -7,7 +7,12 @@ RSpec.describe JSRailsRoutes::Generator do
7
7
 
8
8
  let(:writable) { spy('writable') }
9
9
  let(:builder) { double('builder', build: result) }
10
- let(:result) { Hash['Rails' => 'rails body', 'Admin::Engine' => 'admin body'] }
10
+ let(:result) do
11
+ [
12
+ JSRailsRoutes::Builder::Artifact.new('Rails', 'js', 'rails body'),
13
+ JSRailsRoutes::Builder::Artifact.new('Admin::Engine', 'js', 'admin body')
14
+ ]
15
+ end
11
16
 
12
17
  describe '#generate' do
13
18
  subject { generator.generate(task) }
@@ -8,4 +8,10 @@ RSpec.describe JSRailsRoutes::Language::Base do
8
8
 
9
9
  it { expect { subject }.to raise_error(NotImplementedError) }
10
10
  end
11
+
12
+ describe '#ext' do
13
+ subject { language.ext }
14
+
15
+ it { expect { subject }.to raise_error(NotImplementedError) }
16
+ end
11
17
  end
@@ -147,4 +147,10 @@ RSpec.describe JSRailsRoutes::Language::JavaScript do
147
147
  end
148
148
  end
149
149
  end
150
+
151
+ describe '#ext' do
152
+ subject { language.ext }
153
+
154
+ it { is_expected.to eq 'js' }
155
+ end
150
156
  end
@@ -0,0 +1,157 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe JSRailsRoutes::Language::TypeScript do
4
+ subject(:language) { described_class.new }
5
+
6
+ include_context 'run in a sandbox'
7
+
8
+ describe '::PROCESS_FUNC' do
9
+ subject { described_class::PROCESS_FUNC }
10
+
11
+ it 'returns a typescript function' do
12
+ is_expected.to eq <<~TYPESCRIPT
13
+ type Params = Record<string, string | number>
14
+ function process(route: string, params: Params, keys: string[]): string {
15
+ var query = [];
16
+ for (var param in params) if (params.hasOwnProperty(param)) {
17
+ if (keys.indexOf(param) === -1) {
18
+ query.push(param + "=" + encodeURIComponent(params[param].toString()));
19
+ }
20
+ }
21
+ return query.length ? route + "?" + query.join("&") : route;
22
+ }
23
+ TYPESCRIPT
24
+ end
25
+ end
26
+
27
+ describe '#handle_route_set' do
28
+ subject { language.handle_route_set(route_set) }
29
+
30
+ let(:route_set) do
31
+ rails_route_set = ActionDispatch::Routing::RouteSet.new.tap do |routes|
32
+ routes.draw do
33
+ resources :articles
34
+ end
35
+ end
36
+ JSRailsRoutes::RouteSet.new('Rails', rails_route_set)
37
+ end
38
+
39
+ context 'without camelize option' do
40
+ it 'returns a typescript with snake_case functions' do
41
+ is_expected.to eq <<~TYPESCRIPT
42
+ #{described_class::PROCESS_FUNC}
43
+ export function articles_path(params: Params) { return process('/articles', params, []); }
44
+ export function new_article_path(params: Params) { return process('/articles/new', params, []); }
45
+ export function edit_article_path(params: Params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
46
+ export function article_path(params: Params) { return process('/articles/' + params.id + '', params, ['id']); }
47
+ TYPESCRIPT
48
+ end
49
+ end
50
+
51
+ context 'with camelize = :lower option' do
52
+ before do
53
+ JSRailsRoutes.configure do |c|
54
+ c.camelize = :lower
55
+ end
56
+ end
57
+
58
+ it 'returns a javascript with lowerCamelCase functions' do
59
+ is_expected.to eq <<~TYPESCRIPT
60
+ #{described_class::PROCESS_FUNC}
61
+ export function articlesPath(params: Params) { return process('/articles', params, []); }
62
+ export function newArticlePath(params: Params) { return process('/articles/new', params, []); }
63
+ export function editArticlePath(params: Params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
64
+ export function articlePath(params: Params) { return process('/articles/' + params.id + '', params, ['id']); }
65
+ TYPESCRIPT
66
+ end
67
+ end
68
+
69
+ context 'with camelize = :upper option' do
70
+ before do
71
+ JSRailsRoutes.configure do |c|
72
+ c.camelize = :upper
73
+ end
74
+ end
75
+
76
+ it 'returns a javascript with UpperCamelCase functions' do
77
+ is_expected.to eq <<~TYPESCRIPT
78
+ #{described_class::PROCESS_FUNC}
79
+ export function ArticlesPath(params: Params) { return process('/articles', params, []); }
80
+ export function NewArticlePath(params: Params) { return process('/articles/new', params, []); }
81
+ export function EditArticlePath(params: Params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
82
+ export function ArticlePath(params: Params) { return process('/articles/' + params.id + '', params, ['id']); }
83
+ TYPESCRIPT
84
+ end
85
+ end
86
+
87
+ context 'with include_paths option' do
88
+ before do
89
+ JSRailsRoutes.configure do |c|
90
+ c.include_paths = /new/
91
+ end
92
+ end
93
+
94
+ it 'returns a javascript matching to the regexp' do
95
+ is_expected.to eq <<~TYPESCRIPT
96
+ #{described_class::PROCESS_FUNC}
97
+ export function new_article_path(params: Params) { return process('/articles/new', params, []); }
98
+ TYPESCRIPT
99
+ end
100
+ end
101
+
102
+ context 'with exclude_paths option' do
103
+ before do
104
+ JSRailsRoutes.configure do |c|
105
+ c.exclude_paths = /new/
106
+ end
107
+ end
108
+
109
+ it 'returns a javascript not matching to the regexp' do
110
+ is_expected.to eq <<~TYPESCRIPT
111
+ #{described_class::PROCESS_FUNC}
112
+ export function articles_path(params: Params) { return process('/articles', params, []); }
113
+ export function edit_article_path(params: Params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
114
+ export function article_path(params: Params) { return process('/articles/' + params.id + '', params, ['id']); }
115
+ TYPESCRIPT
116
+ end
117
+ end
118
+
119
+ context 'with include_names option' do
120
+ before do
121
+ JSRailsRoutes.configure do |c|
122
+ c.include_names = /new/
123
+ end
124
+ end
125
+
126
+ it 'returns a javascript matching to the regexp' do
127
+ is_expected.to eq <<~TYPESCRIPT
128
+ #{described_class::PROCESS_FUNC}
129
+ export function new_article_path(params: Params) { return process('/articles/new', params, []); }
130
+ TYPESCRIPT
131
+ end
132
+ end
133
+
134
+ context 'with exclude_names option' do
135
+ before do
136
+ JSRailsRoutes.configure do |c|
137
+ c.exclude_names = /new/
138
+ end
139
+ end
140
+
141
+ it 'returns a javascript not matching to the regexp' do
142
+ is_expected.to eq <<~TYPESCRIPT
143
+ #{described_class::PROCESS_FUNC}
144
+ export function articles_path(params: Params) { return process('/articles', params, []); }
145
+ export function edit_article_path(params: Params) { return process('/articles/' + params.id + '/edit', params, ['id']); }
146
+ export function article_path(params: Params) { return process('/articles/' + params.id + '', params, ['id']); }
147
+ TYPESCRIPT
148
+ end
149
+ end
150
+ end
151
+
152
+ describe '#ext' do
153
+ subject { language.ext }
154
+
155
+ it { is_expected.to eq 'ts' }
156
+ end
157
+ end
@@ -82,5 +82,25 @@ RSpec.describe JSRailsRoutes::RouteSet do
82
82
 
83
83
  it { is_expected.to be false }
84
84
  end
85
+
86
+ context 'when route_set_filter option is specified' do
87
+ before do
88
+ JSRailsRoutes.configure do |c|
89
+ c.route_set_filter = ->(_route) { result }
90
+ end
91
+ end
92
+
93
+ context 'and it returns true' do
94
+ let(:result) { true }
95
+
96
+ it { is_expected.to be true }
97
+ end
98
+
99
+ context 'and it returns false' do
100
+ let(:result) { false }
101
+
102
+ it { is_expected.to be false }
103
+ end
104
+ end
85
105
  end
86
106
  end
@@ -109,5 +109,25 @@ RSpec.describe JSRailsRoutes::Route do
109
109
  it { is_expected.to be true }
110
110
  end
111
111
  end
112
+
113
+ context 'when route_filter option is specified' do
114
+ before do
115
+ JSRailsRoutes.configure do |c|
116
+ c.route_filter = ->(_route) { result }
117
+ end
118
+ end
119
+
120
+ context 'and it returns true' do
121
+ let(:result) { true }
122
+
123
+ it { is_expected.to be true }
124
+ end
125
+
126
+ context 'and it returns false' do
127
+ let(:result) { false }
128
+
129
+ it { is_expected.to be false }
130
+ end
131
+ end
112
132
  end
113
133
  end
@@ -24,8 +24,10 @@ RSpec.describe JSRailsRoutes do
24
24
  end
25
25
  end
26
26
 
27
- describe '.generate_javascript' do
28
- subject { described_class.generate_javascript(task) }
27
+ describe '.generate' do
28
+ include_context 'run in a sandbox'
29
+
30
+ subject { described_class.generate(task) }
29
31
 
30
32
  let(:task) { 'js:routes' }
31
33
  let(:app_root) { JSRailsRoutes::SpecHelper::TestApp.root }
@@ -35,34 +37,88 @@ RSpec.describe JSRailsRoutes do
35
37
  FileUtils.mkdir_p(app_root.join('app/assets/javascripts'))
36
38
  end
37
39
 
38
- it 'generates javascript files' do
39
- subject
40
-
41
- expect(File.read(app_root.join('app/assets/javascripts/rails-routes.js'))).to eq <<~JAVASCRIPT
42
- // Don't edit manually. `rake #{task}` generates this file.
43
- #{JSRailsRoutes::Language::JavaScript::PROCESS_FUNC}
44
- export function blogs_path(params) { return process('/blogs', params, []); }
45
- export function new_blog_path(params) { return process('/blogs/new', params, []); }
46
- export function edit_blog_path(params) { return process('/blogs/' + params.id + '/edit', params, ['id']); }
47
- export function blog_path(params) { return process('/blogs/' + params.id + '', params, ['id']); }
48
- export function users_path(params) { return process('/users', params, []); }
49
- export function new_user_path(params) { return process('/users/new', params, []); }
50
- export function edit_user_path(params) { return process('/users/' + params.id + '/edit', params, ['id']); }
51
- export function user_path(params) { return process('/users/' + params.id + '', params, ['id']); }
52
- JAVASCRIPT
53
-
54
- expect(File.read(app_root.join('app/assets/javascripts/admin-routes.js'))).to eq <<~JAVASCRIPT
55
- // Don't edit manually. `rake #{task}` generates this file.
56
- #{JSRailsRoutes::Language::JavaScript::PROCESS_FUNC}
57
- export function notes_path(params) { return process('/notes', params, []); }
58
- export function new_note_path(params) { return process('/notes/new', params, []); }
59
- export function edit_note_path(params) { return process('/notes/' + params.id + '/edit', params, ['id']); }
60
- export function note_path(params) { return process('/notes/' + params.id + '', params, ['id']); }
61
- export function photos_path(params) { return process('/photos', params, []); }
62
- export function new_photo_path(params) { return process('/photos/new', params, []); }
63
- export function edit_photo_path(params) { return process('/photos/' + params.id + '/edit', params, ['id']); }
64
- export function photo_path(params) { return process('/photos/' + params.id + '', params, ['id']); }
65
- JAVASCRIPT
40
+ shared_examples_for 'javascript target' do
41
+ it 'generates javascript files' do
42
+ subject
43
+
44
+ expect(File.read(app_root.join('app/assets/javascripts/rails-routes.js'))).to eq <<~JAVASCRIPT
45
+ // Don't edit manually. `rake #{task}` generates this file.
46
+ #{JSRailsRoutes::Language::JavaScript::PROCESS_FUNC}
47
+ export function blogs_path(params) { return process('/blogs', params, []); }
48
+ export function new_blog_path(params) { return process('/blogs/new', params, []); }
49
+ export function edit_blog_path(params) { return process('/blogs/' + params.id + '/edit', params, ['id']); }
50
+ export function blog_path(params) { return process('/blogs/' + params.id + '', params, ['id']); }
51
+ export function users_path(params) { return process('/users', params, []); }
52
+ export function new_user_path(params) { return process('/users/new', params, []); }
53
+ export function edit_user_path(params) { return process('/users/' + params.id + '/edit', params, ['id']); }
54
+ export function user_path(params) { return process('/users/' + params.id + '', params, ['id']); }
55
+ JAVASCRIPT
56
+
57
+ expect(File.read(app_root.join('app/assets/javascripts/admin-routes.js'))).to eq <<~JAVASCRIPT
58
+ // Don't edit manually. `rake #{task}` generates this file.
59
+ #{JSRailsRoutes::Language::JavaScript::PROCESS_FUNC}
60
+ export function notes_path(params) { return process('/notes', params, []); }
61
+ export function new_note_path(params) { return process('/notes/new', params, []); }
62
+ export function edit_note_path(params) { return process('/notes/' + params.id + '/edit', params, ['id']); }
63
+ export function note_path(params) { return process('/notes/' + params.id + '', params, ['id']); }
64
+ export function photos_path(params) { return process('/photos', params, []); }
65
+ export function new_photo_path(params) { return process('/photos/new', params, []); }
66
+ export function edit_photo_path(params) { return process('/photos/' + params.id + '/edit', params, ['id']); }
67
+ export function photo_path(params) { return process('/photos/' + params.id + '', params, ['id']); }
68
+ JAVASCRIPT
69
+ end
70
+ end
71
+
72
+ context 'without target config' do
73
+ include_examples 'javascript target'
74
+ end
75
+
76
+ context 'with target="js"' do
77
+ before do
78
+ described_class.configure do |c|
79
+ c.target = 'js'
80
+ end
81
+ end
82
+
83
+ include_examples 'javascript target'
84
+ end
85
+
86
+ context 'with target="ts"' do
87
+ before do
88
+ described_class.configure do |c|
89
+ c.target = 'ts'
90
+ end
91
+ end
92
+
93
+ it 'generates typescript files' do
94
+ subject
95
+
96
+ expect(File.read(app_root.join('app/assets/javascripts/rails-routes.ts'))).to eq <<~JAVASCRIPT
97
+ // Don't edit manually. `rake #{task}` generates this file.
98
+ #{JSRailsRoutes::Language::TypeScript::PROCESS_FUNC}
99
+ export function blogs_path(params: Params) { return process('/blogs', params, []); }
100
+ export function new_blog_path(params: Params) { return process('/blogs/new', params, []); }
101
+ export function edit_blog_path(params: Params) { return process('/blogs/' + params.id + '/edit', params, ['id']); }
102
+ export function blog_path(params: Params) { return process('/blogs/' + params.id + '', params, ['id']); }
103
+ export function users_path(params: Params) { return process('/users', params, []); }
104
+ export function new_user_path(params: Params) { return process('/users/new', params, []); }
105
+ export function edit_user_path(params: Params) { return process('/users/' + params.id + '/edit', params, ['id']); }
106
+ export function user_path(params: Params) { return process('/users/' + params.id + '', params, ['id']); }
107
+ JAVASCRIPT
108
+
109
+ expect(File.read(app_root.join('app/assets/javascripts/admin-routes.ts'))).to eq <<~JAVASCRIPT
110
+ // Don't edit manually. `rake #{task}` generates this file.
111
+ #{JSRailsRoutes::Language::TypeScript::PROCESS_FUNC}
112
+ export function notes_path(params: Params) { return process('/notes', params, []); }
113
+ export function new_note_path(params: Params) { return process('/notes/new', params, []); }
114
+ export function edit_note_path(params: Params) { return process('/notes/' + params.id + '/edit', params, ['id']); }
115
+ export function note_path(params: Params) { return process('/notes/' + params.id + '', params, ['id']); }
116
+ export function photos_path(params: Params) { return process('/photos', params, []); }
117
+ export function new_photo_path(params: Params) { return process('/photos/new', params, []); }
118
+ export function edit_photo_path(params: Params) { return process('/photos/' + params.id + '/edit', params, ['id']); }
119
+ export function photo_path(params: Params) { return process('/photos/' + params.id + '', params, ['id']); }
120
+ JAVASCRIPT
121
+ end
66
122
  end
67
123
  end
68
124
  end
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.7.1
4
+ version: 0.8.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: 2018-07-26 00:00:00.000000000 Z
11
+ date: 2018-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -147,6 +147,7 @@ files:
147
147
  - lib/js_rails_routes/generator.rb
148
148
  - lib/js_rails_routes/language/base.rb
149
149
  - lib/js_rails_routes/language/javascript.rb
150
+ - lib/js_rails_routes/language/typescript.rb
150
151
  - lib/js_rails_routes/route.rb
151
152
  - lib/js_rails_routes/route_set.rb
152
153
  - lib/js_rails_routes/version.rb
@@ -156,6 +157,7 @@ files:
156
157
  - spec/js_rails_routes/generator_spec.rb
157
158
  - spec/js_rails_routes/language/base_spec.rb
158
159
  - spec/js_rails_routes/language/javascript_spec.rb
160
+ - spec/js_rails_routes/language/typescript_spec.rb
159
161
  - spec/js_rails_routes/route_set_spec.rb
160
162
  - spec/js_rails_routes/route_spec.rb
161
163
  - spec/js_rails_routes_spec.rb
@@ -184,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
186
  version: '0'
185
187
  requirements: []
186
188
  rubyforge_project:
187
- rubygems_version: 2.6.11
189
+ rubygems_version: 2.7.6
188
190
  signing_key:
189
191
  specification_version: 4
190
192
  summary: Generate a ES6 module that contains Rails routes.