bullet_train-super_scaffolding 1.8.1 → 1.8.3

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: 5ec15bca016eaa77bfebdc79d680d0a52eef302b2da4134b5dbc88601a7e9241
4
- data.tar.gz: dd4979b27b3e6494785fabc20d2c7ebf3e658101e945f969169d6c69b7eb8ded
3
+ metadata.gz: 2a205bbac5111136b98f7af3d1a87f76a1fbd20449e8c8c2329caeed751df896
4
+ data.tar.gz: 4707519e317c5b3a605010d528485cd90e44c54566aa201bd35848b0cc6d47ff
5
5
  SHA512:
6
- metadata.gz: 1f7be30bfc863f0b38e94f9cc9fd095327adee6376f6e8c60543478fe72129887d683113c8dae5b9588c4059c60534785f5a2b8f7570865ec5e8df47fc06848f
7
- data.tar.gz: 7c110b4b40ec2a0712262efc7dabfef99ea168495ac31ba0e987642c9180ff968d3dbb4ecc5b0431c7a3021b171605f7306d140b67efd644f4623e7482353ab7
6
+ metadata.gz: f0cd1a2f64f9e7c96f81b950e44089885446f3988cc1e8552b392785464f2a034010c5117e5c26343d5c275143d49eea10756ae266d6cb043c29c7b237af1f3f
7
+ data.tar.gz: f7909f319a4ffc2563d5a66cb5ed35c0ee769eb639c671356b34fdd796700ccb34fdfe448aa1b051c6b01f6d1682f450bc18b2ba933e658c96b083d9fd20c479
@@ -1,5 +1,5 @@
1
1
  module BulletTrain
2
2
  module SuperScaffolding
3
- VERSION = "1.8.1"
3
+ VERSION = "1.8.3"
4
4
  end
5
5
  end
@@ -158,6 +158,33 @@ class Scaffolding::RoutesFileManipulator
158
158
  namespace_block_start.present? ? {namespace => namespace_block_start} : {}
159
159
  end
160
160
 
161
+ def find_last_in_namespace(needle, namespaces, within = nil, ignore = nil)
162
+ if namespaces.any?
163
+ namespace_lines = find_namespaces(namespaces, within)
164
+ within = namespace_lines[namespaces.last]
165
+ end
166
+
167
+ lines_within = Scaffolding::FileManipulator.lines_within(lines, within)
168
+ lines_within.reverse.each_with_index do |line, line_number|
169
+ reversed_line_number = lines_within.count - line_number - 1
170
+ # + 2 because line_number starts from 0, and within starts one line after
171
+ actual_line_number = (within + reversed_line_number + 2)
172
+
173
+ # The lines we want to ignore may be a a series of blocks, so we check each Range here.
174
+ ignore_line = false
175
+ if ignore.present?
176
+ ignore.each do |lines_to_ignore|
177
+ ignore_line = true if lines_to_ignore.include?(actual_line_number)
178
+ end
179
+ end
180
+
181
+ next if ignore_line
182
+ return (within + (within ? 1 : 0) + reversed_line_number) if line.match?(needle)
183
+ end
184
+
185
+ nil
186
+ end
187
+
161
188
  def find_in_namespace(needle, namespaces, within = nil, ignore = nil)
162
189
  if namespaces.any?
163
190
  namespace_lines = find_namespaces(namespaces, within)
@@ -187,15 +214,26 @@ class Scaffolding::RoutesFileManipulator
187
214
  within = options[:within]
188
215
  parts = parts.dup
189
216
  resource = parts.pop
217
+ needle = /resources :#{resource}#{options[:options] ? ", #{options[:options].gsub(/({)(.*)(})/, '{\2}')}" : ""}(,?\s.*)? do(\s.*)?$/
218
+
190
219
  # TODO this doesn't take into account any options like we do in `find_resource`.
191
- find_in_namespace(/resources :#{resource}#{options[:options] ? ", #{options[:options].gsub(/({)(.*)(})/, '{\2}')}" : ""}(,?\s.*)? do(\s.*)?$/, parts, within)
220
+ if options[:find_last]
221
+ find_last_in_namespace(needle, parts, within)
222
+ else
223
+ find_in_namespace(needle, parts, within)
224
+ end
192
225
  end
193
226
 
194
227
  def find_resource(parts, options = {})
195
228
  parts = parts.dup
196
229
  resource = parts.pop
197
230
  needle = /resources :#{resource}#{options[:options] ? ", #{options[:options].gsub(/({)(.*)(})/, '{\2}')}" : ""}(,?\s.*)?$/
198
- find_in_namespace(needle, parts, options[:within], options[:ignore])
231
+
232
+ if options[:find_last]
233
+ find_last_in_namespace(needle, parts, options[:within], options[:ignore])
234
+ else
235
+ find_in_namespace(needle, parts, options[:within], options[:ignore])
236
+ end
199
237
  end
200
238
 
201
239
  def find_or_create_resource(parts, options = {})
@@ -278,17 +316,18 @@ class Scaffolding::RoutesFileManipulator
278
316
  end
279
317
 
280
318
  def find_or_convert_resource_block(parent_resource, options = {})
281
- unless find_resource_block([parent_resource], options)
282
- if (resource_line_number = find_resource([parent_resource], options))
283
- # convert it.
284
- lines[resource_line_number].gsub!("\n", " do\n")
285
- insert_after(["end"], resource_line_number)
286
- else
287
- raise BulletTrain::SuperScaffolding::CannotFindParentResourceException.new("the parent resource (`#{parent_resource}`) doesn't appear to exist in `#{@filename}`.")
319
+ resource_statement_line = find_resource([parent_resource], options)
320
+ if resource_statement_line
321
+ resource_statement = lines[resource_statement_line]
322
+ if !resource_statement.match?(/ do(\s.*)?$/)
323
+ lines[resource_statement_line].gsub!("\n", " do\n")
324
+ insert_after(["end"], resource_statement_line)
288
325
  end
326
+ else
327
+ raise BulletTrain::SuperScaffolding::CannotFindParentResourceException.new("the parent resource (`#{parent_resource}`) doesn't appear to exist in `#{@filename}`.")
289
328
  end
290
329
 
291
- # update the block of code we're working within.
330
+ # capture the line number of the block of code we're working within.
292
331
  unless (within = find_resource_block([parent_resource], options))
293
332
  raise "tried to convert the parent resource to a block, but failed?"
294
333
  end
@@ -328,13 +367,18 @@ class Scaffolding::RoutesFileManipulator
328
367
  # end
329
368
  # end
330
369
 
331
- parent_within = find_or_convert_resource_block(parent_resource, within: within)
370
+ parent_within = find_resource([parent_resource], within: within)
371
+
372
+ # With deeply nested namespaces we end up with mutliple resource blocks and some things need
373
+ # to go into the last block. See this issue for details:
374
+ # https://github.com/bullet-train-co/bullet_train/issues/1655
375
+ last_parent_within = find_or_convert_resource_block(parent_resource, within: within, find_last: true)
332
376
 
333
377
  # add the new resource within that namespace.
334
378
  line = "scope module: '#{parent_resource}' do"
335
379
  # TODO you haven't tested this yet.
336
- unless (scope_within = Scaffolding::FileManipulator.find(lines, /#{line}/, parent_within))
337
- scope_within = insert([line, "end"], parent_within)
380
+ unless (scope_within = Scaffolding::FileManipulator.find(lines, /#{line}/, last_parent_within))
381
+ scope_within = insert([line, "end"], last_parent_within)
338
382
  end
339
383
 
340
384
  if child_namespaces.size > 1
@@ -361,7 +405,9 @@ class Scaffolding::RoutesFileManipulator
361
405
  else
362
406
  routing_options = "only: collection_actions"
363
407
  routing_options += ", #{formatted_concerns}" if formatted_concerns
364
- find_or_create_resource([child_resource], options: routing_options, within: scope_within)
408
+ # We find last here for this reason:
409
+ # https://github.com/bullet-train-co/bullet_train/issues/1655
410
+ find_or_create_resource([child_resource], options: routing_options, within: scope_within, find_last: true)
365
411
 
366
412
  # namespace :projects do
367
413
  # resources :deliverables, except: collection_actions
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bullet_train-super_scaffolding
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.8.1
4
+ version: 1.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Culver
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-30 00:00:00.000000000 Z
11
+ date: 2024-11-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: standard