hot-glue 0.4.3 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,20 @@
1
1
  module HotGlue
2
2
  class ErbTemplate < TemplateBase
3
3
 
4
+
5
+
6
+ def add_spaces_each_line(text, num_spaces)
7
+ add_spaces = " " * num_spaces
8
+ text.lines.collect{|line| add_spaces + line}.join("")
9
+ end
10
+
11
+
4
12
  # include GeneratorHelper
5
13
  attr_accessor :singular
6
14
 
7
15
  def field_output(col, type = nil, width, col_identifier )
8
16
  " <%= f.text_field :#{col.to_s}, value: @#{@singular}.#{col.to_s}, autocomplete: 'off', size: #{width}, class: 'form-control', type: '#{type}' %>\n "+
9
- " <label class='form-text' >#{col.to_s.humanize}</label>\n"
17
+ "\n"
10
18
  end
11
19
 
12
20
 
@@ -15,11 +23,12 @@ module HotGlue
15
23
  path_helper_args = args[0][:path_helper_args]
16
24
  singular = args[0][:singular]
17
25
  magic_buttons = args[0][:magic_buttons]
26
+ small_buttons = args[0][:small_buttons]
18
27
 
19
28
  magic_buttons.collect{ |button_name|
20
- "<%= form_with model: #{singular}, url: #{path_helper_singular}(#{path_helper_args}) do |f| %>
29
+ "<%= form_with model: #{singular}, url: #{path_helper_singular}(#{path_helper_args}), html: {style: 'display: inline', data: {\"turbo-confirm\": 'Are you sure you want to #{button_name} this #{singular}?'}} do |f| %>
21
30
  <%= f.hidden_field :#{button_name}, value: \"#{button_name}\" %>
22
- <%= f.submit '#{button_name.titleize}'.html_safe, disabled: (#{singular}.respond_to?(:#{button_name}able?) && ! #{singular}.#{button_name}able? ), data: {confirm: 'Are you sure you want to #{button_name} this #{singular}?'}, class: '#{singular}-button btn btn-primary ' %>
31
+ <%= f.submit '#{button_name.titleize}'.html_safe, disabled: (#{singular}.respond_to?(:#{button_name}able?) && ! #{singular}.#{button_name}able? ), class: '#{singular}-button btn btn-primary #{"btn-sm" if small_buttons}' %>
23
32
  <% end %>"
24
33
  }.join("\n")
25
34
  end
@@ -30,8 +39,7 @@ module HotGlue
30
39
  lines = 5
31
40
  end
32
41
 
33
- "<%= f.text_area :#{col.to_s}, class: 'form-control', autocomplete: 'off', cols: 40, rows: '#{lines}' %>" +
34
- "<label class='form-text'>#{col.to_s.humanize}</label>"
42
+ "<%= f.text_area :#{col.to_s}, class: 'form-control', autocomplete: 'off', cols: 40, rows: '#{lines}' %>"
35
43
  end
36
44
 
37
45
  def list_column_headings(*args)
@@ -58,23 +66,20 @@ module HotGlue
58
66
  show_only = args[0][:show_only]
59
67
  singular_class = args[0][:singular_class]
60
68
  col_identifier = args[0][:col_identifier]
69
+ ownership_field = args[0][:ownership_field]
61
70
 
62
71
 
63
72
  # TODO: CLEAN ME
64
73
  @singular = args[0][:singular]
65
74
  singular = @singular
66
75
 
67
- col_spaces_prepend = " "
68
-
69
-
70
76
  result = layout_columns.map{ |column|
71
- "<div class='#{col_identifier}' >" +
77
+ " <div class='#{col_identifier}' >" +
72
78
 
73
79
  column.map { |col|
74
80
 
75
81
  field_result = if show_only.include?(col)
76
- "<%= @#{singular}.#{col.to_s} %>" +
77
- "<label class='form-text'>#{col.to_s.humanize}</label>"
82
+ "<%= @#{singular}.#{col.to_s} %>"
78
83
  else
79
84
  type = eval("#{singular_class}.columns_hash['#{col}']").type
80
85
  limit = eval("#{singular_class}.columns_hash['#{col}']").limit
@@ -87,16 +92,18 @@ module HotGlue
87
92
  assoc_name = col.to_s.gsub("_id","")
88
93
  assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
89
94
  if assoc.nil?
90
- exit_message= "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
95
+ exit_message = "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
91
96
  exit
92
97
  end
98
+
99
+ is_owner = col.to_s == ownership_field
93
100
  display_column = HotGlue.derrive_reference_name(assoc.class_name)
94
- "<%= f.collection_select(:#{col.to_s}, #{assoc.class_name}.all, :id, :#{display_column}, {prompt: true, selected: @#{singular}.#{col.to_s} }, class: 'form-control') %>
95
- <label class='small form-text text-muted'>#{col.to_s.humanize}</label>"
101
+
102
+ # TODO: add is_owner && check if this nested arg is optional
103
+ (is_owner ? "<% unless #{assoc_name} %>\n" : "") + " <%= f.collection_select(:#{col.to_s}, #{assoc.class_name}.all, :id, :#{display_column}, {prompt: true, selected: @#{singular}.#{col.to_s} }, class: 'form-control') %>\n" + (is_owner ? "<% end %>" : "")
96
104
 
97
105
  else
98
- "<%= f.text_field :#{col.to_s}, value: #{singular}.#{col.to_s}, class: 'form-control', size: 4, type: 'number' %>
99
- <label class='small form-text text-muted'>#{col.to_s.humanize}</label>"
106
+ "<%= f.text_field :#{col.to_s}, value: #{singular}.#{col.to_s}, class: 'form-control', size: 4, type: 'number' %>"
100
107
 
101
108
  end
102
109
  when :string
@@ -130,18 +137,22 @@ module HotGlue
130
137
  ""
131
138
  when :enum
132
139
  enum_type = eval("#{singular_class}.columns.select{|x| x.name == '#{col.to_s}'}[0].sql_type")
133
- "<%= f.collection_select(:#{col.to_s}, enum_to_collection_select( #{singular_class}.defined_enums['#{enum_type}']), :key, :value, {selected: @#{singular}.#{col.to_s} }, class: 'form-control') %>
134
- <label class='small form-text text-muted'>#{col.to_s.humanize}</label>"
135
- end
136
- end
140
+ "<%= f.collection_select(:#{col.to_s}, enum_to_collection_select( #{singular_class}.defined_enums['#{enum_type}']), :key, :value, {selected: @#{singular}.#{col.to_s} }, class: 'form-control') %>"
141
+ end
142
+
143
+ end
137
144
 
138
145
  if (type == :integer) && col.to_s.ends_with?("_id")
139
146
  field_error_name = col.to_s.gsub("_id","")
140
147
  else
141
148
  field_error_name = col.to_s
142
149
  end
143
- "<span class='<%= \"alert-danger\" if #{singular}.errors.details.keys.include?(:#{field_error_name}) %>' #{ 'style="display: inherit;"'} >" + field_result + "</span>"
144
- }.join("<br />\n") + "</div>"
150
+
151
+ add_spaces_each_line( "\n <span class='<%= \"alert-danger\" if #{singular}.errors.details.keys.include?(:#{field_error_name}) %>' #{ 'style="display: inherit;"'} >\n" +
152
+ add_spaces_each_line(field_result + "\n<label class='small form-text text-muted'>#{col.to_s.humanize}</label>", 4) +
153
+ "\n </span>\n <br />", 2)
154
+
155
+ }.join("") + "\n </div>"
145
156
  }.join("\n")
146
157
  return result
147
158
  end
@@ -41,7 +41,10 @@ module HotGlue
41
41
  class_option :singular, type: :string, default: nil
42
42
  class_option :plural, type: :string, default: nil
43
43
  class_option :singular_class, type: :string, default: nil
44
- class_option :nest, type: :string, default: ""
44
+ class_option :nest, type: :string, default: nil # DEPRECATED —— DO NOT USE
45
+ class_option :nested, type: :string, default: ""
46
+
47
+
45
48
  class_option :namespace, type: :string, default: nil
46
49
  class_option :auth, type: :string, default: nil
47
50
  class_option :auth_identifier, type: :string, default: nil
@@ -59,15 +62,18 @@ module HotGlue
59
62
  class_option :big_edit, type: :boolean, default: false
60
63
  class_option :show_only, type: :string, default: ""
61
64
 
62
- class_option :stimulus_syntax, type: :boolean, default: nil # TODO: rename to ujs_syntax and default to false
65
+ class_option :ujs_syntax, type: :boolean, default: nil
63
66
 
64
67
  class_option :downnest, type: :string, default: nil
65
68
  class_option :magic_buttons, type: :string, default: nil
69
+ class_option :small_buttons, type: :boolean, default: nil
70
+
66
71
  class_option :display_list_after_update, type: :boolean, default: false
67
72
  class_option :smart_layout, type: :boolean, default: false
68
73
  class_option :markup, type: :string, default: nil # deprecated -- use in app config instead
69
74
  class_option :layout, type: :string, default: nil # if used here it will override what is in the config
70
-
75
+ class_option :no_list_labels, type: :boolean, default: false
76
+ class_option :before_list_labels, type: :boolean, default: false
71
77
 
72
78
  def initialize(*meta_args)
73
79
  super
@@ -138,14 +144,34 @@ module HotGlue
138
144
  end
139
145
 
140
146
  args = meta_args[0]
147
+
148
+
141
149
  @singular = args.first.tableize.singularize # should be in form hello_world
142
150
  @plural = options['plural'] || @singular + "s" # supply to override; leave blank to use default
151
+ @namespace = options['namespace'] || nil
152
+
153
+
154
+ use_controller_name = plural.titleize.gsub(" ", "")
155
+
156
+ @controller_build_name = (( @namespace.titleize.gsub(" ","") + "::" if @namespace) || "") + use_controller_name + "Controller"
157
+ @controller_build_folder = use_controller_name.underscore
158
+ @controller_build_folder_singular = singular
159
+
160
+ if ! @controller_build_folder.ends_with?("s")
161
+ raise "can't build with controller name #{@controller_build_folder} because it doesn't end with an 's'"
162
+ end
163
+
143
164
  @auth = options['auth'] || "current_user"
144
165
  @auth_identifier = options['auth_identifier'] || (! @god && @auth.gsub("current_", "")) || nil
145
166
 
146
167
 
147
- @nest = (!options['nest'].empty? && options['nest']) || nil
148
- @namespace = options['namespace'] || nil
168
+
169
+ if options['nest']
170
+ raise "STOP: the flag --nest has been replaced with --nested; please re-run using the --nested flag"
171
+
172
+ end
173
+
174
+ @nested = (!options['nested'].empty? && options['nested']) || nil
149
175
 
150
176
  @singular_class = @singular.titleize.gsub(" ", "")
151
177
  @exclude_fields = []
@@ -164,7 +190,6 @@ module HotGlue
164
190
  @show_only += options['show_only'].split(",").collect(&:to_sym)
165
191
  end
166
192
 
167
-
168
193
  @god = options['god'] || options['gd'] || false
169
194
  @specs_only = options['specs_only'] || false
170
195
 
@@ -177,13 +202,12 @@ module HotGlue
177
202
 
178
203
  @no_edit = options['no_edit'] || false
179
204
  @no_list = options['no_list'] || false
180
-
205
+ @no_list_labels = options['no_list_labels'] || false
181
206
  @display_list_after_update = options['display_list_after_update'] || false
182
207
  @smart_layout = options['smart_layout']
183
208
 
184
209
 
185
210
  @container_name = @layout == "hotglue" ? "scaffold-container" : "container-fluid"
186
-
187
211
  @downnest = options['downnest'] || false
188
212
 
189
213
  @downnest_children = []
@@ -198,13 +222,18 @@ module HotGlue
198
222
  end
199
223
 
200
224
  # when in self auth, the object is the same as the authenticated object
225
+
201
226
  if @auth && auth_identifier == @singular
202
227
  @self_auth = true
203
228
  end
204
229
 
230
+ if @self_auth && !@no_create
231
+ raise "This controller appears to be the same as the authentication object but in this context you cannot build a new/create action; please re-run with --no-create flag"
232
+ end
233
+
205
234
  @nested_args = []
206
- if !@nest.nil?
207
- @nested_args = @nest.split("/")
235
+ if ! @nested.nil?
236
+ @nested_args = @nested.split("/")
208
237
  @nested_args_plural = {}
209
238
  @nested_args.each do |a|
210
239
  @nested_args_plural[a] = a + "s"
@@ -218,6 +247,9 @@ module HotGlue
218
247
  @magic_buttons = options['magic_buttons'].split(',')
219
248
  end
220
249
 
250
+
251
+ @small_buttons = options['small_buttons'] || false
252
+
221
253
  @build_update_action = !@no_edit || !@magic_buttons.empty?
222
254
  # if the magic buttons are present, build the update action anyway
223
255
 
@@ -236,16 +268,27 @@ module HotGlue
236
268
  end
237
269
  end
238
270
 
271
+
272
+ @ujs_syntax = options['ujs_syntax']
273
+
274
+ if !@ujs_syntax
275
+ @ujs_syntax = !defined?(Turbo::Engine)
276
+ end
239
277
  @reference_name = HotGlue.derrive_reference_name(singular_class)
240
278
 
241
279
  identify_object_owner
242
280
  setup_fields
243
281
 
282
+ if (@columns - @show_only - (@object_owner_sym.empty? ? [] : [@ownership_field.to_sym])).empty?
283
+ @no_field_form = true
284
+ end
285
+
286
+ buttons_width = ((!@no_edit && 1) || 0) + ((!@no_delete && 1) || 0) + @magic_buttons.count
287
+
244
288
  builder = HotGlue::Layout::Builder.new({
245
289
  include_setting: options['include'],
246
290
  downnest_children: @downnest_children,
247
- no_edit: @no_edit,
248
- no_delete: @no_delete,
291
+ buttons_width: buttons_width,
249
292
  columns: @columns,
250
293
  smart_layout: @smart_layout
251
294
  })
@@ -261,23 +304,19 @@ module HotGlue
261
304
  auth_assoc_field = auth_assoc + "_id" unless @god
262
305
  assoc = eval("#{singular_class}.reflect_on_association(:#{@object_owner_sym})")
263
306
 
307
+
264
308
  if assoc
265
309
  @ownership_field = assoc.name.to_s + "_id"
266
- elsif !@nest
310
+ elsif ! @nested_args.any?
267
311
  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."
268
312
 
269
313
  else
270
- if @god
271
- exit_message= "*** Oops: Gd mode could not find the association(#{@object_owner_sym}). Something is wrong."
314
+
315
+ if eval(singular_class + ".reflect_on_association(:#{@object_owner_sym.to_s})").nil? && !eval(singular_class + ".reflect_on_association(:#{@object_owner_sym.to_s.singularize})").nil?
316
+ exit_message = "*** Oops: you tried to nest #{singular_class} within a route for `#{@object_owner_sym}` but I can't find an association for this relationship. Did you mean `#{@object_owner_sym.to_s.singularize}` (singular) instead?"
272
317
  else
273
- @auth_check = eval(@auth_identifier.titleize)
274
- @nested_args.each do |arg|
275
- if ! @auth_check.reflect_on_association("#{arg}s".to_sym)
276
- exit_message = "*** Oops: your nesting chain does not have a association for #{arg}s on #{@auth_check} something is wrong."
277
- end
278
- end
318
+ exit_message = "*** Oops: Missing relationship from class #{singular_class} to :#{@object_owner_sym} maybe add `belongs_to :#{@object_owner_sym}` to #{singular_class}\n (If your user is called something else, pass with flag auth=current_X where X is the model for your auth object 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 flag). To make a controller that can read all records, specify with --god."
279
319
  end
280
- puts exit_message
281
320
  raise(HotGlue::Error, exit_message)
282
321
  end
283
322
  end
@@ -357,7 +396,7 @@ module HotGlue
357
396
  def copy_controller_and_spec_files
358
397
  @default_colspan = @columns.size
359
398
  unless @specs_only
360
- template "controller.rb.erb", File.join("#{'spec/dummy/' if Rails.env.test?}app/controllers#{namespace_with_dash}", "#{plural}_controller.rb")
399
+ template "controller.rb.erb", File.join("#{'spec/dummy/' if Rails.env.test?}app/controllers#{namespace_with_dash}", "#{@controller_build_folder}_controller.rb")
361
400
  if @namespace
362
401
  begin
363
402
  eval(controller_descends_from)
@@ -461,10 +500,7 @@ module HotGlue
461
500
 
462
501
 
463
502
  def controller_class_name
464
- res = ""
465
- res << @namespace.titleize + "::" if @namespace
466
- res << plural.titleize.gsub(" ", "") + "Controller"
467
- res
503
+ @controller_build_name
468
504
  end
469
505
 
470
506
  def singular_name
@@ -480,7 +516,7 @@ module HotGlue
480
516
  end
481
517
 
482
518
  def path_helper_args
483
- if @nested_args.any? && @nest
519
+ if @nested_args.any? && @nested
484
520
  [(@nested_args).collect{|a| "#{a}"} , singular].join(",")
485
521
  else
486
522
  singular
@@ -488,16 +524,16 @@ module HotGlue
488
524
  end
489
525
 
490
526
  def path_helper_singular
491
- if @nest
492
- "#{@namespace+"_" if @namespace}#{(@nested_args.join("_") + "_" if @nested_args.any?)}#{singular}_path"
527
+ if @nested
528
+ "#{@namespace+"_" if @namespace}#{(@nested_args.join("_") + "_" if @nested_args.any?)}#{@controller_build_folder_singular}_path"
493
529
  else
494
- "#{@namespace+"_" if @namespace}#{singular}_path"
530
+ "#{@namespace+"_" if @namespace}#{@controller_build_folder_singular}_path"
495
531
  end
496
532
  end
497
533
 
498
534
  def path_helper_plural
499
- if ! @nest
500
- "#{@namespace+"_" if @namespace}#{plural}_path"
535
+ if ! @nested
536
+ "#{@namespace+"_" if @namespace}#{@controller_build_folder}_path"
501
537
  else
502
538
  "#{@namespace+"_" if @namespace}#{(@nested_args.join("_") + "_" if @nested_args.any?)}#{plural}_path"
503
539
  end
@@ -505,26 +541,26 @@ module HotGlue
505
541
 
506
542
  def path_arity
507
543
  res = ""
508
- if @nested_args.any? && @nest
544
+ if @nested_args.any? && @nested
509
545
  res << nested_objects_arity + ", "
510
546
  end
511
547
  res << "@" + singular
512
548
  end
513
549
 
514
550
  def line_path_partial
515
- "#{@namespace+"/" if @namespace}#{plural}/line"
551
+ "#{@namespace+"/" if @namespace}#{@controller_build_folder}/line"
516
552
  end
517
553
 
518
554
  def show_path_partial
519
- "#{@namespace+"/" if @namespace}#{plural}/show"
555
+ "#{@namespace+"/" if @namespace}#{@controller_build_folder}/show"
520
556
  end
521
557
 
522
558
  def list_path_partial
523
- "#{@namespace+"/" if @namespace}#{plural}/list"
559
+ "#{@namespace+"/" if @namespace}#{@controller_build_folder}/list"
524
560
  end
525
561
 
526
562
  def new_path_name
527
- base = "new_#{@namespace+"_" if @namespace}#{(@nested_args.join("_") + "_") if @nested_args.any?}#{singular}_path"
563
+ base = "new_#{@namespace+"_" if @namespace}#{(@nested_args.join("_") + "_") if @nested_args.any?}#{@controller_build_folder_singular}_path"
528
564
  if @nested_args.any?
529
565
  base += "(" + @nested_args.collect { |arg|
530
566
  "#{arg}.id"
@@ -545,7 +581,7 @@ module HotGlue
545
581
 
546
582
  def nest_assignments_operator(top_level = false, leading_comma = false)
547
583
  if @nested_args.any?
548
- "#{', ' + "\n " if leading_comma}#{top_level ? nested_assignments_top_level : nested_assignments }"
584
+ "#{", " if leading_comma}#{top_level ? nested_assignments_top_level : nested_assignments }"
549
585
  else
550
586
  ""
551
587
  end
@@ -617,7 +653,8 @@ module HotGlue
617
653
  path_helper_singular: path_helper_singular,
618
654
  path_helper_args: path_helper_args,
619
655
  singular: singular,
620
- magic_buttons: @magic_buttons
656
+ magic_buttons: @magic_buttons,
657
+ small_buttons: @small_buttons
621
658
  )
622
659
  end
623
660
 
@@ -630,7 +667,7 @@ module HotGlue
630
667
 
631
668
 
632
669
  dest_filepath = File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}",
633
- plural, dest_filename)
670
+ @controller_build_folder, dest_filename)
634
671
 
635
672
 
636
673
  template source_filename, dest_filepath
@@ -644,7 +681,7 @@ module HotGlue
644
681
  source_filename = cc_filename_with_extensions( "#{@markup}/#{view}.turbo_stream.#{@markup}")
645
682
  dest_filename = cc_filename_with_extensions("#{view}", "turbo_stream.#{@markup}")
646
683
  dest_filepath = File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}",
647
- plural, dest_filename)
684
+ @controller_build_folder, dest_filename)
648
685
 
649
686
 
650
687
  template source_filename, dest_filepath
@@ -652,6 +689,15 @@ module HotGlue
652
689
 
653
690
  end
654
691
  end
692
+
693
+ # menu_file = "app/views#{namespace_with_dash}/menu.erb"
694
+ #
695
+ # if File.exists?(menu_file)
696
+ # # TODO: can I insert the new menu item into the menu programatically here?
697
+ # # not sure how i would acheive this without nokogiri
698
+ #
699
+ # end
700
+
655
701
  end
656
702
 
657
703
  def namespace_with_dash
@@ -729,7 +775,8 @@ module HotGlue
729
775
  show_only: @show_only,
730
776
  singular_class: singular_class,
731
777
  singular: singular,
732
- col_identifier: col_identifier
778
+ col_identifier: col_identifier,
779
+ ownership_field: @ownership_field
733
780
  )
734
781
  end
735
782
 
@@ -770,8 +817,8 @@ module HotGlue
770
817
  end
771
818
 
772
819
  def controller_descends_from
773
- if defined?(@namespace.titlecase + "::BaseController")
774
- @namespace.titlecase + "::BaseController"
820
+ if defined?(@namespace.titlecase.gsub(" ", "") + "::BaseController")
821
+ @namespace.titlecase.gsub(" ", "") + "::BaseController"
775
822
  else
776
823
  "ApplicationController"
777
824
  end
@@ -825,13 +872,13 @@ module HotGlue
825
872
  @template_builder.paginate(plural: plural)
826
873
  end
827
874
 
828
- def delete_confirmation_syntax
829
- if !@stimulus_syntax
830
- "{confirm: 'Are you sure?'}"
831
- else
832
- "{controller: 'confirmable', 'confirm-message': \"Are you sure you want to delete \#{ #{@singular}.#{ display_class } } \", 'action': 'confirmation#confirm'}"
833
- end
834
- end
875
+ # def delete_confirmation_syntax
876
+ # if !@stimulus_syntax
877
+ # "{confirm: 'Are you sure?'}"
878
+ # else
879
+ # "{controller: 'confirmable', 'confirm-message': \"Are you sure you want to delete \#{ #{@singular}.#{ display_class } } \", 'action': 'confirmation#confirm'}"
880
+ # end
881
+ # end
835
882
 
836
883
 
837
884
  def controller_magic_button_update_actions
@@ -4,50 +4,51 @@ class <%= controller_class_name %> < <%= controller_descends_from %>
4
4
 
5
5
  <% unless @auth_identifier == '' || @god %>before_action :authenticate_<%= @auth_identifier %>!<% end %>
6
6
  <% if any_nested? %><% nest_chain = [] %> <% @nested_args.each { |arg|
7
- this_scope = nest_chain.empty? ? "#{@auth ? auth_object : class_name}.#{arg}s" : "#{nest_chain.last}.#{arg}s"
8
- nest_chain << arg %>
7
+
8
+ if auth_identifier == arg
9
+ this_scope = auth_object
10
+ elsif nest_chain.empty?
11
+ this_scope = "#{@auth ? auth_object : class_name}.#{arg}s"
12
+ else
13
+ this_scope = "#{nest_chain.last}.#{arg}s"
14
+ end
15
+
16
+ nest_chain << arg %>
9
17
  before_action :<%= arg %>
10
18
  <% } %><% end %>
11
19
  before_action :load_<%= singular_name %>, only: [:show, :edit, :update, :destroy]
12
20
  after_action -> { flash.discard }, if: -> { request.format.symbol == :turbo_stream }
13
-
14
- <% if no_devise_installed %>
15
- # TODO: implement current_user or use Devise
16
- <% end %>
17
-
18
- <% if @nested_args.any? %>
19
- def <%= @nested_args[0] %>
20
- <% if @god
21
- next_object = nil
22
- collect_objects = @nested_args.reverse.collect {|x|
23
- if eval("#{next_object || class_name}.reflect_on_association(:#{x})").nil?
24
- raise "***** Unable to find the association `#{x}` on the class #{next_object || class_name} ..... you probably want to add `belongs_to :#{x}` to the #{next_object || class_name} object?"
25
- end
26
- next_object = eval("#{next_object || class_name}.reflect_on_association(:#{x})").class_name
27
- }
28
- root_object = collect_objects.last
21
+ <% if @nested_args.any? %>
22
+ def <%= @nested_args[0] %><% if @god
23
+ next_object = nil
24
+ collect_objects = @nested_args.reverse.collect {|x|
25
+ if eval("#{next_object || class_name}.reflect_on_association(:#{x})").nil?
26
+ raise "***** Unable to find the association `#{x}` on the class #{next_object || class_name} ..... you probably want to add `belongs_to :#{x}` to the #{next_object || class_name} object?"
27
+ end
28
+ next_object = eval("#{next_object || class_name}.reflect_on_association(:#{x})").class_name
29
+ }
30
+ root_object = collect_objects.last
29
31
  else
30
- root_object = @auth + "." + @nested_args[0] + "s"
32
+ if @nested_args[0] == @auth_identifier
33
+ root_object = @auth
34
+ else
35
+ root_object = @auth + "." + @nested_args[0] + "s"
36
+ end
31
37
  end
32
-
33
- %>
34
- <% if !@god %> @<%= @nested_args[0] %> ||= <%= root_object %>.find(params[:<%= @nested_args[0] %>_id])
35
- <% else %> @<%= @nested_args[0] %> ||= <%= root_object %>.find(params[:<%= @nested_args[0] %>_id]) <% end %>
38
+ %><% if !@god && @nested_args[0] == @auth_identifier %>
39
+ @<%= @nested_args[0] %> ||= <%= root_object %> <% elsif !@god %>
40
+ @<%= @nested_args[0] %> ||= <%= root_object %>.find(params[:<%= @nested_args[0] %>_id]) <% else %>
41
+ @<%= @nested_args[0] %> ||= <%= root_object %>.find(params[:<%= @nested_args[0] %>_id]) <% end %>
36
42
  end
37
43
  <% end %>
38
-
39
44
  <% if any_nested? %><% nest_chain = [@nested_args[0]]; this_scope = @nested_args[0] + 's'; %> <% @nested_args[1..-1].each { |arg|
40
45
  this_scope = "#{nest_chain.last}.#{arg}s"
41
46
  nest_chain << arg
42
47
  %>
43
48
  def <%= arg %>
44
49
  @<%= arg %> ||= <%= this_scope %>.find(params[:<%= arg %>_id])
45
- end<% } %>
46
-
47
- <% end %>
48
-
50
+ end<% } %> <% end %> <% if !@self_auth %>
49
51
 
50
- <% if !@self_auth %>
51
52
  def load_<%= singular_name %>
52
53
  @<%= singular_name %> = <%= object_scope.gsub("@",'') %>.find(params[:id])
53
54
  end
@@ -57,7 +58,11 @@ class <%= controller_class_name %> < <%= controller_descends_from %>
57
58
  end<% end %>
58
59
 
59
60
  def load_all_<%= plural %>
61
+ <% if !@self_auth %>
60
62
  @<%= plural_name %> = <%= object_scope.gsub("@",'') %>.page(params[:page])
63
+ <% else %>
64
+ @<%= plural_name %> = <%= class_name %>.where(id: <%= auth_object.gsub("@",'') %>.id) # returns iterable even though this <%= singular_name %> is anly allowed access to themselves
65
+ <% end %>
61
66
  end
62
67
 
63
68
  def index
@@ -67,10 +72,11 @@ class <%= controller_class_name %> < <%= controller_descends_from %>
67
72
  end
68
73
  end
69
74
 
70
- <% if create_action %> def new <% if ! @god %>
75
+ <% if create_action %> def new
76
+ <% if ! @god %>
71
77
  @<%= singular_name %> = <%= class_name %>.new(<%= @object_owner_sym %>: <%= @object_owner_eval %>)
72
78
  <% else %>
73
- @<%= singular_name %> = <%= class_name %>.new
79
+ @<%= singular_name %> = <%= class_name %>.new(<% if any_nested? %><%= @object_owner_sym %>: <%= @object_owner_eval %><% end %>)
74
80
  <% end %>
75
81
  respond_to do |format|
76
82
  format.html
@@ -78,8 +84,8 @@ class <%= controller_class_name %> < <%= controller_descends_from %>
78
84
  end
79
85
 
80
86
  def create
81
- modified_params = modify_date_inputs_on_params(<%=singular_name %>_params.dup<% if !@object_owner_sym.empty? %>.merge!(<%= @object_owner_sym %>: <%= @object_owner_eval %> )<% end %> <%= @auth ? ', ' + @auth : '' %>)
82
- @<%=singular_name %> = <%=class_name %>.create(modified_params)
87
+ modified_params = modify_date_inputs_on_params(<%= singular_name %>_params.dup<% if ! @object_owner_sym.empty? %>.merge!(<%= @object_owner_sym %>: <%= @object_owner_eval %> )<% end %> <%= @auth ? ', ' + @auth : '' %>)
88
+ @<%=singular_name %> = <%= class_name %>.create(modified_params)
83
89
 
84
90
  if @<%= singular_name %>.save
85
91
  flash[:notice] = "Successfully created #{@<%= singular %>.<%= display_class %>}"
@@ -1,8 +1,9 @@
1
1
  <div class="row">
2
- <%= all_form_fields %>
2
+ <%= all_form_fields %>
3
3
 
4
4
  <div class="<%= @layout == "hotglue" ? 'scaffold-cell' : 'col-md-2' %>">
5
- <\%= link_to "Cancel", <%= path_helper_plural %>, {class: "btn btn-secondary"} %>
5
+ <\%= link_to "Cancel", <%= path_helper_plural %>, {class: "btn btn-secondary"} %><% if @no_field_form %>
6
+ <\%= f.hidden_field "_________" %><% end %>
6
7
  <\%= f.submit "Save", class: "btn btn-primary pull-right" %>
7
8
  </div>
8
9
  </div>