js_from_routes 3.0.1 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/lib/js_from_routes/generator.rb +58 -30
- data/lib/js_from_routes/railtie.rb +1 -1
- data/lib/js_from_routes/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d025db2a61cac22a1639c8164379135ecc21495cf9088e100c3301af27836bc3
|
4
|
+
data.tar.gz: f2480ec441295677d3289269225052b99c91a8a7ba5ef554f6bc63a6eedf0fa2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cd07f08b897c978aaa8d38ddee26298ab9f0202f57cbad37cecaf4cd86694a7700f2b0980ca8e8f8821d9379bdf70db88f301ff3e67c5086e3c6e388909060d
|
7
|
+
data.tar.gz: a121e858da619a7b1843d708cb1b564aff0f2aca3762384361e4d02179e2bb58f4b65029bd09bf7ec4d4064a6d6da25b2a3099fe1e001a58891f5d245c4a29d3
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,21 @@
|
|
1
|
+
# [4.0.0](https://github.com/ElMassimo/js_from_routes/compare/js_from_routes@3.0.2...js_from_routes@4.0.0) (2024-10-11)
|
2
|
+
|
3
|
+
|
4
|
+
### Features
|
5
|
+
|
6
|
+
* allow customizing the namespace for path helpers ([#50](https://github.com/ElMassimo/js_from_routes/issues/50)) ([9d38c83](https://github.com/ElMassimo/js_from_routes/commit/9d38c8312ef1275b3b8c18c1129bc936da08bcd0))
|
7
|
+
|
8
|
+
|
9
|
+
|
10
|
+
## [3.0.2](https://github.com/ElMassimo/js_from_routes/compare/js_from_routes@3.0.1...js_from_routes@3.0.2) (2024-09-25)
|
11
|
+
|
12
|
+
|
13
|
+
### Bug Fixes
|
14
|
+
|
15
|
+
* remove references to OpenStruct ([#49](https://github.com/ElMassimo/js_from_routes/issues/49)) ([6c0d8de](https://github.com/ElMassimo/js_from_routes/commit/6c0d8ded2bf82ccc2f8f35508dc256c3b301069a)), closes [#47](https://github.com/ElMassimo/js_from_routes/issues/47)
|
16
|
+
|
17
|
+
|
18
|
+
|
1
19
|
## [3.0.1](https://github.com/ElMassimo/js_from_routes/compare/js_from_routes@3.0.0...js_from_routes@3.0.1) (2023-10-09)
|
2
20
|
|
3
21
|
|
@@ -15,8 +15,13 @@ module JsFromRoutes
|
|
15
15
|
def initialize(controller, routes, config)
|
16
16
|
@controller, @config = controller, config
|
17
17
|
@routes = routes
|
18
|
-
.
|
19
|
-
.
|
18
|
+
.reject { |route| route.requirements[:action] == "update" && route.verb == "PUT" }
|
19
|
+
.group_by { |route| route.requirements.fetch(:action) }
|
20
|
+
.flat_map { |action, routes|
|
21
|
+
routes.each_with_index.map { |route, index|
|
22
|
+
Route.new(route, mappings: config.helper_mappings, index:, controller:)
|
23
|
+
}
|
24
|
+
}
|
20
25
|
end
|
21
26
|
|
22
27
|
# Public: Used to check whether the file should be generated again, changes
|
@@ -53,8 +58,8 @@ module JsFromRoutes
|
|
53
58
|
|
54
59
|
# Internal: A presenter for an individual Rails action.
|
55
60
|
class Route
|
56
|
-
def initialize(route, mappings
|
57
|
-
@route, @mappings = route, mappings
|
61
|
+
def initialize(route, mappings:, controller:, index: 0)
|
62
|
+
@route, @mappings, @controller, @index = route, mappings, controller, index
|
58
63
|
end
|
59
64
|
|
60
65
|
# Public: The `export` setting specified for the action.
|
@@ -64,7 +69,7 @@ module JsFromRoutes
|
|
64
69
|
|
65
70
|
# Public: The HTTP verb for the action. Example: 'patch'
|
66
71
|
def verb
|
67
|
-
@route.verb.downcase
|
72
|
+
@route.verb.split("|").last.downcase
|
68
73
|
end
|
69
74
|
|
70
75
|
# Public: The path for the action. Example: '/users/:id/edit'
|
@@ -74,8 +79,12 @@ module JsFromRoutes
|
|
74
79
|
|
75
80
|
# Public: The name of the JS helper for the action. Example: 'destroyAll'
|
76
81
|
def helper
|
77
|
-
action = @route.requirements.fetch(:action)
|
78
|
-
@
|
82
|
+
action = @route.requirements.fetch(:action)
|
83
|
+
if @index > 0
|
84
|
+
action = @route.name&.sub(@controller.tr(":/", "_"), "") || "#{action}#{verb.titleize}"
|
85
|
+
end
|
86
|
+
helper = action.camelize(:lower)
|
87
|
+
@mappings.fetch(helper, helper)
|
79
88
|
end
|
80
89
|
|
81
90
|
# Internal: Useful as a cache key for the route, and for debugging purposes.
|
@@ -126,10 +135,39 @@ module JsFromRoutes
|
|
126
135
|
end
|
127
136
|
end
|
128
137
|
|
138
|
+
class Configuration
|
139
|
+
attr_accessor :all_helpers_file, :client_library, :export_if, :file_suffix,
|
140
|
+
:helper_mappings, :output_folder, :template_path,
|
141
|
+
:template_all_path, :template_index_path
|
142
|
+
|
143
|
+
def initialize(root)
|
144
|
+
dir = %w[frontend packs javascript assets].find { |dir| root.join("app", dir).exist? }
|
145
|
+
@all_helpers_file = true
|
146
|
+
@client_library = "@js-from-routes/client"
|
147
|
+
@export_if = ->(route) { route.defaults.fetch(:export, nil) }
|
148
|
+
@file_suffix = "Api.js"
|
149
|
+
@helper_mappings = {}
|
150
|
+
@output_folder = root.join("app", dir, "api")
|
151
|
+
@template_path = File.expand_path("template.js.erb", __dir__)
|
152
|
+
@template_all_path = File.expand_path("template_all.js.erb", __dir__)
|
153
|
+
@template_index_path = File.expand_path("template_index.js.erb", __dir__)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
class TemplateConfig
|
158
|
+
attr_reader :cache_key, :filename, :helpers
|
159
|
+
|
160
|
+
def initialize(cache_key:, filename:, helpers: nil)
|
161
|
+
@cache_key = cache_key
|
162
|
+
@filename = filename
|
163
|
+
@helpers = helpers
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
129
167
|
class << self
|
130
168
|
# Public: Configuration of the code generator.
|
131
169
|
def config
|
132
|
-
@config ||=
|
170
|
+
@config ||= Configuration.new(::Rails.root || Pathname.new(Dir.pwd))
|
133
171
|
yield(@config) if block_given?
|
134
172
|
@config
|
135
173
|
end
|
@@ -145,7 +183,8 @@ module JsFromRoutes
|
|
145
183
|
|
146
184
|
def generate_files(exported_routes)
|
147
185
|
template = Template.new(config.template_path)
|
148
|
-
generate_file_for_all exported_routes.
|
186
|
+
generate_file_for_all exported_routes.filter_map { |controller, routes|
|
187
|
+
next unless controller
|
149
188
|
ControllerRoutes.new(controller, routes, config).tap do |routes|
|
150
189
|
template.write_if_changed routes
|
151
190
|
end
|
@@ -156,41 +195,30 @@ module JsFromRoutes
|
|
156
195
|
return unless config.all_helpers_file && !routes.empty?
|
157
196
|
|
158
197
|
preferred_extension = File.extname(config.file_suffix)
|
159
|
-
index_file = config.all_helpers_file == true ? "index#{preferred_extension}" : config.all_helpers_file
|
198
|
+
index_file = (config.all_helpers_file == true) ? "index#{preferred_extension}" : config.all_helpers_file
|
160
199
|
|
161
|
-
Template.new(config.template_all_path).write_if_changed
|
200
|
+
Template.new(config.template_all_path).write_if_changed TemplateConfig.new(
|
162
201
|
cache_key: routes.map(&:import_filename).join + File.read(config.template_all_path),
|
163
202
|
filename: config.output_folder.join("all#{preferred_extension}"),
|
164
203
|
helpers: routes,
|
165
204
|
)
|
166
|
-
Template.new(config.template_index_path).write_if_changed
|
205
|
+
Template.new(config.template_index_path).write_if_changed TemplateConfig.new(
|
167
206
|
cache_key: File.read(config.template_index_path),
|
168
207
|
filename: config.output_folder.join(index_file),
|
169
208
|
)
|
170
209
|
end
|
171
210
|
|
172
|
-
def
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
client_library: "@js-from-routes/client",
|
177
|
-
export_if: ->(route) { route.defaults.fetch(:export, nil) },
|
178
|
-
file_suffix: "Api.js",
|
179
|
-
helper_mappings: {},
|
180
|
-
output_folder: root.join("app", dir, "api"),
|
181
|
-
template_path: File.expand_path("template.js.erb", __dir__),
|
182
|
-
template_all_path: File.expand_path("template_all.js.erb", __dir__),
|
183
|
-
template_index_path: File.expand_path("template_index.js.erb", __dir__),
|
184
|
-
}
|
211
|
+
def namespace_for_route(route)
|
212
|
+
if (export = route.defaults[:export]).is_a?(Hash)
|
213
|
+
export[:namespace]
|
214
|
+
end || route.requirements[:controller]
|
185
215
|
end
|
186
216
|
|
187
217
|
# Internal: Returns exported routes grouped by controller name.
|
188
218
|
def exported_routes_by_controller(routes)
|
189
|
-
routes
|
190
|
-
config.export_if.call(route)
|
191
|
-
|
192
|
-
route.requirements.fetch(:controller)
|
193
|
-
}
|
219
|
+
routes
|
220
|
+
.select { |route| config.export_if.call(route) }
|
221
|
+
.group_by { |route| namespace_for_route(route)&.to_s }
|
194
222
|
end
|
195
223
|
end
|
196
224
|
end
|
@@ -30,7 +30,7 @@ class JsFromRoutes::Railtie < Rails::Railtie
|
|
30
30
|
initializer "js_from_routes.required_defaults" do |app|
|
31
31
|
ActionDispatch::Journey::Route.prepend Module.new {
|
32
32
|
def required_default?(key)
|
33
|
-
key == :export ? false : super
|
33
|
+
(key == :export) ? false : super
|
34
34
|
end
|
35
35
|
}
|
36
36
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: js_from_routes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Máximo Mussini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -167,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
167
|
- !ruby/object:Gem::Version
|
168
168
|
version: '0'
|
169
169
|
requirements: []
|
170
|
-
rubygems_version: 3.
|
170
|
+
rubygems_version: 3.5.16
|
171
171
|
signing_key:
|
172
172
|
specification_version: 4
|
173
173
|
summary: Generate JS automatically from Rails routes.
|