hot-glue 0.1.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +22 -19
  3. data/README.md +36 -19
  4. data/app/helpers/hot_glue/controller_helper.rb +1 -1
  5. data/lib/generators/hot_glue/install_generator.rb +0 -2
  6. data/lib/generators/hot_glue/markup_templates/base.rb +7 -0
  7. data/lib/generators/hot_glue/markup_templates/erb.rb +227 -0
  8. data/lib/generators/hot_glue/markup_templates/haml.rb +236 -0
  9. data/lib/generators/hot_glue/markup_templates/slim.rb +9 -0
  10. data/lib/generators/hot_glue/scaffold_generator.rb +127 -264
  11. data/lib/generators/hot_glue/templates/controller.rb.erb +14 -2
  12. data/lib/generators/hot_glue/templates/erb/_errors.erb +11 -0
  13. data/lib/generators/hot_glue/templates/erb/_flash_notices.erb +12 -0
  14. data/lib/generators/hot_glue/templates/erb/_form.erb +8 -0
  15. data/lib/generators/hot_glue/templates/erb/_line.erb +10 -0
  16. data/lib/generators/hot_glue/templates/erb/_list.erb +19 -0
  17. data/lib/generators/hot_glue/templates/erb/_new_button.erb +3 -0
  18. data/lib/generators/hot_glue/templates/erb/_new_form.erb +8 -0
  19. data/lib/generators/hot_glue/templates/erb/_show.erb +8 -0
  20. data/lib/generators/hot_glue/templates/erb/create.turbo_stream.erb +18 -0
  21. data/lib/generators/hot_glue/templates/erb/destroy.turbo_stream.erb +3 -0
  22. data/lib/generators/hot_glue/templates/erb/edit.erb +30 -0
  23. data/lib/generators/hot_glue/templates/erb/edit.turbo_stream.erb +3 -0
  24. data/lib/generators/hot_glue/templates/erb/index.erb +10 -0
  25. data/lib/generators/hot_glue/templates/erb/new.erb +1 -0
  26. data/lib/generators/hot_glue/templates/erb/update.turbo_stream.erb +10 -0
  27. data/lib/generators/hot_glue/templates/{_errors.haml → haml/_errors.haml} +0 -0
  28. data/lib/generators/hot_glue/templates/{_flash_notices.haml → haml/_flash_notices.haml} +0 -0
  29. data/lib/generators/hot_glue/templates/{_form.haml → haml/_form.haml} +1 -0
  30. data/lib/generators/hot_glue/templates/{_line.haml → haml/_line.haml} +0 -0
  31. data/lib/generators/hot_glue/templates/{_list.haml → haml/_list.haml} +0 -0
  32. data/lib/generators/hot_glue/templates/{_new_button.haml → haml/_new_button.haml} +0 -0
  33. data/lib/generators/hot_glue/templates/{_new_form.haml → haml/_new_form.haml} +0 -0
  34. data/lib/generators/hot_glue/templates/{_show.haml → haml/_show.haml} +0 -0
  35. data/lib/generators/hot_glue/templates/{create.turbo_stream.haml → haml/create.turbo_stream.haml} +0 -0
  36. data/lib/generators/hot_glue/templates/{destroy.turbo_stream.haml → haml/destroy.turbo_stream.haml} +0 -0
  37. data/lib/generators/hot_glue/templates/{edit.haml → haml/edit.haml} +0 -0
  38. data/lib/generators/hot_glue/templates/{edit.turbo_stream.haml → haml/edit.turbo_stream.haml} +0 -0
  39. data/lib/generators/hot_glue/templates/{index.haml → haml/index.haml} +0 -0
  40. data/lib/generators/hot_glue/templates/{new.haml → haml/new.haml} +0 -0
  41. data/lib/generators/hot_glue/templates/{update.turbo_stream.haml → haml/update.turbo_stream.haml} +0 -0
  42. data/lib/generators/hot_glue/templates/system_spec.rb.erb +1 -1
  43. data/lib/hot-glue.rb +6 -20
  44. data/lib/hotglue/version.rb +1 -1
  45. metadata +49 -24
@@ -1,71 +1,41 @@
1
1
  require 'rails/generators/erb/scaffold/scaffold_generator'
2
2
  require 'ffaker'
3
3
 
4
+ require_relative './markup_templates/base'
5
+ require_relative './markup_templates/erb'
6
+ require_relative './markup_templates/haml'
7
+ require_relative './markup_templates/slim'
4
8
 
5
9
  module HotGlue
6
10
  class Error < StandardError
7
11
  end
8
12
 
9
-
10
-
11
-
12
-
13
- module GeneratorHelper
14
- def derrive_reference_name thing_as_string
15
- assoc_class = eval(thing_as_string)
16
-
17
- if assoc_class.respond_to?("name")
18
- display_column = "name"
19
- elsif assoc_class.respond_to?("to_label")
20
- display_column = "to_label"
21
- elsif assoc_class.respond_to?("full_name")
22
- display_column = "full_name"
23
- elsif assoc_class.respond_to?("display_name")
24
- display_column = "display_name"
25
- elsif assoc_class.respond_to?("email")
26
- display_column = "email"
27
- else
28
- raise("this should have been caught by the checker in the initializer")
29
- # puts "*** Oops: Can't find any column to use as the display label for the #{assoc.name.to_s} association on the #{singular_class} model . TODO: Please implement just one of: 1) name, 2) to_label, 3) full_name, 4) display_name, or 5) email directly on your #{assoc.class_name} model (either as database field or model methods), then RERUN THIS GENERATOR. (If more than one is implemented, the field to use will be chosen based on the rank here, e.g., if name is present it will be used; if not, I will look for a to_label, etc)"
30
- end
31
- display_column
32
- end
33
-
34
-
35
- def text_area_output(col, field_length, col_identifier )
36
- lines = field_length % 40
37
- if lines > 5
38
- lines = 5
39
- end
40
-
41
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
42
- = f.text_area :#{col.to_s}, class: 'form-control', cols: 40, rows: '#{lines}'
43
- %label.form-text
44
- #{col.to_s.humanize}\n"
45
- end
46
-
47
-
48
-
49
- def field_output(col, type = nil, width, col_identifier )
50
-
51
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
52
- = f.text_field :#{col.to_s}, value: @#{singular}.#{col.to_s}, size: #{width}, class: 'form-control', type: '#{type}'
53
- %label.form-text
54
- #{col.to_s.humanize}\n"
55
- end
13
+ def self.derrive_reference_name(thing_as_string)
14
+ assoc_class = eval(thing_as_string)
15
+
16
+ if assoc_class.respond_to?("name")
17
+ display_column = "name"
18
+ elsif assoc_class.respond_to?("to_label")
19
+ display_column = "to_label"
20
+ elsif assoc_class.respond_to?("full_name")
21
+ display_column = "full_name"
22
+ elsif assoc_class.respond_to?("display_name")
23
+ display_column = "display_name"
24
+ elsif assoc_class.respond_to?("email")
25
+ display_column = "email"
26
+ else
27
+ raise("this should have been caught by the checker in the initializer")
28
+ # puts "*** Oops: Can't find any column to use as the display label for the #{assoc.name.to_s} association on the #{singular_class} model . TODO: Please implement just one of: 1) name, 2) to_label, 3) full_name, 4) display_name, or 5) email directly on your #{assoc.class_name} model (either as database field or model methods), then RERUN THIS GENERATOR. (If more than one is implemented, the field to use will be chosen based on the rank here, e.g., if name is present it will be used; if not, I will look for a to_label, etc)"
29
+ end
30
+ display_column
56
31
  end
57
32
 
58
-
59
33
  class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator
60
- hook_for :form_builder, :as => :scaffold
34
+ hook_for :form_builder, :as => :scaffold
61
35
 
62
36
  source_root File.expand_path('templates', __dir__)
63
37
  attr_accessor :path, :singular, :plural, :singular_class, :nest_with
64
38
 
65
-
66
- include GeneratorHelper
67
-
68
-
69
39
  class_option :singular, type: :string, default: nil
70
40
  class_option :plural, type: :string, default: nil
71
41
  class_option :singular_class, type: :string, default: nil
@@ -84,12 +54,23 @@ module HotGlue
84
54
  class_option :no_paginate, type: :boolean, default: false
85
55
  class_option :big_edit, type: :boolean, default: false
86
56
  class_option :show_only, type: :string, default: ""
57
+ class_option :markup, type: :string, default: "erb"
58
+
59
+
60
+
61
+ # def erb_replace_ampersands
62
+ # if @template_builder.is_a?(HotGlue::ErbTemplate)
63
+ # @output_buffer.gsub!('\%', '%')
64
+ # end
65
+ # end
66
+
67
+
87
68
 
88
- def initialize(*meta_args) #:nodoc:
69
+ def initialize(*meta_args)
89
70
  super
90
71
 
91
72
  begin
92
- object = eval(class_name)
73
+ @the_object = eval(class_name)
93
74
  rescue StandardError => e
94
75
  message = "*** Oops: It looks like there is no object for #{class_name}. Please define the object + database table first."
95
76
  raise(HotGlue::Error, message)
@@ -99,6 +80,18 @@ module HotGlue
99
80
  raise(HotGlue::Error, "*** Oops: You seem to have specified both the --specs-only flag and --no-specs flags. this doesn't make any sense, so I am aborting. sorry.")
100
81
  end
101
82
 
83
+ if options['markup'] == "erb"
84
+ @template_builder = HotGlue::ErbTemplate.new
85
+ elsif options['markup'] == "slim"
86
+ puts "SLIM IS NOT IMPLEMENTED; please see https://github.com/jasonfb/hot-glue/issues/3"
87
+ abort
88
+ @template_builder = HotGlue::SlimTemplate.new
89
+
90
+ elsif options['markup'] == "haml"
91
+ @template_builder = HotGlue::HamlTemplate.new
92
+ end
93
+ @markup = options['markup']
94
+
102
95
 
103
96
  args = meta_args[0]
104
97
  @singular = args.first.tableize.singularize # should be in form hello_world
@@ -125,7 +118,6 @@ module HotGlue
125
118
  @show_only += options['show_only'].split(",").collect(&:to_sym)
126
119
  end
127
120
 
128
- auth_assoc = @auth.gsub("current_","")
129
121
 
130
122
  @god = options['god'] || options['gd'] || false
131
123
  @specs_only = options['specs_only'] || false
@@ -172,15 +164,23 @@ module HotGlue
172
164
  end
173
165
  end
174
166
 
167
+ @reference_name = HotGlue.derrive_reference_name(singular_class)
168
+
169
+ identify_object_owner
170
+ setup_fields
171
+ end
172
+
173
+ def identify_object_owner
174
+ auth_assoc = @auth && @auth.gsub("current_","")
175
175
 
176
176
  if !@object_owner_sym.empty?
177
- auth_assoc_field = auth_assoc + "_id"
177
+ auth_assoc_field = auth_assoc + "_id" unless @god
178
178
  assoc = eval("#{singular_class}.reflect_on_association(:#{@object_owner_sym})")
179
179
 
180
180
  if assoc
181
- ownership_field = assoc.name.to_s + "_id"
181
+ @ownership_field = assoc.name.to_s + "_id"
182
182
  elsif !@nest
183
- exit_message = "*** Oops: It looks like is no association from current_#{@object_owner_sym} to a class called #{@singular_class}. If your user is called something else, pass with flag auth=current_X where X is the model for your users as lowercase. Also, be sure to implement current_X as a method on your controller. (If you really don't want to implement a current_X on your controller and want me to check some other method for your current user, see the section in the docs for auth_identifier.) To make a controller that can read all records, specify with --god."
183
+ exit_message = "*** Oops: It looks like is no association from current_#{@object_owner_sym} to a class called #{@singular_class}. If your user is called something else, pass with flag auth=current_X where X is the model for your users as lowercase. Also, be sure to implement current_X as a method on your controller. (If you really don't want to implement a current_X on your controller and want me to check some other method for your current user, see the section in the docs for auth_identifier.) To make a controller that can read all records, specify with --god."
184
184
 
185
185
  else
186
186
  if @god
@@ -201,6 +201,11 @@ module HotGlue
201
201
  raise(HotGlue::Error, exit_message)
202
202
  end
203
203
  end
204
+ end
205
+
206
+
207
+ def setup_fields
208
+ auth_assoc = @auth && @auth.gsub("current_","")
204
209
 
205
210
  if !@include_fields
206
211
  @exclude_fields.push :id, :created_at, :updated_at, :encrypted_password,
@@ -209,15 +214,14 @@ module HotGlue
209
214
  :confirmation_token, :confirmed_at,
210
215
  :confirmation_sent_at, :unconfirmed_email
211
216
 
212
- @exclude_fields.push(auth_assoc_field.to_sym) if !auth_assoc_field.nil?
213
- @exclude_fields.push(ownership_field.to_sym) if !ownership_field.nil?
217
+ @exclude_fields.push( (auth_assoc + "_id").to_sym) if ! auth_assoc.nil?
218
+ @exclude_fields.push( @ownership_field.to_sym ) if ! @ownership_field.nil?
214
219
 
215
220
 
216
- @columns = object.columns.map(&:name).map(&:to_sym).reject{|field| @exclude_fields.include?(field) }
221
+ @columns = @the_object.columns.map(&:name).map(&:to_sym).reject{|field| @exclude_fields.include?(field) }
217
222
 
218
223
  else
219
- @columns = object.columns.map(&:name).map(&:to_sym).reject{|field| !@include_fields.include?(field) }
220
-
224
+ @columns = @the_object.columns.map(&:name).map(&:to_sym).reject{|field| !@include_fields.include?(field) }
221
225
  end
222
226
 
223
227
  @columns.each do |col|
@@ -225,7 +229,7 @@ module HotGlue
225
229
  @show_only << col
226
230
  end
227
231
 
228
- if object.columns_hash[col.to_s].type == :integer
232
+ if @the_object.columns_hash[col.to_s].type == :integer
229
233
  if col.to_s.ends_with?("_id")
230
234
  # guess the association name label
231
235
  assoc_name = col.to_s.gsub("_id","")
@@ -264,8 +268,6 @@ module HotGlue
264
268
  end
265
269
  end
266
270
 
267
-
268
-
269
271
  #
270
272
  def formats
271
273
  [format]
@@ -277,7 +279,6 @@ module HotGlue
277
279
 
278
280
  def copy_controller_and_spec_files
279
281
  @default_colspan = @columns.size
280
-
281
282
  unless @specs_only
282
283
  template "controller.rb.erb", File.join("#{'spec/dummy/' if Rails.env.test?}app/controllers#{namespace_with_dash}", "#{plural}_controller.rb")
283
284
  if @namespace
@@ -294,11 +295,12 @@ module HotGlue
294
295
  template "system_spec.rb.erb", File.join("#{'spec/dummy/' if Rails.env.test?}spec/system#{namespace_with_dash}", "#{plural}_behavior_spec.rb")
295
296
  end
296
297
 
297
- template "_errors.haml", File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}", "_errors.haml")
298
+ template "#{@markup}/_errors.#{@markup}", File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}", "_errors.#{@markup}")
298
299
  end
299
300
 
300
301
  def list_column_headings
301
- @columns.map(&:to_s).map{|col_name| ' .col ' + col_name.humanize}.join("\n")
302
+ @template_builder.list_column_headings(columns: @columns)
303
+
302
304
  end
303
305
 
304
306
  def columns_spec_with_sample_data
@@ -369,11 +371,6 @@ module HotGlue
369
371
  @auth_identifier
370
372
  end
371
373
 
372
-
373
- # def path_helper_full
374
- # "#{@namespace+"_" if @namespace}#{(@nested_args.join("_") + "_" if @nested_args.any?)}#{singular}_path"
375
- # end
376
-
377
374
  def path_helper_args
378
375
  if @nested_args.any?
379
376
  [(@nested_args).collect{|a| "@#{a}"} , singular].join(",")
@@ -411,7 +408,14 @@ module HotGlue
411
408
  end
412
409
 
413
410
  def new_path_name
414
- "new_#{@namespace+"_" if @namespace}#{singular}_path"
411
+
412
+ base = "new_#{@namespace+"_" if @namespace}#{(@nested_args.join("_") + "_") if @nested_args.any?}#{singular}_path"
413
+ if @nested_args.any?
414
+ base += "(" + @nested_args.collect { |arg|
415
+ "@#{arg}.id"
416
+ }.join(", ") + ")"
417
+ end
418
+ base
415
419
  end
416
420
 
417
421
  def nested_assignments
@@ -466,40 +470,60 @@ module HotGlue
466
470
  end
467
471
 
468
472
  def all_objects_variable
469
-
470
- # needs the authenticated root user
471
- # "#{@auth}.#{ @nested_args.map{|a| "#{@nested_args_plural[a]}.find(@#{a})"}.join('.') + "." if @nested_args.any?}#{plural}"
472
-
473
473
  all_objects_root + ".page(params[:page])"
474
-
475
474
  end
476
475
 
477
476
  def auth_object
478
477
  @auth
479
478
  end
480
479
 
481
-
482
480
  def no_devise_installed
483
481
  !Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }['devise']
484
482
  end
485
483
 
484
+ # def erb_replace_ampersands!(filename = nil)
485
+ #
486
+ # return if filename.nil?
487
+ # file = File.open(filename, "r")
488
+ # contents = file.read
489
+ # file.close
490
+ #
491
+ # file = File.open(filename, "w")
492
+ # file.write( contents.gsub('\%', '%'))
493
+ # file.close
494
+ # end
495
+
486
496
 
487
497
 
488
498
 
489
499
 
490
500
  def copy_view_files
491
501
  return if @specs_only
492
- haml_views.each do |view|
502
+ all_views.each do |view|
493
503
  formats.each do |format|
494
- filename = cc_filename_with_extensions(view, "haml")
495
- template filename, File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}", controller_file_path, filename)
504
+ source_filename = cc_filename_with_extensions("#{@markup}/#{view}", "#{@markup}")
505
+ dest_filename = cc_filename_with_extensions("#{view}", "#{@markup}")
506
+ dest_filepath = File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}",
507
+ controller_file_path, dest_filename)
508
+
509
+
510
+ template source_filename, dest_filepath
511
+ gsub_file dest_filepath, '\%', '%'
512
+
496
513
  end
497
514
  end
498
515
 
499
516
  turbo_stream_views.each do |view|
500
517
  formats.each do |format|
501
- filename = cc_filename_with_extensions(view, 'turbo_stream.haml')
502
- template filename, File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}", controller_file_path, filename)
518
+ source_filename = cc_filename_with_extensions( "#{@markup}/#{view}.turbo_stream.#{@markup}")
519
+ dest_filename = cc_filename_with_extensions("#{view}", "turbo_stream.#{@markup}")
520
+ dest_filepath = File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}",
521
+ controller_file_path, dest_filename)
522
+
523
+
524
+ template source_filename, dest_filepath
525
+ gsub_file dest_filepath, '\%', '%'
526
+
503
527
  end
504
528
  end
505
529
  end
@@ -520,7 +544,7 @@ module HotGlue
520
544
  end
521
545
  end
522
546
 
523
- def haml_views
547
+ def all_views
524
548
  res = %w(index edit _form _line _list _show _errors)
525
549
 
526
550
  unless @no_create
@@ -552,180 +576,23 @@ module HotGlue
552
576
  end
553
577
 
554
578
  def all_form_fields
555
- col_identifier = " .col"
556
- col_spaces_prepend = " "
557
-
558
- res = @columns.map { |col|
559
-
560
- if @show_only.include?(col)
561
-
562
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
563
- = @#{singular}.#{col.to_s}
564
- %label.form-text
565
- #{col.to_s.humanize}\n"
566
- else
567
-
568
-
569
- type = eval("#{singular_class}.columns_hash['#{col}']").type
570
- limit = eval("#{singular_class}.columns_hash['#{col}']").limit
571
- sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
572
-
573
- case type
574
- when :integer
575
- # look for a belongs_to on this object
576
- if col.to_s.ends_with?("_id")
577
- assoc_name = col.to_s.gsub("_id","")
578
- assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
579
- if assoc.nil?
580
- exit_message= "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
581
- exit
582
- end
583
- display_column = derrive_reference_name(assoc.class_name)
584
-
585
-
586
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{assoc_name.to_s})}\"}
587
- #{col_spaces_prepend}= f.collection_select(:#{col.to_s}, #{assoc.class_name}.all, :id, :#{display_column}, {prompt: true, selected: @#{singular}.#{col.to_s} }, class: 'form-control')
588
- #{col_spaces_prepend}%label.small.form-text.text-muted
589
- #{col_spaces_prepend} #{col.to_s.humanize}"
590
-
591
- else
592
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if @#{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
593
- #{col_spaces_prepend}= f.text_field :#{col.to_s}, value: #{singular}.#{col.to_s}, class: 'form-control', size: 4, type: 'number'
594
- #{col_spaces_prepend}%label.form-text
595
- #{col_spaces_prepend} #{col.to_s.humanize}\n"
596
- end
597
- when :string
598
- limit ||= 256
599
- if limit <= 256
600
- field_output(col, nil, limit, col_identifier)
601
- else
602
- text_area_output(col, limit, col_identifier)
603
- end
604
-
605
- when :text
606
- limit ||= 256
607
- if limit <= 256
608
- field_output(col, nil, limit, col_identifier)
609
- else
610
- text_area_output(col, limit, col_identifier)
611
- end
612
- when :float
613
- limit ||= 256
614
- field_output(col, nil, limit, col_identifier)
615
-
616
-
617
- when :datetime
618
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
619
- #{col_spaces_prepend}= datetime_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{col.to_s.humanize}', #{@auth ? @auth+'.timezone' : 'nil'})"
620
- when :date
621
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
622
- #{col_spaces_prepend}= date_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{col.to_s.humanize}', #{@auth ? @auth+'.timezone' : 'nil'})"
623
- when :time
624
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
625
- #{col_spaces_prepend}= time_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{col.to_s.humanize}', #{@auth ? @auth+'.timezone' : 'nil'})"
626
- when :boolean
627
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
628
- #{col_spaces_prepend}%span
629
- #{col_spaces_prepend} #{col.to_s.humanize}
630
- #{col_spaces_prepend}= f.radio_button(:#{col.to_s}, '0', checked: #{singular}.#{col.to_s} ? '' : 'checked')
631
- #{col_spaces_prepend}= f.label(:#{col.to_s}, value: 'No', for: '#{singular}_#{col.to_s}_0')
632
-
633
- #{col_spaces_prepend}= f.radio_button(:#{col.to_s}, '1', checked: #{singular}.#{col.to_s} ? 'checked' : '')
634
- #{col_spaces_prepend}= f.label(:#{col.to_s}, value: 'Yes', for: '#{singular}_#{col.to_s}_1')
635
- "
636
- end
637
- end
638
- }.join("\n")
639
- return res
579
+ @template_builder.all_form_fields(
580
+ columns: @columns,
581
+ show_only: @show_only,
582
+ singular_class: singular_class,
583
+ singular: singular
584
+ )
640
585
  end
641
586
 
642
-
643
587
  def all_line_fields
644
- columns = @columns.count + 1
645
- perc_width = (100/columns).floor
646
-
647
- col_identifer = ".col"
648
- @columns.map { |col|
649
- type = eval("#{singular_class}.columns_hash['#{col}']").type
650
- limit = eval("#{singular_class}.columns_hash['#{col}']").limit
651
- sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
652
-
653
- case type
654
- when :integer
655
- # look for a belongs_to on this object
656
- if col.to_s.ends_with?("_id")
657
-
658
- assoc_name = col.to_s.gsub("_id","")
659
-
660
-
661
- assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
662
-
663
- if assoc.nil?
664
- exit_message = "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
665
- raise(HotGlue::Error,exit_message)
666
- end
667
-
668
- display_column = derrive_reference_name(assoc.class_name)
669
-
670
-
671
- "#{col_identifer}
672
- = #{singular}.#{assoc.name.to_s}.try(:#{display_column}) || '<span class=\"content alert-danger\">MISSING</span>'.html_safe"
673
-
674
- else
675
- "#{col_identifer}
676
- = #{singular}.#{col}"
677
- end
678
- when :float
679
- width = (limit && limit < 40) ? limit : (40)
680
- "#{col_identifer}
681
- = #{singular}.#{col}"
682
-
683
- when :string
684
- width = (limit && limit < 40) ? limit : (40)
685
- "#{col_identifer}
686
- = #{singular}.#{col}"
687
- when :text
688
- "#{col_identifer}
689
- = #{singular}.#{col}"
690
- when :datetime
691
- "#{col_identifer}
692
- - unless #{singular}.#{col}.nil?
693
- = #{singular}.#{col}.in_time_zone(current_timezone).strftime('%m/%d/%Y @ %l:%M %p ') + timezonize(current_timezone)
694
- - else
695
- %span.alert-danger
696
- MISSING
697
- "
698
- when :date
699
- ".cell
700
- - unless #{singular}.#{col}.nil?
701
- = #{singular}.#{col}
702
- - else
703
- %span.alert-danger
704
- MISSING
705
- "
706
- when :time
707
- "#{col_identifer}
708
- - unless #{singular}.#{col}.nil?
709
- = #{singular}.#{col}.in_time_zone(current_timezone).strftime('%l:%M %p ') + timezonize(current_timezone)
710
- - else
711
- %span.alert-danger
712
- MISSING
713
- "
714
- when :boolean
715
- "#{col_identifer}
716
- - if #{singular}.#{col}.nil?
717
- %span.alert-danger
718
- MISSING
719
- - elsif #{singular}.#{col}
720
- YES
721
- - else
722
- NO
723
- "
724
- end
725
- }.join("\n")
588
+ @template_builder.all_line_fields(
589
+ columns: @columns,
590
+ show_only: @show_only,
591
+ singular_class: singular_class,
592
+ singular: singular
593
+ )
726
594
  end
727
595
 
728
-
729
596
  def controller_descends_from
730
597
  if defined?(@namespace.titlecase + "::BaseController")
731
598
  @namespace.titlecase + "::BaseController"
@@ -778,13 +645,9 @@ module HotGlue
778
645
  end
779
646
  end
780
647
 
781
-
782
-
783
- def paginate
784
- "- if #{plural}.respond_to?(:total_pages)
785
- = paginate #{plural}"
786
- end
787
-
648
+ def paginate
649
+ @template_builder.paginate(plural: plural)
650
+ end
788
651
  private # thor does something fancy like sending the class all of its own methods during some strange run sequence
789
652
  # does not like public methods
790
653
 
@@ -1,6 +1,11 @@
1
1
  class <%= controller_class_name %> < <%= controller_descends_from %>
2
2
  <% unless @auth_identifier == '' || @auth.nil? %>before_action :authenticate_<%= @auth_identifier %>!<% end %>
3
3
 
4
+ <% if any_nested? %><% nest_chain = [] %> <% @nested_args.each { |arg|
5
+ this_scope = nest_chain.empty? ? "#{@auth ? auth_object : class_name}.#{arg}s" : "#{nest_chain.last}.#{arg}s"
6
+ nest_chain << arg %>
7
+ before_action :<%= arg %>
8
+ <% } %><% end %>
4
9
  before_action :load_<%= singular_name %>, only: [:show, :edit, :update, :destroy]
5
10
  helper :hot_glue
6
11
  include HotGlue::ControllerHelper
@@ -9,9 +14,16 @@ class <%= controller_class_name %> < <%= controller_descends_from %>
9
14
  # TODO: implement current_user or use Devise
10
15
  <% end %>
11
16
 
17
+
18
+
12
19
  <% if any_nested? %><% nest_chain = [] %> <% @nested_args.each { |arg|
13
- this_scope = nest_chain.empty? ? "#{@auth ? auth_object : class_name}.#{arg}s" : "#{nest_chain.last}.#{arg}s"
14
- nest_chain << arg %>
20
+ if !@god
21
+ this_scope = nest_chain.empty? ? "#{@auth ? auth_object : class_name}.#{arg}s" : "#{nest_chain.last}.#{arg}s"
22
+ else
23
+ this_scope = eval(class_name + ".reflect_on_association(:#{arg})").class_name
24
+ end
25
+ nest_chain << arg
26
+ %>
15
27
  def <%= arg %>
16
28
  @<%= arg %> ||= <%= this_scope %>.find(params[:<%= arg %>_id])
17
29
  end<% } %><% end %>
@@ -0,0 +1,11 @@
1
+ <\%= turbo_frame_tag "errors" do %>
2
+ <\% if resource.errors.any? %>
3
+ <div id="error_explanation">
4
+ <\% resource.errors.full_messages.each do |message| %>
5
+ <div class="alert alert-danger">
6
+ <\%= message %>
7
+ </div>
8
+ <\% end %>
9
+ </div>
10
+ <\% end %>
11
+ <\% end %>
@@ -0,0 +1,12 @@
1
+ <%= turbo_frame_tag "flash_notices" do %>
2
+ <% unless notice.nil? %>
3
+ <div class="alert alert-notice alert-dismissible">
4
+ <%= notice %>
5
+ </div>
6
+ <% end %>
7
+ <% unless alert.nil? %>
8
+ <div class="alert alert-danger alert-dismissible">
9
+ <%= alert %>
10
+ </div>
11
+ <% end %>
12
+ <% end %>
@@ -0,0 +1,8 @@
1
+ <div class="row">
2
+ <%= all_form_fields %>
3
+
4
+ <div class="col">
5
+ <\%= link_to "Cancel", <%= path_helper_plural %>, {class: "btn btn-secondary"} %>
6
+ <\%= f.submit "Save", class: "btn btn-primary pull-right" %>
7
+ </div>
8
+ </div>
@@ -0,0 +1,10 @@
1
+
2
+ <\%= turbo_frame_tag "<%= singular %>__#{ <%= singular %>.id }" do %>
3
+ <div class='row' data-id='<\%= <%= singular %>.id %>' data-edit='false'>
4
+ <\%= render partial: '<%= show_path_partial %>', locals: { <%= singular %>: <%= singular %>} %>
5
+ </div>
6
+ <\% end %>
7
+
8
+
9
+
10
+
@@ -0,0 +1,19 @@
1
+ <\%= turbo_frame_tag "<%= plural %>-list" do %>
2
+ <div class="container-fluid "><%= singular %>-table
3
+ <div class="row">
4
+ <%= list_column_headings %>
5
+ <div class='col buttons-col'></div>
6
+ </div>
7
+
8
+ <\% if <%= plural %>.empty? %>
9
+ <div>
10
+ None
11
+ </div>
12
+ <\% end %>
13
+ <\% <%= plural %>.each do |<%= singular %>| %>
14
+ <\%= render partial: '<%= line_path_partial %>', locals: {<%= singular %>: <%= singular %> <%= nested_assignments_with_leading_comma %> } %>
15
+ <\% end %>
16
+
17
+ <%= @no_paginate ? "" : paginate %>
18
+ </div>
19
+ <\% end %>
@@ -0,0 +1,3 @@
1
+ <\%= turbo_frame_tag "<%= singular %>-new" do %>
2
+ <\%= link_to "New <%= singular.titlecase %>", <%= new_path_name %>, disable_with: "Loading...", class: "new-<%= singular %>-button btn btn-primary pull-right" %>
3
+ <\% end %>
@@ -0,0 +1,8 @@
1
+ <\%= turbo_frame_tag "<%= singular %>-new" do %>
2
+ <h3>
3
+ New <%= singular.titlecase %>
4
+ </h3>
5
+ <\%= form_with model: <%= singular %>, url: <%= path_helper_plural %>(<%= nested_objects_arity %>), method: "post" do |f| %>
6
+ <\%= render partial: "<%= namespace_with_slash + @plural %>/form", locals: { <%= singular %>: <%= singular %>, f: f} %>
7
+ <\% end %>
8
+ <\% end %>