js_from_routes 3.0.2 → 4.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b4a7e4009c35ed9b21a6ea1010d157faaed01d21143086a7573d87ae1a21aae
4
- data.tar.gz: 67db6af0cbae2656a450649384c86f19a9c890fd58e84ea99fa7f03299866dd0
3
+ metadata.gz: '0638fb2569cd2fb42213161ed2ac0f850edf79a3d383b067fbfef9ba28c91635'
4
+ data.tar.gz: 5dc8afb64a0ba08b94ac23f253c7934e485690d67af2811a0aff123ff97acb3e
5
5
  SHA512:
6
- metadata.gz: 4b519fba61999909979dd313995733497c9536cb5fda7bdf0c0585e133ab718f4b733c46014d2b3b039fa81b7b70d6b8939e3e155656613dafc0577f64970041
7
- data.tar.gz: 570cc1c4a497adf344b02e52c8c1da005877830be355638f85be7c9fe664094b03ee9d62c66a5295db1dfcd5d47993928581c0949f239b90f041e0d85f51c619
6
+ metadata.gz: 7df240239621acee87a8c216218dbd6aa138cf49b117929930da3153da651b4d242ec4cc27c2e0a734fee796fb632e5005a60f56848de807a8dd93faf84d0636
7
+ data.tar.gz: 4e179ae1663a0e77f300e7f6875246e58ad120f1d2fb9b4620a1f148621d9489d2f2897982e792a1f904b896650d7537caf6763ce363390e51b7a94533004053
data/CHANGELOG.md CHANGED
@@ -1,3 +1,21 @@
1
+ ## [4.0.1](https://github.com/ElMassimo/js_from_routes/compare/js_from_routes@4.0.0...js_from_routes@4.0.1) (2024-11-08)
2
+
3
+
4
+ ### Features
5
+
6
+ * allow using with rails 8 ([ffcefb9](https://github.com/ElMassimo/js_from_routes/commit/ffcefb9c4eda530cec107b0e74e71d318f6e76a0)), closes [#53](https://github.com/ElMassimo/js_from_routes/issues/53)
7
+
8
+
9
+
10
+ # [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)
11
+
12
+
13
+ ### Features
14
+
15
+ * 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))
16
+
17
+
18
+
1
19
  ## [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)
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
- .uniq { |route| route.requirements.fetch(:action) }
19
- .map { |route| Route.new(route, config.helper_mappings) }
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).camelize(:lower)
78
- @mappings.fetch(action, action)
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.
@@ -145,6 +154,16 @@ module JsFromRoutes
145
154
  end
146
155
  end
147
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
+
148
167
  class << self
149
168
  # Public: Configuration of the code generator.
150
169
  def config
@@ -164,7 +183,8 @@ module JsFromRoutes
164
183
 
165
184
  def generate_files(exported_routes)
166
185
  template = Template.new(config.template_path)
167
- generate_file_for_all exported_routes.map { |controller, routes|
186
+ generate_file_for_all exported_routes.filter_map { |controller, routes|
187
+ next unless controller
168
188
  ControllerRoutes.new(controller, routes, config).tap do |routes|
169
189
  template.write_if_changed routes
170
190
  end
@@ -177,24 +197,28 @@ module JsFromRoutes
177
197
  preferred_extension = File.extname(config.file_suffix)
178
198
  index_file = (config.all_helpers_file == true) ? "index#{preferred_extension}" : config.all_helpers_file
179
199
 
180
- Template.new(config.template_all_path).write_if_changed OpenStruct.new(
200
+ Template.new(config.template_all_path).write_if_changed TemplateConfig.new(
181
201
  cache_key: routes.map(&:import_filename).join + File.read(config.template_all_path),
182
202
  filename: config.output_folder.join("all#{preferred_extension}"),
183
203
  helpers: routes,
184
204
  )
185
- Template.new(config.template_index_path).write_if_changed OpenStruct.new(
205
+ Template.new(config.template_index_path).write_if_changed TemplateConfig.new(
186
206
  cache_key: File.read(config.template_index_path),
187
207
  filename: config.output_folder.join(index_file),
188
208
  )
189
209
  end
190
210
 
211
+ def namespace_for_route(route)
212
+ if (export = route.defaults[:export]).is_a?(Hash)
213
+ export[:namespace]
214
+ end || route.requirements[:controller]
215
+ end
216
+
191
217
  # Internal: Returns exported routes grouped by controller name.
192
218
  def exported_routes_by_controller(routes)
193
- routes.select { |route|
194
- config.export_if.call(route) && route.requirements[:controller]
195
- }.group_by { |route|
196
- route.requirements.fetch(:controller)
197
- }
219
+ routes
220
+ .select { |route| config.export_if.call(route) }
221
+ .group_by { |route| namespace_for_route(route)&.to_s }
198
222
  end
199
223
  end
200
224
  end
@@ -4,5 +4,5 @@
4
4
  # Generates one file per controller, and one function per route.
5
5
  module JsFromRoutes
6
6
  # Public: This library adheres to semantic versioning.
7
- VERSION = "3.0.2"
7
+ VERSION = "4.0.1"
8
8
  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: 3.0.2
4
+ version: 4.0.1
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: 2024-09-25 00:00:00.000000000 Z
11
+ date: 2024-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5.1'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '8'
22
+ version: '9'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '5.1'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '8'
32
+ version: '9'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: bundler
35
35
  requirement: !ruby/object:Gem::Requirement