b4um_generators 0.4.0 → 0.4.2

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.
@@ -64,6 +64,10 @@ module B4um
64
64
  .map(&:strip)
65
65
  .reject(&:empty?)
66
66
  end
67
+
68
+ def revoking?
69
+ behavior == :revoke
70
+ end
67
71
  end
68
72
 
69
73
  def validate_editor_options
@@ -181,6 +185,7 @@ module B4um
181
185
  end
182
186
 
183
187
  def add_required_validations
188
+ return if revoking?
184
189
  return if required_fields.empty?
185
190
 
186
191
  validation = required_fields
@@ -198,6 +203,7 @@ module B4um
198
203
  end
199
204
 
200
205
  def add_secure_password
206
+ return if revoking?
201
207
  return unless attributes.any? { |attribute| attribute.name == "password_digest" }
202
208
 
203
209
  inject_into_class(
@@ -208,6 +214,7 @@ module B4um
208
214
  end
209
215
 
210
216
  def add_bcrypt
217
+ return if revoking?
211
218
  return unless attributes.any? { |attribute| attribute.name == "password_digest" }
212
219
 
213
220
  gemfile_path = "Gemfile"
@@ -221,6 +228,7 @@ module B4um
221
228
  end
222
229
 
223
230
  def update_password_params
231
+ return if revoking?
224
232
  return unless attributes.any? { |attribute| attribute.name == "password_digest" }
225
233
 
226
234
  controller_path = File.join(
@@ -233,74 +241,41 @@ module B4um
233
241
  ":password_digest",
234
242
  ":password, :password_confirmation"
235
243
  )
244
+
245
+ update_password_test_params
236
246
  end
237
247
 
238
248
  def update_multiple_attachments
239
- attachment_attributes = attributes.select do |attribute|
240
- attribute.type == :attachments
241
- end
249
+ return if revoking?
250
+
251
+ attachment_names = multiple_attachment_names
242
252
 
243
- return if attachment_attributes.empty?
253
+ return if attachment_names.empty?
244
254
 
245
255
  controller_path = File.join(
246
256
  "app/controllers",
247
257
  "#{plural_table_name}_controller.rb"
248
258
  )
249
259
 
250
- attachment_names = attachment_attributes.map(&:name)
251
-
252
- setup_lines = attachment_names.map do |name|
253
- [
254
- " new_#{name} = update_params.delete(\"#{name}\")",
255
- " remove_#{name}_ids = update_params.delete(\"remove_#{name}_ids\")"
256
- ]
257
- end.flatten.join("\n")
258
-
259
- attachment_lines = attachment_names.map do |name|
260
- [
261
- " @#{singular_table_name}.#{name}.attach(new_#{name}) if new_#{name}.present?",
262
- "",
263
- " if remove_#{name}_ids.present?",
264
- " @#{singular_table_name}.#{name}.attachments",
265
- " .where(id: remove_#{name}_ids)",
266
- " .find_each(&:purge)",
267
- " end"
268
- ]
269
- end.flatten.join("\n")
270
-
271
- gsub_file(
260
+ add_multiple_attachment_setup(
272
261
  controller_path,
273
- " def update\n respond_to do |format|\n",
274
- [
275
- " def update",
276
- " update_params = #{singular_table_name}_params",
277
- setup_lines,
278
- "",
279
- " respond_to do |format|",
280
- ""
281
- ].join("\n")
262
+ attachment_names
282
263
  )
283
264
 
284
- gsub_file(
265
+ add_multiple_attachment_handling(
285
266
  controller_path,
286
- " if @#{singular_table_name}.update(#{singular_table_name}_params)\n",
287
- [
288
- " if @#{singular_table_name}.update(update_params)",
289
- attachment_lines,
290
- ""
291
- ].join("\n")
267
+ attachment_names
292
268
  )
293
269
 
294
- attachment_names.each do |name|
295
- gsub_file(
296
- controller_path,
297
- "#{name}: []",
298
- "#{name}: [], remove_#{name}_ids: []"
299
- )
300
- end
270
+ add_multiple_attachment_removal_params(
271
+ controller_path,
272
+ attachment_names
273
+ )
301
274
  end
302
275
 
303
276
  def add_readable_param
277
+ return if revoking?
278
+
304
279
  param_name = options[:param]
305
280
 
306
281
  return if param_name.blank?
@@ -315,18 +290,24 @@ module B4um
315
290
  return
316
291
  end
317
292
 
293
+ readable_method = [
294
+ " def to_param",
295
+ " \"\#{id} \#{#{param_name}}\".parameterize",
296
+ " end",
297
+ "",
298
+ ""
299
+ ].join("\n")
300
+
318
301
  inject_into_class(
319
302
  File.join("app/models", "#{singular_table_name}.rb"),
320
303
  class_name,
321
- <<~RUBY
322
- def to_param
323
- "\#{id} \#{#{param_name}}".parameterize
324
- end
325
- RUBY
304
+ readable_method
326
305
  )
327
306
  end
328
307
 
329
308
  def update_destroy_flash
309
+ return if revoking?
310
+
330
311
  controller_path = File.join(
331
312
  "app/controllers",
332
313
  "#{plural_table_name}_controller.rb"
@@ -343,16 +324,16 @@ module B4um
343
324
  gsub_file(
344
325
  controller_path,
345
326
  /
346
- format\.html\ \{\ redirect_to\ ([^,]+),
327
+ ^([ \t]*)format\.html\ \{\ redirect_to\ ([^,]+),
347
328
  \ notice:\ ("[^"]*\ was\ successfully\ destroyed\."),
348
329
  \ status:\ :see_other\ \}
349
330
  /x,
350
- <<~RUBY.chomp
351
- format.html do
352
- flash[:deleted] = \\2
353
- redirect_to \\1, status: :see_other
354
- end
355
- RUBY
331
+ [
332
+ "\\1format.html do",
333
+ "\\1 flash[:deleted] = \\3",
334
+ "\\1 redirect_to \\2, status: :see_other",
335
+ "\\1end"
336
+ ].join("\n")
356
337
  )
357
338
 
358
339
  return
@@ -361,11 +342,11 @@ module B4um
361
342
  # Rails-Scaffold ohne respond_to
362
343
  gsub_file(
363
344
  controller_path,
364
- /redirect_to ([^,]+), notice: ("[^"]* was successfully destroyed\."), status: :see_other/,
365
- <<~RUBY.chomp
366
- flash[:deleted] = \\2
367
- redirect_to \\1, status: :see_other
368
- RUBY
345
+ /^([ \t]*)redirect_to ([^,]+), notice: ("[^"]* was successfully destroyed\."), status: :see_other/,
346
+ [
347
+ "\\1flash[:deleted] = \\3",
348
+ "\\1redirect_to \\2, status: :see_other"
349
+ ].join("\n")
369
350
  )
370
351
  end
371
352
 
@@ -376,15 +357,8 @@ module B4um
376
357
  return unless File.exist?(full_navigation_path)
377
358
 
378
359
  navigation = File.read(full_navigation_path)
379
-
380
- return if navigation.include?(
381
- "controller: :#{plural_table_name}"
382
- )
383
-
384
360
  marker = "<%# B4UM_NAVIGATION_LINKS %>"
385
361
 
386
- return unless navigation.include?(marker)
387
-
388
362
  navigation_link = <<~ERB
389
363
  <%= navigation_link_to "#{plural_table_name.humanize}",
390
364
  #{plural_route_name}_path,
@@ -393,6 +367,34 @@ module B4um
393
367
  #{marker}
394
368
  ERB
395
369
 
370
+ if revoking?
371
+ generated_link = navigation_link.chomp
372
+
373
+ return unless navigation.include?(generated_link)
374
+
375
+ File.write(
376
+ full_navigation_path,
377
+ navigation.sub(
378
+ generated_link,
379
+ marker
380
+ )
381
+ )
382
+
383
+ say_status(
384
+ :remove,
385
+ "#{plural_table_name.humanize} navigation link",
386
+ :red
387
+ )
388
+
389
+ return
390
+ end
391
+
392
+ return if navigation.include?(
393
+ "controller: :#{plural_table_name}"
394
+ )
395
+
396
+ return unless navigation.include?(marker)
397
+
396
398
  gsub_file(
397
399
  navigation_path,
398
400
  marker,
@@ -402,6 +404,94 @@ module B4um
402
404
 
403
405
  private
404
406
 
407
+ def multiple_attachment_names
408
+ attributes
409
+ .select { |attribute| attribute.type == :attachments }
410
+ .map(&:name)
411
+ end
412
+
413
+ def add_multiple_attachment_setup(
414
+ controller_path,
415
+ attachment_names
416
+ )
417
+ setup_lines = attachment_names.flat_map do |name|
418
+ [
419
+ " new_#{name} = update_params.delete(\"#{name}\")",
420
+ " remove_#{name}_ids = update_params.delete(\"remove_#{name}_ids\")"
421
+ ]
422
+ end.join("\n")
423
+
424
+ gsub_file(
425
+ controller_path,
426
+ " def update\n respond_to do |format|\n",
427
+ [
428
+ " def update",
429
+ " update_params = #{singular_table_name}_params",
430
+ setup_lines,
431
+ "",
432
+ " respond_to do |format|",
433
+ ""
434
+ ].join("\n")
435
+ )
436
+ end
437
+
438
+ def add_multiple_attachment_handling(
439
+ controller_path,
440
+ attachment_names
441
+ )
442
+ attachment_lines = attachment_names.flat_map do |name|
443
+ [
444
+ " @#{singular_table_name}.#{name}.attach(new_#{name}) if new_#{name}.present?",
445
+ "",
446
+ " if remove_#{name}_ids.present?",
447
+ " @#{singular_table_name}.#{name}.attachments",
448
+ " .where(id: remove_#{name}_ids)",
449
+ " .find_each(&:purge)",
450
+ " end"
451
+ ]
452
+ end.join("\n")
453
+
454
+ gsub_file(
455
+ controller_path,
456
+ " if @#{singular_table_name}.update(#{singular_table_name}_params)\n",
457
+ [
458
+ " if @#{singular_table_name}.update(update_params)",
459
+ attachment_lines,
460
+ ""
461
+ ].join("\n")
462
+ )
463
+ end
464
+
465
+ def add_multiple_attachment_removal_params(
466
+ controller_path,
467
+ attachment_names
468
+ )
469
+ attachment_names.each do |name|
470
+ gsub_file(
471
+ controller_path,
472
+ "#{name}: []",
473
+ "#{name}: [], remove_#{name}_ids: []"
474
+ )
475
+ end
476
+ end
477
+
478
+ def update_password_test_params
479
+ test_path = File.join(
480
+ "test/controllers",
481
+ "#{plural_table_name}_controller_test.rb"
482
+ )
483
+
484
+ return unless File.exist?(
485
+ File.join(destination_root, test_path)
486
+ )
487
+
488
+ gsub_file(
489
+ test_path,
490
+ "password_digest: @#{singular_table_name}.password_digest",
491
+ 'password: "password", password_confirmation: "password"'
492
+ )
493
+ end
494
+
405
495
  def validate_editor_configuration(option_name, configuration)
406
496
  return unless options[option_name].present?
407
497
 
@@ -51,30 +51,80 @@ module B4um
51
51
  )
52
52
  end
53
53
 
54
+ def update_fixtures
55
+ return unless File.exist?(fixture_path)
56
+
57
+ fixtures = File.read(fixture_path)
58
+ position = 0
59
+
60
+ updated_fixtures = fixtures.gsub(
61
+ /^([^\s#][^:\n]*:\s*\n)((?:^[ \t]+.*\n?)*)/
62
+ ) do
63
+ fixture_header = Regexp.last_match(1)
64
+ fixture_body = Regexp.last_match(2)
65
+
66
+ next "#{fixture_header}#{fixture_body}" if fixture_header.start_with?("_")
67
+
68
+ position += 1
69
+
70
+ if fixture_body.match?(/^[ \t]+position:\s+/)
71
+ fixture_body = fixture_body.sub(
72
+ /^[ \t]+position:\s+.*$/,
73
+ " position: #{position}"
74
+ )
75
+
76
+ "#{fixture_header}#{fixture_body}"
77
+ else
78
+ "#{fixture_header} position: #{position}\n#{fixture_body}"
79
+ end
80
+ end
81
+
82
+ return if updated_fixtures == fixtures
83
+
84
+ File.write(fixture_path, updated_fixtures)
85
+
86
+ say_status(
87
+ :fixture,
88
+ "#{plural_table_name}.yml now includes sortable positions",
89
+ :green
90
+ )
91
+ end
92
+
54
93
  def update_model
55
94
  model_content = File.read(model_path)
56
95
 
57
- return if model_content.include?("before_create :b4um_set_sortable_position")
96
+ return if model_content.include?(
97
+ "before_create :b4um_set_sortable_position"
98
+ )
58
99
 
59
- inject_into_class(
60
- model_path,
61
- class_name,
62
- <<~RUBY.indent(2)
63
- before_create :b4um_set_sortable_position
64
- after_destroy :b4um_compact_sortable_positions
100
+ existing_model_body = model_content
101
+ .lines[1...-1]
102
+ .to_a
103
+ .any? { |line| line.match?(/\S/) }
65
104
 
66
- def b4um_set_sortable_position
67
- return if position.present?
105
+ sortable_model_code = <<~RUBY.indent(2)
106
+ before_create :b4um_set_sortable_position
107
+ after_destroy :b4um_compact_sortable_positions
68
108
 
69
- self.position = self.class.maximum(:position).to_i + 1
70
- end
109
+ def b4um_set_sortable_position
110
+ return if position.present?
71
111
 
72
- def b4um_compact_sortable_positions
73
- self.class
74
- .where("position > ?", position)
75
- .update_all("position = position - 1")
76
- end
77
- RUBY
112
+ self.position = self.class.maximum(:position).to_i + 1
113
+ end
114
+
115
+ def b4um_compact_sortable_positions
116
+ self.class
117
+ .where("position > ?", position)
118
+ .update_all("position = position - 1")
119
+ end
120
+ RUBY
121
+
122
+ sortable_model_code << "\n" if existing_model_body
123
+
124
+ inject_into_class(
125
+ model_path,
126
+ class_name,
127
+ sortable_model_code
78
128
  )
79
129
 
80
130
  say_status(
@@ -217,6 +267,14 @@ module B4um
217
267
  )
218
268
  end
219
269
 
270
+ def fixture_path
271
+ File.join(
272
+ destination_root,
273
+ "test/fixtures",
274
+ "#{plural_table_name}.yml"
275
+ )
276
+ end
277
+
220
278
  def model_path
221
279
  File.join(
222
280
  destination_root,
@@ -322,15 +380,16 @@ module B4um
322
380
 
323
381
  head :no_content
324
382
  end
325
-
326
383
  RUBY
327
384
 
385
+ action = action.rstrip
386
+
328
387
  private_marker = "\n private\n"
329
388
 
330
389
  if controller_content.include?(private_marker)
331
390
  insert_into_file(
332
391
  controller_path,
333
- action,
392
+ "\n#{action}\n",
334
393
  before: private_marker
335
394
  )
336
395
  else
@@ -341,10 +400,10 @@ module B4um
341
400
  "Could not find the end of app/controllers/#{plural_table_name}_controller.rb"
342
401
  end
343
402
 
344
- insert_into_file(
403
+ gsub_file(
345
404
  controller_path,
346
- "\n#{action}",
347
- before: controller_end
405
+ controller_end,
406
+ "\n\n#{action}\nend\n"
348
407
  )
349
408
  end
350
409
  end
@@ -354,7 +413,7 @@ module B4um
354
413
  return if controller_content.include?("if request.get?")
355
414
 
356
415
  sort_action =
357
- /^(\s*)def sort\s*$/
416
+ /^([ \t]*)def sort[ \t]*$/
358
417
 
359
418
  unless controller_content.match?(sort_action)
360
419
  raise Thor::Error,
@@ -365,15 +424,15 @@ module B4um
365
424
  controller_path,
366
425
  sort_action
367
426
  ) do |match|
368
- indentation = match[/\A\s*/]
369
-
370
- <<~RUBY.chomp
371
- #{match}
372
- #{indentation} if request.get?
373
- #{indentation} @#{plural_table_name} = #{class_name}.order(:position, :id)
374
- #{indentation} return
375
- #{indentation} end
376
- RUBY
427
+ indentation = match[/\A[ \t]*/]
428
+
429
+ [
430
+ match,
431
+ "#{indentation} if request.get?",
432
+ "#{indentation} @#{plural_table_name} = #{class_name}.order(:position, :id)",
433
+ "#{indentation} return",
434
+ "#{indentation} end"
435
+ ].join("\n")
377
436
  end
378
437
  end
379
438
 
@@ -510,19 +569,28 @@ module B4um
510
569
  end
511
570
 
512
571
  def schema_contains_position?
513
- schema_path = File.join(destination_root, "db/schema.rb")
572
+ schema_path = File.join(
573
+ destination_root,
574
+ "db/schema.rb"
575
+ )
514
576
 
515
577
  return false unless File.exist?(schema_path)
516
578
 
517
579
  schema = File.read(schema_path)
518
580
 
519
581
  table_pattern = /
520
- create_table\s+"#{Regexp.escape(plural_table_name)}".*?^ end
582
+ ^[ \t]*create_table\s+"#{Regexp.escape(plural_table_name)}".*?\n
583
+ (?<table_body>.*?)
584
+ ^[ \t]{2}end\s*$
521
585
  /mx
522
586
 
523
- table = schema[table_pattern]
587
+ match = schema.match(table_pattern)
524
588
 
525
- table&.match?(/t\.integer\s+"position"/) || false
589
+ return false unless match
590
+
591
+ match[:table_body].match?(
592
+ /^[ \t]+t\.integer\s+"position"/
593
+ )
526
594
  end
527
595
  end
528
596
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: b4um_generators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Baum