js_from_routes 3.0.2 → 4.0.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: 2b4a7e4009c35ed9b21a6ea1010d157faaed01d21143086a7573d87ae1a21aae
4
- data.tar.gz: 67db6af0cbae2656a450649384c86f19a9c890fd58e84ea99fa7f03299866dd0
3
+ metadata.gz: d025db2a61cac22a1639c8164379135ecc21495cf9088e100c3301af27836bc3
4
+ data.tar.gz: f2480ec441295677d3289269225052b99c91a8a7ba5ef554f6bc63a6eedf0fa2
5
5
  SHA512:
6
- metadata.gz: 4b519fba61999909979dd313995733497c9536cb5fda7bdf0c0585e133ab718f4b733c46014d2b3b039fa81b7b70d6b8939e3e155656613dafc0577f64970041
7
- data.tar.gz: 570cc1c4a497adf344b02e52c8c1da005877830be355638f85be7c9fe664094b03ee9d62c66a5295db1dfcd5d47993928581c0949f239b90f041e0d85f51c619
6
+ metadata.gz: 2cd07f08b897c978aaa8d38ddee26298ab9f0202f57cbad37cecaf4cd86694a7700f2b0980ca8e8f8821d9379bdf70db88f301ff3e67c5086e3c6e388909060d
7
+ data.tar.gz: a121e858da619a7b1843d708cb1b564aff0f2aca3762384361e4d02179e2bb58f4b65029bd09bf7ec4d4064a6d6da25b2a3099fe1e001a58891f5d245c4a29d3
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
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
+
1
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)
2
11
 
3
12
 
@@ -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.0"
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.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: 2024-09-25 00:00:00.000000000 Z
11
+ date: 2024-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties