js_rails_routes 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +33 -0
- data/CHANGELOG.md +5 -1
- data/Gemfile +2 -0
- data/README.md +4 -4
- data/js_rails_routes.gemspec +2 -1
- data/lib/js_rails_routes/builder.rb +27 -0
- data/lib/js_rails_routes/configuration.rb +32 -0
- data/lib/js_rails_routes/generator.rb +21 -89
- data/lib/js_rails_routes/language/base.rb +20 -0
- data/lib/js_rails_routes/language/javascript.rb +61 -0
- data/lib/js_rails_routes/route.rb +34 -0
- data/lib/js_rails_routes/route_set.rb +53 -0
- data/lib/js_rails_routes/version.rb +1 -1
- data/lib/js_rails_routes.rb +35 -1
- data/lib/tasks/js_rails_routes.rake +3 -9
- data/spec/js_rails_routes/builder_spec.rb +38 -0
- data/spec/js_rails_routes/configuration_spec.rb +79 -0
- data/spec/js_rails_routes/generator_spec.rb +18 -187
- data/spec/js_rails_routes/language/base_spec.rb +11 -0
- data/spec/js_rails_routes/language/javascript_spec.rb +150 -0
- data/spec/js_rails_routes/route_set_spec.rb +86 -0
- data/spec/js_rails_routes/route_spec.rb +113 -0
- data/spec/js_rails_routes_spec.rb +61 -2
- data/spec/spec_helper.rb +1 -17
- data/spec/support/matchers/not_change.rb +3 -0
- data/spec/support/shared_contexts/run_in_a_sandbox.rb +7 -0
- data/spec/support/test_app.rb +34 -0
- metadata +33 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1768a1c21e72db96adfed68e4e68cf5ba15a2ad2
|
4
|
+
data.tar.gz: cecbb2cbd58e60f4e0449dcd12b8c903d1aa65bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d599a65ae0bb2396c780f13c9cfee5a9431fd8c7e4c5ff1b5d6319c0f8650ea8db8db20b504f22e67c03357181b44d5c9a9a3aefe5cc56c5009120ea691a0fde
|
7
|
+
data.tar.gz: 06c1288cbf4107240254dbef471fca09b1d81a60301cf6c0a5a2b6ccb5a07eecdf3d731ab3628b3c265c930b623aefc951e887f71b2b936ccc0d410e784de272
|
data/.rubocop.yml
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
|
1
4
|
AllCops:
|
2
5
|
TargetRubyVersion: 2.3
|
3
6
|
|
@@ -10,3 +13,33 @@ Metrics/BlockLength:
|
|
10
13
|
|
11
14
|
Style/Documentation:
|
12
15
|
Enabled: false
|
16
|
+
|
17
|
+
Layout/MultilineMethodCallIndentation:
|
18
|
+
Exclude:
|
19
|
+
- 'spec/**/*'
|
20
|
+
|
21
|
+
RSpec/ExampleLength:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
RSpec/MultipleExpectations:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
RSpec/NamedSubject:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
RSpec/VerifiedDoubles:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
RSpec/NestedGroups:
|
34
|
+
Max: 4
|
35
|
+
|
36
|
+
RSpec/ContextWording:
|
37
|
+
Prefixes:
|
38
|
+
- when
|
39
|
+
- with
|
40
|
+
- without
|
41
|
+
- if
|
42
|
+
- and
|
43
|
+
|
44
|
+
RSpec/FilePath:
|
45
|
+
Enabled: false
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,9 @@ This change log adheres to [keepachangelog.com](http://keepachangelog.com).
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [0.7.1] - 2018-07-26
|
10
|
+
- Refactor whole code base
|
11
|
+
|
9
12
|
## [0.7.0] - 2018-07-11
|
10
13
|
### Added
|
11
14
|
- Support `camelize` option
|
@@ -51,7 +54,8 @@ This change log adheres to [keepachangelog.com](http://keepachangelog.com).
|
|
51
54
|
### Added
|
52
55
|
- Implement "js:rails:routes" task
|
53
56
|
|
54
|
-
[Unreleased]: https://github.com/yuku-t/js_rails_routes/compare/v0.7.
|
57
|
+
[Unreleased]: https://github.com/yuku-t/js_rails_routes/compare/v0.7.1...HEAD
|
58
|
+
[0.7.1]: https://github.com/yuku-t/js_rails_routes/compare/v0.7.0...v0.7.1
|
55
59
|
[0.7.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.6.0...v0.7.0
|
56
60
|
[0.6.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.5.0...v0.6.0
|
57
61
|
[0.5.0]: https://github.com/yuku-t/js_rails_routes/compare/v0.4.0...v0.5.0
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# rake js:routes
|
2
2
|
|
3
3
|
[](https://rubygems.org/gems/js_rails_routes)
|
4
|
-
[](https://travis-ci.org/yuku/js_rails_routes)
|
5
|
+
[](https://codeclimate.com/github/yuku/js_rails_routes)
|
6
|
+
[](https://codeclimate.com/github/yuku/js_rails_routes/coverage)
|
7
|
+
[](https://github.com/yuku/js_rails_routes/blob/master/LICENSE)
|
8
8
|
[](https://github.com/igrigorik/ga-beacon)
|
9
9
|
|
10
10
|
Generate a ES6 module that contains Rails routes.
|
data/js_rails_routes.gemspec
CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0'
|
22
22
|
spec.add_development_dependency 'rake', '~> 12.3'
|
23
23
|
spec.add_development_dependency 'rspec', '~> 3.7'
|
24
|
-
spec.add_development_dependency 'rubocop', '~> 0.
|
24
|
+
spec.add_development_dependency 'rubocop', '~> 0.58.1'
|
25
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 1.27'
|
25
26
|
spec.add_development_dependency 'simplecov', '~> 0.16.1'
|
26
27
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'js_rails_routes/route_set'
|
4
|
+
|
5
|
+
module JSRailsRoutes
|
6
|
+
class Builder
|
7
|
+
# @return [Array<JSRailsRoutes::RouteSet>]
|
8
|
+
attr_reader :route_set_list
|
9
|
+
|
10
|
+
# @return [JSRailsRoutes::Language::Base]
|
11
|
+
attr_reader :language
|
12
|
+
|
13
|
+
# @param language [JSRailsRoutes::Language::Base]
|
14
|
+
# @param route_set_list [Array<JSRailsRoutes::RouteSet>]
|
15
|
+
def initialize(language, route_set_list = RouteSet.correct_matching_route_set_list)
|
16
|
+
@language = language
|
17
|
+
@route_set_list = route_set_list
|
18
|
+
end
|
19
|
+
|
20
|
+
# @return [Hash{String => String}]
|
21
|
+
def build
|
22
|
+
route_set_list.each_with_object({}) do |route_set, memo|
|
23
|
+
memo[route_set.name] = language.handle_route_set(route_set)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JSRailsRoutes
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :include_paths,
|
6
|
+
:exclude_paths,
|
7
|
+
:include_names,
|
8
|
+
:exclude_names,
|
9
|
+
:exclude_engines,
|
10
|
+
:output_dir,
|
11
|
+
:camelize
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
self.include_paths = /.*/
|
15
|
+
self.exclude_paths = /^$/
|
16
|
+
self.include_names = /.*/
|
17
|
+
self.exclude_names = /^$/
|
18
|
+
self.exclude_engines = /^$/
|
19
|
+
self.camelize = nil
|
20
|
+
self.output_dir = Rails.root.join('app', 'assets', 'javascripts')
|
21
|
+
end
|
22
|
+
|
23
|
+
# @param env [Hash{String=>String}]
|
24
|
+
def configure_with_env_vars(env = ENV)
|
25
|
+
%w[include_paths exclude_paths include_names exclude_names exclude_engines].each do |name|
|
26
|
+
public_send("#{name}=", Regexp.new(env[name])) if env[name]
|
27
|
+
end
|
28
|
+
self.output_dir = env['output_dir'] if env['output_dir']
|
29
|
+
self.camelize = env['camelize'].presence.to_sym if env['camelize']
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -1,108 +1,40 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'js_rails_routes/route_set'
|
4
|
+
require 'js_rails_routes/builder'
|
4
5
|
|
5
6
|
module JSRailsRoutes
|
6
7
|
class Generator
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
for (var param in params) if (params.hasOwnProperty(param)) {
|
13
|
-
if (keys.indexOf(param) === -1) {
|
14
|
-
query.push(param + "=" + encodeURIComponent(params[param]));
|
15
|
-
}
|
16
|
-
}
|
17
|
-
return query.length ? route + "?" + query.join("&") : route;
|
18
|
-
}
|
19
|
-
JAVASCRIPT
|
20
|
-
|
21
|
-
include Singleton
|
22
|
-
|
23
|
-
attr_accessor :include_paths,
|
24
|
-
:exclude_paths,
|
25
|
-
:include_names,
|
26
|
-
:exclude_names,
|
27
|
-
:exclude_engines,
|
28
|
-
:output_dir,
|
29
|
-
:camelize
|
30
|
-
|
31
|
-
def initialize
|
32
|
-
self.include_paths = /.*/
|
33
|
-
self.exclude_paths = /^$/
|
34
|
-
self.include_names = /.*/
|
35
|
-
self.exclude_names = /^$/
|
36
|
-
self.exclude_engines = /^$/
|
37
|
-
self.camelize = nil
|
38
|
-
|
39
|
-
self.output_dir = Rails.root.join('app', 'assets', 'javascripts')
|
40
|
-
Rails.application.reload_routes!
|
8
|
+
# @param builder [JSRailsRoutes::Builder]
|
9
|
+
# @param writable [#write]
|
10
|
+
def initialize(builder, writable: File)
|
11
|
+
@builder = builder
|
12
|
+
@writable = writable
|
41
13
|
end
|
42
14
|
|
15
|
+
# @param task [String]
|
16
|
+
# @return [Hash{String => String}]
|
43
17
|
def generate(task)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end.compact
|
49
|
-
|
50
|
-
lines += [''] # End with new line
|
51
|
-
write(rails_engine_name, lines.join("\n"))
|
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}"
|
21
|
+
writable.write(file_name, file_body)
|
52
22
|
end
|
53
23
|
end
|
54
24
|
|
55
25
|
private
|
56
26
|
|
57
|
-
|
58
|
-
return false if include_paths !~ route_path
|
59
|
-
return false if exclude_paths =~ route_path
|
60
|
-
return false if include_names !~ route_name
|
61
|
-
return false if exclude_names =~ route_name
|
62
|
-
true
|
63
|
-
end
|
64
|
-
|
65
|
-
def handle_route(route_name, route_path)
|
66
|
-
keys = []
|
67
|
-
while route_path =~ COMPARE_REGEXP
|
68
|
-
keys.push("'#{Regexp.last_match(1)}'")
|
69
|
-
route_path.sub!(COMPARE_REGEXP, "' + params.#{Regexp.last_match(1)} + '#{Regexp.last_match(2)}")
|
70
|
-
end
|
71
|
-
|
72
|
-
function_name = make_function_name(route_name)
|
73
|
-
"export function #{function_name}(params) { return process('#{route_path}', params, [#{keys.join(',')}]); }"
|
74
|
-
end
|
75
|
-
|
76
|
-
def routes_with_engines
|
77
|
-
@routes_with_engines ||= [default_routes] + subengine_routes
|
78
|
-
end
|
79
|
-
|
80
|
-
def make_function_name(route_name)
|
81
|
-
url_helper_name = route_name + '_path'
|
82
|
-
camelize.nil? ? url_helper_name : url_helper_name.camelize(camelize)
|
83
|
-
end
|
84
|
-
|
85
|
-
def make_routes(routes)
|
86
|
-
routes
|
87
|
-
.select(&:name)
|
88
|
-
.map { |r| [r.name, r.path.spec.to_s.split('(')[0]] }
|
89
|
-
.sort_by(&:first)
|
90
|
-
end
|
91
|
-
|
92
|
-
def write(rails_engine_name, string)
|
93
|
-
file_name = File.join(output_dir, "#{rails_engine_name.gsub('::Engine', '').downcase}-routes.js")
|
94
|
-
File.write(file_name, string)
|
95
|
-
end
|
27
|
+
attr_reader :writable, :builder
|
96
28
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
.map { |engine| [engine.to_s, make_routes(engine.routes.routes)] }
|
101
|
-
.reject { |_, routes| routes.empty? }
|
29
|
+
# @return [JSRailsRoutes::Configuration]
|
30
|
+
def config
|
31
|
+
JSRailsRoutes.config
|
102
32
|
end
|
103
33
|
|
104
|
-
|
105
|
-
|
34
|
+
# @param engine_name [String]
|
35
|
+
# @return [String]
|
36
|
+
def convert(engine_name)
|
37
|
+
engine_name.gsub('::Engine', '').underscore.tr('/', '-')
|
106
38
|
end
|
107
39
|
end
|
108
40
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JSRailsRoutes
|
4
|
+
module Language
|
5
|
+
class Base
|
6
|
+
# @param routes [JSRailsRoutes::RouteSet]
|
7
|
+
# @return [String]
|
8
|
+
def handle_route_set(routes) # rubocop:disable Lint/UnusedMethodArgument
|
9
|
+
raise NotImplementedError
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
# @return [JSRailsRoutes::Configuration]
|
15
|
+
def config
|
16
|
+
JSRailsRoutes.config
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'js_rails_routes/route'
|
4
|
+
require 'js_rails_routes/language/base'
|
5
|
+
|
6
|
+
module JSRailsRoutes
|
7
|
+
module Language
|
8
|
+
class JavaScript < Base
|
9
|
+
PROCESS_FUNC = <<~JAVASCRIPT
|
10
|
+
function process(route, params, keys) {
|
11
|
+
var query = [];
|
12
|
+
for (var param in params) if (params.hasOwnProperty(param)) {
|
13
|
+
if (keys.indexOf(param) === -1) {
|
14
|
+
query.push(param + "=" + encodeURIComponent(params[param]));
|
15
|
+
}
|
16
|
+
}
|
17
|
+
return query.length ? route + "?" + query.join("&") : route;
|
18
|
+
}
|
19
|
+
JAVASCRIPT
|
20
|
+
|
21
|
+
# @note Implementation for {JSRailsRoutes::Language::Base#generate}
|
22
|
+
def handle_route_set(routes)
|
23
|
+
routes.each_with_object([PROCESS_FUNC]) do |route, lines|
|
24
|
+
lines.push(handle_route(route))
|
25
|
+
end.join("\n") + "\n"
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param route [JSRailsRoutes::Route]
|
29
|
+
# @return [String]
|
30
|
+
def handle_route(route)
|
31
|
+
path, keys = parse(route.path)
|
32
|
+
name = function_name(route.name)
|
33
|
+
"export function #{name}(params) { return process('#{path}', params, [#{keys.join(',')}]); }"
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# @param route_path [String]
|
39
|
+
# @return [Array<(String, Array<String>)>]
|
40
|
+
def parse(route_path)
|
41
|
+
destructured_path = route_path.dup
|
42
|
+
keys = []
|
43
|
+
while destructured_path =~ JSRailsRoutes::PARAM_REGEXP
|
44
|
+
keys.push("'#{Regexp.last_match(1)}'")
|
45
|
+
destructured_path.sub!(
|
46
|
+
JSRailsRoutes::PARAM_REGEXP,
|
47
|
+
"' + params.#{Regexp.last_match(1)} + '#{Regexp.last_match(2)}"
|
48
|
+
)
|
49
|
+
end
|
50
|
+
[destructured_path, keys]
|
51
|
+
end
|
52
|
+
|
53
|
+
# @param route_name [String]
|
54
|
+
# @return [String]
|
55
|
+
def function_name(route_name)
|
56
|
+
url_helper_name = route_name + '_path'
|
57
|
+
config.camelize.nil? ? url_helper_name : url_helper_name.camelize(config.camelize)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JSRailsRoutes
|
4
|
+
# Encapsulate a single routing rule
|
5
|
+
class Route
|
6
|
+
# @return [String]
|
7
|
+
attr_reader :name
|
8
|
+
|
9
|
+
# @return [String]
|
10
|
+
attr_reader :path
|
11
|
+
|
12
|
+
# @param route [ActionDispatch::Journey::Route]
|
13
|
+
def initialize(route)
|
14
|
+
@name = route.name
|
15
|
+
@path = route.path.spec.to_s.split('(').first
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [Boolean]
|
19
|
+
def match? # rubocop:disable Metrics/AbcSize
|
20
|
+
return false if config.include_paths !~ path
|
21
|
+
return false if config.exclude_paths =~ path
|
22
|
+
return false if config.include_names !~ name
|
23
|
+
return false if config.exclude_names =~ name
|
24
|
+
true
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
# @return [JSRailsRoutes::Configuration]
|
30
|
+
def config
|
31
|
+
JSRailsRoutes.config
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'js_rails_routes/route'
|
4
|
+
|
5
|
+
module JSRailsRoutes
|
6
|
+
# Encapsulate a set of routes
|
7
|
+
class RouteSet
|
8
|
+
include ::Enumerable
|
9
|
+
|
10
|
+
# @return [Array<JSRailsRoutes::RouteSet>]
|
11
|
+
def self.correct_matching_route_set_list
|
12
|
+
[
|
13
|
+
RouteSet.new('Rails', ::Rails.application.routes),
|
14
|
+
::Rails::Engine.subclasses.map do |engine|
|
15
|
+
RouteSet.new(engine.name, engine.routes)
|
16
|
+
end
|
17
|
+
].flatten.select(&:match?)
|
18
|
+
end
|
19
|
+
|
20
|
+
# @!method each
|
21
|
+
# @yield [JSRailsRoutes::Route>]
|
22
|
+
# @note Implementation for {Enumerable}
|
23
|
+
delegate :each, to: :routes
|
24
|
+
|
25
|
+
# @return [String]
|
26
|
+
attr_reader :name
|
27
|
+
|
28
|
+
# @return [Array<JSRailsRoutes::Route>]
|
29
|
+
attr_reader :routes
|
30
|
+
|
31
|
+
# @param name [String] engine name
|
32
|
+
# @param routes [ActionDispatch::Routing::RouteSet]
|
33
|
+
def initialize(name, routes)
|
34
|
+
@name = name
|
35
|
+
@routes = routes.routes
|
36
|
+
.select(&:name)
|
37
|
+
.map { |route| Route.new(route) }
|
38
|
+
.select(&:match?)
|
39
|
+
end
|
40
|
+
|
41
|
+
# @return [Boolean]
|
42
|
+
def match?
|
43
|
+
name !~ config.exclude_engines && routes.present?
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
# @return [JSRailsRoutes::Configuration]
|
49
|
+
def config
|
50
|
+
JSRailsRoutes.config
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/js_rails_routes.rb
CHANGED
@@ -1,13 +1,47 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'js_rails_routes/engine'
|
4
|
+
require 'js_rails_routes/configuration'
|
4
5
|
require 'js_rails_routes/generator'
|
5
6
|
require 'js_rails_routes/version'
|
7
|
+
require 'js_rails_routes/language/javascript'
|
6
8
|
|
7
9
|
module JSRailsRoutes
|
10
|
+
PARAM_REGEXP = %r{:(.*?)(/|$)}
|
11
|
+
|
8
12
|
module_function
|
9
13
|
|
14
|
+
# @yield [Configuration]
|
10
15
|
def configure
|
11
|
-
yield
|
16
|
+
yield config if block_given?
|
17
|
+
end
|
18
|
+
|
19
|
+
# Current configuration.
|
20
|
+
#
|
21
|
+
# @return [Configuration]
|
22
|
+
def config
|
23
|
+
@config ||= Configuration.new
|
24
|
+
end
|
25
|
+
|
26
|
+
# @param task [String]
|
27
|
+
def generate_javascript(task)
|
28
|
+
builder = Builder.new(Language::JavaScript.new)
|
29
|
+
Generator.new(builder).generate(task)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Execute a given block within a new sandbox. For test purpose.
|
33
|
+
#
|
34
|
+
# @yield
|
35
|
+
def sandbox
|
36
|
+
raise 'Already in a sandbox' if @sandbox
|
37
|
+
@sandbox = true
|
38
|
+
prev = @config
|
39
|
+
@config = Configuration.new
|
40
|
+
begin
|
41
|
+
yield if block_given?
|
42
|
+
ensure
|
43
|
+
@config = prev
|
44
|
+
@sandbox = nil
|
45
|
+
end
|
12
46
|
end
|
13
47
|
end
|
@@ -3,14 +3,8 @@
|
|
3
3
|
desc 'Generate a ES6 module that contains Rails routes'
|
4
4
|
namespace :js do
|
5
5
|
task routes: :environment do |task|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
generator.include_names = Regexp.new(ENV['include_names']) if ENV['include_names']
|
10
|
-
generator.exclude_names = Regexp.new(ENV['exclude_names']) if ENV['exclude_names']
|
11
|
-
generator.output_dir = ENV['output_dir'] if ENV['output_dir']
|
12
|
-
generator.camelize = ENV['camelize']&.to_sym if ENV['camelize']
|
13
|
-
generator.generate(task)
|
14
|
-
puts "Routes saved into #{generator.output_dir}."
|
6
|
+
JSRailsRoutes.config.configure_with_env_vars
|
7
|
+
JSRailsRoutes.generate_javascript(task)
|
8
|
+
puts "Routes saved into #{JSRailsRoutes.config.output_dir}."
|
15
9
|
end
|
16
10
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe JSRailsRoutes::Builder do
|
4
|
+
subject(:builder) { described_class.new(language, route_set_list) }
|
5
|
+
|
6
|
+
include_context 'run in a sandbox'
|
7
|
+
|
8
|
+
let(:language) { instance_double('JSRailsRoutes::Language::Base', handle_route_set: body) }
|
9
|
+
let(:body) { 'hello' }
|
10
|
+
let(:route_set_list) { [rails_route_set, engine_route_set] }
|
11
|
+
|
12
|
+
let(:rails_route_set) do
|
13
|
+
route_set = ActionDispatch::Routing::RouteSet.new.tap do |routes|
|
14
|
+
routes.draw do
|
15
|
+
get '/articles' => 'articles#index'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
JSRailsRoutes::RouteSet.new('Rails', route_set)
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:engine_route_set) do
|
22
|
+
route_set = ActionDispatch::Routing::RouteSet.new.tap do |routes|
|
23
|
+
routes.draw do
|
24
|
+
get '/users' => 'users#index'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
JSRailsRoutes::RouteSet.new('Users::Engine', route_set)
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#build' do
|
31
|
+
subject { builder.build }
|
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)
|
35
|
+
expect(language).to have_received(:handle_route_set).twice
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe JSRailsRoutes::Configuration do
|
4
|
+
subject(:config) { described_class.new }
|
5
|
+
|
6
|
+
describe '#configure_with_env_vars' do
|
7
|
+
subject { config.configure_with_env_vars(env) }
|
8
|
+
|
9
|
+
context 'with empty env' do
|
10
|
+
let(:env) { Hash[] }
|
11
|
+
|
12
|
+
it 'does not change' do
|
13
|
+
expect { subject }.to not_change(config, :include_paths)
|
14
|
+
.and not_change(config, :exclude_paths)
|
15
|
+
.and not_change(config, :include_names)
|
16
|
+
.and not_change(config, :exclude_names)
|
17
|
+
.and not_change(config, :exclude_engines)
|
18
|
+
.and not_change(config, :output_dir)
|
19
|
+
.and not_change(config, :camelize)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'with include_paths env' do
|
24
|
+
let(:env) { Hash['include_paths' => 'a'] }
|
25
|
+
|
26
|
+
it 'changes #include_paths' do
|
27
|
+
expect { subject }.to change(config, :include_paths).to eq(/a/)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'with exclude_paths env' do
|
32
|
+
let(:env) { Hash['exclude_paths' => 'a'] }
|
33
|
+
|
34
|
+
it 'changes #exclude_paths' do
|
35
|
+
expect { subject }.to change(config, :exclude_paths).to eq(/a/)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with include_names env' do
|
40
|
+
let(:env) { Hash['include_names' => 'a'] }
|
41
|
+
|
42
|
+
it 'changes #include_names' do
|
43
|
+
expect { subject }.to change(config, :include_names).to eq(/a/)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'with exclude_names env' do
|
48
|
+
let(:env) { Hash['exclude_names' => 'a'] }
|
49
|
+
|
50
|
+
it 'changes #exclude_names' do
|
51
|
+
expect { subject }.to change(config, :exclude_names).to eq(/a/)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'with exclude_engines env' do
|
56
|
+
let(:env) { Hash['exclude_engines' => 'a'] }
|
57
|
+
|
58
|
+
it 'changes #exclude_engines' do
|
59
|
+
expect { subject }.to change(config, :exclude_engines).to eq(/a/)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'with output_dir env' do
|
64
|
+
let(:env) { Hash['output_dir' => 'path'] }
|
65
|
+
|
66
|
+
it 'changes #output_dir' do
|
67
|
+
expect { subject }.to change(config, :output_dir).to eq 'path'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'with camelize env' do
|
72
|
+
let(:env) { Hash['camelize' => 'lower'] }
|
73
|
+
|
74
|
+
it 'changes #camelize' do
|
75
|
+
expect { subject }.to change(config, :camelize).to eq :lower
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|