hot-glue 0.0.9 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +21 -18
- data/README.md +83 -50
- data/app/helpers/hot_glue/controller_helper.rb +1 -1
- data/db/migrate/20210306223305_create_ghis.rb +9 -0
- data/db/schema.rb +1 -1
- data/lib/generators/hot_glue/install_generator.rb +0 -2
- data/lib/generators/hot_glue/markup_templates/base.rb +7 -0
- data/lib/generators/hot_glue/markup_templates/erb.rb +228 -0
- data/lib/generators/hot_glue/markup_templates/haml.rb +223 -0
- data/lib/generators/hot_glue/markup_templates/slim.rb +9 -0
- data/lib/generators/hot_glue/scaffold_generator.rb +162 -265
- data/lib/generators/hot_glue/templates/controller.rb.erb +24 -10
- data/lib/generators/hot_glue/templates/erb/_errors.erb +11 -0
- data/lib/generators/hot_glue/templates/erb/_flash_notices.erb +12 -0
- data/lib/generators/hot_glue/templates/erb/_form.erb +8 -0
- data/lib/generators/hot_glue/templates/erb/_line.erb +10 -0
- data/lib/generators/hot_glue/templates/erb/_list.erb +19 -0
- data/lib/generators/hot_glue/templates/erb/_new_button.erb +3 -0
- data/lib/generators/hot_glue/templates/erb/_new_form.erb +8 -0
- data/lib/generators/hot_glue/templates/erb/_show.erb +8 -0
- data/lib/generators/hot_glue/templates/erb/create.turbo_stream.erb +18 -0
- data/lib/generators/hot_glue/templates/erb/destroy.turbo_stream.erb +3 -0
- data/lib/generators/hot_glue/templates/erb/edit.erb +30 -0
- data/lib/generators/hot_glue/templates/erb/edit.turbo_stream.erb +3 -0
- data/lib/generators/hot_glue/templates/erb/index.erb +10 -0
- data/lib/generators/hot_glue/templates/erb/new.erb +1 -0
- data/lib/generators/hot_glue/templates/erb/update.turbo_stream.erb +10 -0
- data/lib/generators/hot_glue/templates/{_errors.haml → haml/_errors.haml} +0 -0
- data/lib/generators/hot_glue/templates/{_flash_notices.haml → haml/_flash_notices.haml} +0 -0
- data/lib/generators/hot_glue/templates/{_form.haml → haml/_form.haml} +1 -0
- data/lib/generators/hot_glue/templates/{_line.haml → haml/_line.haml} +0 -0
- data/lib/generators/hot_glue/templates/{_list.haml → haml/_list.haml} +0 -0
- data/lib/generators/hot_glue/templates/{_new_button.haml → haml/_new_button.haml} +0 -0
- data/lib/generators/hot_glue/templates/{_new_form.haml → haml/_new_form.haml} +0 -3
- data/lib/generators/hot_glue/templates/haml/_show.haml +7 -0
- data/lib/generators/hot_glue/templates/{create.turbo_stream.haml → haml/create.turbo_stream.haml} +0 -0
- data/lib/generators/hot_glue/templates/{destroy.turbo_stream.haml → haml/destroy.turbo_stream.haml} +0 -0
- data/lib/generators/hot_glue/templates/{edit.haml → haml/edit.haml} +0 -0
- data/lib/generators/hot_glue/templates/{edit.turbo_stream.haml → haml/edit.turbo_stream.haml} +0 -0
- data/lib/generators/hot_glue/templates/{index.haml → haml/index.haml} +0 -0
- data/lib/generators/hot_glue/templates/{new.haml → haml/new.haml} +0 -0
- data/lib/generators/hot_glue/templates/haml/update.turbo_stream.haml +9 -0
- data/lib/generators/hot_glue/templates/system_spec.rb.erb +109 -14
- data/lib/hot-glue.rb +6 -20
- data/lib/hotglue/version.rb +1 -1
- metadata +38 -20
- data/db/migrate/20210306223305_create_hgis.rb +0 -9
- data/lib/generators/hot_glue/templates/_show.haml +0 -7
- data/lib/generators/hot_glue/templates/request_spec.rb.erb +0 -110
- data/lib/generators/hot_glue/templates/update.turbo_stream.haml +0 -5
@@ -1,15 +1,37 @@
|
|
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
|
-
|
7
|
-
|
8
10
|
class Error < StandardError
|
9
11
|
end
|
10
12
|
|
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
|
+
|
13
35
|
def text_area_output(col, field_length, col_identifier )
|
14
36
|
lines = field_length % 40
|
15
37
|
if lines > 5
|
@@ -24,13 +46,7 @@ module HotGlue
|
|
24
46
|
|
25
47
|
|
26
48
|
|
27
|
-
def field_output(col, type = nil, width, col_identifier )
|
28
49
|
|
29
|
-
"#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
|
30
|
-
= f.text_field :#{col.to_s}, value: @#{singular}.#{col.to_s}, size: #{width}, class: 'form-control', type: '#{type}'
|
31
|
-
%label.form-text
|
32
|
-
#{col.to_s.humanize}\n"
|
33
|
-
end
|
34
50
|
end
|
35
51
|
|
36
52
|
|
@@ -62,12 +78,23 @@ module HotGlue
|
|
62
78
|
class_option :no_paginate, type: :boolean, default: false
|
63
79
|
class_option :big_edit, type: :boolean, default: false
|
64
80
|
class_option :show_only, type: :string, default: ""
|
81
|
+
class_option :markup, type: :string, default: "erb"
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
# def erb_replace_ampersands
|
86
|
+
# if @template_builder.is_a?(HotGlue::ErbTemplate)
|
87
|
+
# @output_buffer.gsub!('\%', '%')
|
88
|
+
# end
|
89
|
+
# end
|
90
|
+
|
65
91
|
|
66
|
-
|
92
|
+
|
93
|
+
def initialize(*meta_args)
|
67
94
|
super
|
68
95
|
|
69
96
|
begin
|
70
|
-
|
97
|
+
@the_object = eval(class_name)
|
71
98
|
rescue StandardError => e
|
72
99
|
message = "*** Oops: It looks like there is no object for #{class_name}. Please define the object + database table first."
|
73
100
|
raise(HotGlue::Error, message)
|
@@ -77,14 +104,29 @@ module HotGlue
|
|
77
104
|
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.")
|
78
105
|
end
|
79
106
|
|
107
|
+
if options['markup'] == "erb"
|
108
|
+
@template_builder = HotGlue::ErbTemplate.new
|
109
|
+
elsif options['markup'] == "slim"
|
110
|
+
puts "SLIM IS NOT IMPLEMENTED; please see https://github.com/jasonfb/hot-glue/issues/3"
|
111
|
+
abort
|
112
|
+
@template_builder = HotGlue::SlimTemplate.new
|
113
|
+
|
114
|
+
elsif options['markup'] == "haml"
|
115
|
+
@template_builder = HotGlue::HamlTemplate.new
|
116
|
+
end
|
117
|
+
@markup = options['markup']
|
118
|
+
|
80
119
|
|
81
120
|
args = meta_args[0]
|
82
121
|
@singular = args.first.tableize.singularize # should be in form hello_world
|
83
122
|
@plural = options['plural'] || @singular + "s" # supply to override; leave blank to use default
|
84
123
|
@auth = options['auth'] || "current_user"
|
85
|
-
@auth_identifier = options['
|
86
|
-
|
124
|
+
@auth_identifier = options['auth_identifier'] || (!@auth.nil? && @auth.gsub("current_", "")) || nil
|
125
|
+
|
126
|
+
|
127
|
+
@nest = (!options['nest'].empty? && options['nest']) || nil
|
87
128
|
@namespace = options['namespace'] || nil
|
129
|
+
|
88
130
|
@singular_class = @singular.titleize.gsub(" ", "")
|
89
131
|
@exclude_fields = []
|
90
132
|
@exclude_fields += options['exclude'].split(",").collect(&:to_sym)
|
@@ -100,7 +142,6 @@ module HotGlue
|
|
100
142
|
@show_only += options['show_only'].split(",").collect(&:to_sym)
|
101
143
|
end
|
102
144
|
|
103
|
-
auth_assoc = @auth.gsub("current_","")
|
104
145
|
|
105
146
|
@god = options['god'] || options['gd'] || false
|
106
147
|
@specs_only = options['specs_only'] || false
|
@@ -147,22 +188,46 @@ module HotGlue
|
|
147
188
|
end
|
148
189
|
end
|
149
190
|
|
191
|
+
identify_object_owner
|
192
|
+
setup_fields
|
193
|
+
end
|
194
|
+
|
195
|
+
def identify_object_owner
|
196
|
+
auth_assoc = @auth && @auth.gsub("current_","")
|
150
197
|
|
151
198
|
if !@object_owner_sym.empty?
|
152
|
-
auth_assoc_field = auth_assoc + "_id"
|
199
|
+
auth_assoc_field = auth_assoc + "_id" unless @god
|
153
200
|
assoc = eval("#{singular_class}.reflect_on_association(:#{@object_owner_sym})")
|
154
201
|
|
155
202
|
if assoc
|
156
|
-
ownership_field = assoc.name.to_s + "_id"
|
203
|
+
@ownership_field = assoc.name.to_s + "_id"
|
204
|
+
elsif !@nest
|
205
|
+
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."
|
206
|
+
|
157
207
|
else
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
208
|
+
if @god
|
209
|
+
|
210
|
+
exit_message= "*** Oops: god mode could not find the association(?). something is wrong."
|
211
|
+
else
|
212
|
+
@auth_check = "current_user"
|
213
|
+
@nested_args.each do |arg|
|
214
|
+
|
215
|
+
if !@auth_check.method("#{arg}s")
|
216
|
+
exit_message= "*** Oops: your nesting chain does not have a assocation for #{arg}s on #{@auth_check} something is wrong."
|
217
|
+
end
|
218
|
+
byebug
|
219
|
+
puts ""
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
163
223
|
raise(HotGlue::Error, exit_message)
|
164
224
|
end
|
165
225
|
end
|
226
|
+
end
|
227
|
+
|
228
|
+
|
229
|
+
def setup_fields
|
230
|
+
auth_assoc = @auth && @auth.gsub("current_","")
|
166
231
|
|
167
232
|
if !@include_fields
|
168
233
|
@exclude_fields.push :id, :created_at, :updated_at, :encrypted_password,
|
@@ -171,15 +236,14 @@ module HotGlue
|
|
171
236
|
:confirmation_token, :confirmed_at,
|
172
237
|
:confirmation_sent_at, :unconfirmed_email
|
173
238
|
|
174
|
-
@exclude_fields.push(
|
175
|
-
@exclude_fields.push(ownership_field.to_sym) if !ownership_field.nil?
|
239
|
+
@exclude_fields.push( (auth_assoc + "_id").to_sym) if ! auth_assoc.nil?
|
240
|
+
@exclude_fields.push( @ownership_field.to_sym ) if ! @ownership_field.nil?
|
176
241
|
|
177
242
|
|
178
|
-
@columns =
|
243
|
+
@columns = @the_object.columns.map(&:name).map(&:to_sym).reject{|field| @exclude_fields.include?(field) }
|
179
244
|
|
180
245
|
else
|
181
|
-
@columns =
|
182
|
-
|
246
|
+
@columns = @the_object.columns.map(&:name).map(&:to_sym).reject{|field| !@include_fields.include?(field) }
|
183
247
|
end
|
184
248
|
|
185
249
|
@columns.each do |col|
|
@@ -187,7 +251,7 @@ module HotGlue
|
|
187
251
|
@show_only << col
|
188
252
|
end
|
189
253
|
|
190
|
-
if
|
254
|
+
if @the_object.columns_hash[col.to_s].type == :integer
|
191
255
|
if col.to_s.ends_with?("_id")
|
192
256
|
# guess the association name label
|
193
257
|
assoc_name = col.to_s.gsub("_id","")
|
@@ -197,7 +261,7 @@ module HotGlue
|
|
197
261
|
begin
|
198
262
|
eval(assoc.class_name)
|
199
263
|
rescue NameError => e
|
200
|
-
exit_message = "*** Oops: The
|
264
|
+
exit_message = "*** Oops: The model #{singular_class} is missing an association for #{assoc_name} or the model doesn't exist. TODO: Please implement a model for #{assoc_name.titlecase}; your model #{singular_class.titlecase} should have_many :#{assoc_name}s. To make a controller that can read all records, specify with --god."
|
201
265
|
raise(HotGlue::Error, exit_message)
|
202
266
|
|
203
267
|
end
|
@@ -226,8 +290,6 @@ module HotGlue
|
|
226
290
|
end
|
227
291
|
end
|
228
292
|
|
229
|
-
|
230
|
-
|
231
293
|
#
|
232
294
|
def formats
|
233
295
|
[format]
|
@@ -242,22 +304,26 @@ module HotGlue
|
|
242
304
|
|
243
305
|
unless @specs_only
|
244
306
|
template "controller.rb.erb", File.join("#{'spec/dummy/' if Rails.env.test?}app/controllers#{namespace_with_dash}", "#{plural}_controller.rb")
|
245
|
-
if @namespace
|
246
|
-
|
307
|
+
if @namespace
|
308
|
+
begin
|
309
|
+
eval(controller_descends_from)
|
310
|
+
puts " skipping base controller #{controller_descends_from}"
|
311
|
+
rescue NameError => e
|
312
|
+
template "base_controller.rb.erb", File.join("#{'spec/dummy/' if Rails.env.test?}app/controllers#{namespace_with_dash}", "base_controller.rb")
|
313
|
+
end
|
247
314
|
end
|
248
315
|
end
|
249
316
|
|
250
317
|
unless @no_specs
|
251
|
-
template "
|
252
|
-
template "system_spec.rb.erb", File.join("#{'spec/dummy/' if Rails.env.test?}spec/system#{namespace_with_dash}", "#{plural}_spec.rb")
|
253
|
-
|
318
|
+
template "system_spec.rb.erb", File.join("#{'spec/dummy/' if Rails.env.test?}spec/system#{namespace_with_dash}", "#{plural}_behavior_spec.rb")
|
254
319
|
end
|
255
320
|
|
256
|
-
template "_errors
|
321
|
+
template "#{@markup}/_errors.#{@markup}", File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}", "_errors.#{@markup}")
|
257
322
|
end
|
258
323
|
|
259
324
|
def list_column_headings
|
260
|
-
@
|
325
|
+
@template_builder.list_column_headings(columns: @columns)
|
326
|
+
|
261
327
|
end
|
262
328
|
|
263
329
|
def columns_spec_with_sample_data
|
@@ -328,11 +394,6 @@ module HotGlue
|
|
328
394
|
@auth_identifier
|
329
395
|
end
|
330
396
|
|
331
|
-
|
332
|
-
def path_helper_full
|
333
|
-
"#{@namespace+"_" if @namespace}#{(@nested_args.join("_") + "_" if @nested_args.any?)}#{singular}_path"
|
334
|
-
end
|
335
|
-
|
336
397
|
def path_helper_args
|
337
398
|
if @nested_args.any?
|
338
399
|
[(@nested_args).collect{|a| "@#{a}"} , singular].join(",")
|
@@ -370,7 +431,14 @@ module HotGlue
|
|
370
431
|
end
|
371
432
|
|
372
433
|
def new_path_name
|
373
|
-
|
434
|
+
|
435
|
+
base = "new_#{@namespace+"_" if @namespace}#{(@nested_args.join("_") + "_") if @nested_args.any?}#{singular}_path"
|
436
|
+
if @nested_args.any?
|
437
|
+
base += "(" + @nested_args.collect { |arg|
|
438
|
+
"@#{arg}.id"
|
439
|
+
}.join(", ") + ")"
|
440
|
+
|
441
|
+
end
|
374
442
|
end
|
375
443
|
|
376
444
|
def nested_assignments
|
@@ -425,37 +493,60 @@ module HotGlue
|
|
425
493
|
end
|
426
494
|
|
427
495
|
def all_objects_variable
|
428
|
-
|
429
|
-
# needs the authenticated root user
|
430
|
-
# "#{@auth}.#{ @nested_args.map{|a| "#{@nested_args_plural[a]}.find(@#{a})"}.join('.') + "." if @nested_args.any?}#{plural}"
|
431
|
-
|
432
496
|
all_objects_root + ".page(params[:page])"
|
433
|
-
|
434
497
|
end
|
435
498
|
|
436
499
|
def auth_object
|
437
500
|
@auth
|
438
501
|
end
|
439
502
|
|
440
|
-
|
441
503
|
def no_devise_installed
|
442
504
|
!Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }['devise']
|
443
505
|
end
|
444
506
|
|
507
|
+
# def erb_replace_ampersands!(filename = nil)
|
508
|
+
#
|
509
|
+
# return if filename.nil?
|
510
|
+
# file = File.open(filename, "r")
|
511
|
+
# contents = file.read
|
512
|
+
# file.close
|
513
|
+
#
|
514
|
+
# file = File.open(filename, "w")
|
515
|
+
# file.write( contents.gsub('\%', '%'))
|
516
|
+
# file.close
|
517
|
+
# end
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
|
445
522
|
|
446
523
|
def copy_view_files
|
447
524
|
return if @specs_only
|
448
|
-
|
525
|
+
all_views.each do |view|
|
449
526
|
formats.each do |format|
|
450
|
-
|
451
|
-
|
527
|
+
source_filename = cc_filename_with_extensions("#{@markup}/#{view}", "#{@markup}")
|
528
|
+
dest_filename = cc_filename_with_extensions("#{view}", "#{@markup}")
|
529
|
+
dest_filepath = File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}",
|
530
|
+
controller_file_path, dest_filename)
|
531
|
+
|
532
|
+
|
533
|
+
template source_filename, dest_filepath
|
534
|
+
gsub_file dest_filepath, '\%', '%'
|
535
|
+
|
452
536
|
end
|
453
537
|
end
|
454
538
|
|
455
539
|
turbo_stream_views.each do |view|
|
456
540
|
formats.each do |format|
|
457
|
-
|
458
|
-
|
541
|
+
source_filename = cc_filename_with_extensions( "#{@markup}/#{view}.turbo_stream.#{@markup}")
|
542
|
+
dest_filename = cc_filename_with_extensions("#{view}", "turbo_stream.#{@markup}")
|
543
|
+
dest_filepath = File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}",
|
544
|
+
controller_file_path, dest_filename)
|
545
|
+
|
546
|
+
|
547
|
+
template source_filename, dest_filepath
|
548
|
+
gsub_file dest_filepath, '\%', '%'
|
549
|
+
|
459
550
|
end
|
460
551
|
end
|
461
552
|
end
|
@@ -476,7 +567,7 @@ module HotGlue
|
|
476
567
|
end
|
477
568
|
end
|
478
569
|
|
479
|
-
def
|
570
|
+
def all_views
|
480
571
|
res = %w(index edit _form _line _list _show _errors)
|
481
572
|
|
482
573
|
unless @no_create
|
@@ -508,212 +599,23 @@ module HotGlue
|
|
508
599
|
end
|
509
600
|
|
510
601
|
def all_form_fields
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
"#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
|
519
|
-
= @#{singular}.#{col.to_s}
|
520
|
-
%label.form-text
|
521
|
-
#{col.to_s.humanize}\n"
|
522
|
-
else
|
523
|
-
|
524
|
-
|
525
|
-
type = eval("#{singular_class}.columns_hash['#{col}']").type
|
526
|
-
limit = eval("#{singular_class}.columns_hash['#{col}']").limit
|
527
|
-
sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
|
528
|
-
|
529
|
-
case type
|
530
|
-
when :integer
|
531
|
-
# look for a belongs_to on this object
|
532
|
-
if col.to_s.ends_with?("_id")
|
533
|
-
assoc_name = col.to_s.gsub("_id","")
|
534
|
-
assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
|
535
|
-
if assoc.nil?
|
536
|
-
exit_message= "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
|
537
|
-
exit
|
538
|
-
end
|
539
|
-
|
540
|
-
|
541
|
-
assoc_class = eval(assoc.class_name)
|
542
|
-
|
543
|
-
if assoc_class.respond_to?("name")
|
544
|
-
display_column = "name"
|
545
|
-
elsif assoc_class.respond_to?("to_label")
|
546
|
-
display_column = "to_label"
|
547
|
-
elsif assoc_class.respond_to?("full_name")
|
548
|
-
display_column = "full_name"
|
549
|
-
elsif assoc_class.respond_to?("display_name")
|
550
|
-
display_column = "display_name"
|
551
|
-
elsif assoc_class.respond_to?("email")
|
552
|
-
display_column = "email"
|
553
|
-
else
|
554
|
-
raise("this should have been caught by the checker in the initializer")
|
555
|
-
# 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)"
|
556
|
-
end
|
557
|
-
|
558
|
-
"#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{assoc_name.to_s})}\"}
|
559
|
-
#{col_spaces_prepend}= f.collection_select(:#{col.to_s}, #{assoc_class}.all, :id, :#{display_column}, {prompt: true, selected: @#{singular}.#{col.to_s} }, class: 'form-control')
|
560
|
-
#{col_spaces_prepend}%label.small.form-text.text-muted
|
561
|
-
#{col_spaces_prepend} #{col.to_s.humanize}"
|
562
|
-
|
563
|
-
else
|
564
|
-
"#{col_identifier}{class: \"form-group \#{'alert-danger' if @#{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
|
565
|
-
#{col_spaces_prepend}= f.text_field :#{col.to_s}, value: #{singular}.#{col.to_s}, class: 'form-control', size: 4, type: 'number'
|
566
|
-
#{col_spaces_prepend}%label.form-text
|
567
|
-
#{col_spaces_prepend} #{col.to_s.humanize}\n"
|
568
|
-
end
|
569
|
-
when :string
|
570
|
-
limit ||= 256
|
571
|
-
if limit <= 256
|
572
|
-
field_output(col, nil, limit, col_identifier)
|
573
|
-
else
|
574
|
-
text_area_output(col, limit, col_identifier)
|
575
|
-
end
|
576
|
-
|
577
|
-
when :text
|
578
|
-
limit ||= 256
|
579
|
-
if limit <= 256
|
580
|
-
field_output(col, nil, limit, col_identifier)
|
581
|
-
else
|
582
|
-
text_area_output(col, limit, col_identifier)
|
583
|
-
end
|
584
|
-
when :float
|
585
|
-
limit ||= 256
|
586
|
-
field_output(col, nil, limit, col_identifier)
|
587
|
-
|
588
|
-
|
589
|
-
when :datetime
|
590
|
-
"#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
|
591
|
-
#{col_spaces_prepend}= datetime_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{col.to_s.humanize}', #{@auth ? @auth+'.timezone' : 'nil'})"
|
592
|
-
when :date
|
593
|
-
"#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
|
594
|
-
#{col_spaces_prepend}= date_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{col.to_s.humanize}', #{@auth ? @auth+'.timezone' : 'nil'})"
|
595
|
-
when :time
|
596
|
-
"#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
|
597
|
-
#{col_spaces_prepend}= time_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{col.to_s.humanize}', #{@auth ? @auth+'.timezone' : 'nil'})"
|
598
|
-
when :boolean
|
599
|
-
"#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
|
600
|
-
#{col_spaces_prepend}%span
|
601
|
-
#{col_spaces_prepend} #{col.to_s.humanize}
|
602
|
-
#{col_spaces_prepend}= f.radio_button(:#{col.to_s}, '0', checked: #{singular}.#{col.to_s} ? '' : 'checked')
|
603
|
-
#{col_spaces_prepend}= f.label(:#{col.to_s}, value: 'No', for: '#{singular}_#{col.to_s}_0')
|
604
|
-
|
605
|
-
#{col_spaces_prepend}= f.radio_button(:#{col.to_s}, '1', checked: #{singular}.#{col.to_s} ? 'checked' : '')
|
606
|
-
#{col_spaces_prepend}= f.label(:#{col.to_s}, value: 'Yes', for: '#{singular}_#{col.to_s}_1')
|
607
|
-
"
|
608
|
-
end
|
609
|
-
end
|
610
|
-
}.join("\n")
|
611
|
-
return res
|
602
|
+
@template_builder.all_form_fields(
|
603
|
+
columns: @columns,
|
604
|
+
show_only: @show_only,
|
605
|
+
singular_class: singular_class,
|
606
|
+
singular: singular
|
607
|
+
)
|
612
608
|
end
|
613
609
|
|
614
|
-
|
615
610
|
def all_line_fields
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
limit = eval("#{singular_class}.columns_hash['#{col}']").limit
|
623
|
-
sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
|
624
|
-
|
625
|
-
case type
|
626
|
-
when :integer
|
627
|
-
# look for a belongs_to on this object
|
628
|
-
if col.to_s.ends_with?("_id")
|
629
|
-
|
630
|
-
assoc_name = col.to_s.gsub("_id","")
|
631
|
-
|
632
|
-
|
633
|
-
assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
|
634
|
-
|
635
|
-
if assoc.nil?
|
636
|
-
exit_message = "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
|
637
|
-
raise(HotGlue::Error,exit_message)
|
638
|
-
end
|
639
|
-
|
640
|
-
assoc_class = eval(assoc.class_name)
|
641
|
-
|
642
|
-
if assoc_class.respond_to?("name")
|
643
|
-
display_column = "name"
|
644
|
-
elsif assoc_class.respond_to?("to_label")
|
645
|
-
display_column = "to_label"
|
646
|
-
elsif assoc_class.respond_to?("full_name")
|
647
|
-
display_column = "full_name"
|
648
|
-
elsif assoc_class.respond_to?("display_name")
|
649
|
-
display_column = "display_name"
|
650
|
-
elsif assoc_class.respond_to?("email")
|
651
|
-
display_column = "email"
|
652
|
-
elsif assoc_class.respond_to?("number")
|
653
|
-
display_column = "number"
|
654
|
-
|
655
|
-
else
|
656
|
-
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, 5) email, or 6) number 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)"
|
657
|
-
end
|
658
|
-
|
659
|
-
"#{col_identifer}
|
660
|
-
= #{singular}.#{assoc.name.to_s}.try(:#{display_column}) || '<span class=\"content alert-danger\">MISSING</span>'.html_safe"
|
661
|
-
|
662
|
-
else
|
663
|
-
"#{col_identifer}
|
664
|
-
= #{singular}.#{col}"
|
665
|
-
end
|
666
|
-
when :float
|
667
|
-
width = (limit && limit < 40) ? limit : (40)
|
668
|
-
"#{col_identifer}
|
669
|
-
= #{singular}.#{col}"
|
670
|
-
|
671
|
-
when :string
|
672
|
-
width = (limit && limit < 40) ? limit : (40)
|
673
|
-
"#{col_identifer}
|
674
|
-
= #{singular}.#{col}"
|
675
|
-
when :text
|
676
|
-
"#{col_identifer}
|
677
|
-
= #{singular}.#{col}"
|
678
|
-
when :datetime
|
679
|
-
"#{col_identifer}
|
680
|
-
- unless #{singular}.#{col}.nil?
|
681
|
-
= #{singular}.#{col}.in_time_zone(current_timezone).strftime('%m/%d/%Y @ %l:%M %p ') + timezonize(current_timezone)
|
682
|
-
- else
|
683
|
-
%span.alert-danger
|
684
|
-
MISSING
|
685
|
-
"
|
686
|
-
when :date
|
687
|
-
".cell
|
688
|
-
- unless #{singular}.#{col}.nil?
|
689
|
-
= #{singular}.#{col}
|
690
|
-
- else
|
691
|
-
%span.alert-danger
|
692
|
-
MISSING
|
693
|
-
"
|
694
|
-
when :time
|
695
|
-
"#{col_identifer}
|
696
|
-
- unless #{singular}.#{col}.nil?
|
697
|
-
= #{singular}.#{col}.in_time_zone(current_timezone).strftime('%l:%M %p ') + timezonize(current_timezone)
|
698
|
-
- else
|
699
|
-
%span.alert-danger
|
700
|
-
MISSING
|
701
|
-
"
|
702
|
-
when :boolean
|
703
|
-
"#{col_identifer}
|
704
|
-
- if #{singular}.#{col}.nil?
|
705
|
-
%span.alert-danger
|
706
|
-
MISSING
|
707
|
-
- elsif #{singular}.#{col}
|
708
|
-
YES
|
709
|
-
- else
|
710
|
-
NO
|
711
|
-
"
|
712
|
-
end
|
713
|
-
}.join("\n")
|
611
|
+
@template_builder.all_line_fields(
|
612
|
+
columns: @columns,
|
613
|
+
show_only: @show_only,
|
614
|
+
singular_class: singular_class,
|
615
|
+
singular: singular
|
616
|
+
)
|
714
617
|
end
|
715
618
|
|
716
|
-
|
717
619
|
def controller_descends_from
|
718
620
|
if defined?(@namespace.titlecase + "::BaseController")
|
719
621
|
@namespace.titlecase + "::BaseController"
|
@@ -728,7 +630,7 @@ module HotGlue
|
|
728
630
|
me = eval(singular_class)
|
729
631
|
|
730
632
|
@display_class ||=
|
731
|
-
if me.column_names.include?("name")
|
633
|
+
if me.column_names.include?("name") || me.instance_methods(false).include?(:name)
|
732
634
|
# note that all class object respond_to?(:name) with the name of their own class
|
733
635
|
# this one is unique
|
734
636
|
"name"
|
@@ -742,7 +644,6 @@ module HotGlue
|
|
742
644
|
"email"
|
743
645
|
elsif me.column_names.include?("number") || me.instance_methods(false).include?(:number)
|
744
646
|
"number"
|
745
|
-
|
746
647
|
else
|
747
648
|
exit_message = "*** Oops: Can't find any column to use as the display label on #{singular_class} model . TODO: Please implement just one of: 1) name, 2) to_label, 3) full_name, 4) display_name, 5) email, or 6) number directly on your #{singular_class} 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)"
|
748
649
|
raise(HotGlue::Error, exit_message)
|
@@ -767,13 +668,9 @@ module HotGlue
|
|
767
668
|
end
|
768
669
|
end
|
769
670
|
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
"- if #{plural}.respond_to?(:total_pages)
|
774
|
-
= paginate #{plural}"
|
775
|
-
end
|
776
|
-
|
671
|
+
def paginate
|
672
|
+
@template_builder.paginate(plural: plural)
|
673
|
+
end
|
777
674
|
private # thor does something fancy like sending the class all of its own methods during some strange run sequence
|
778
675
|
# does not like public methods
|
779
676
|
|