hot-glue 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +7 -6
  3. data/README.md +10 -2
  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 +228 -0
  8. data/lib/generators/hot_glue/markup_templates/haml.rb +223 -0
  9. data/lib/generators/hot_glue/markup_templates/slim.rb +9 -0
  10. data/lib/generators/hot_glue/scaffold_generator.rb +97 -214
  11. data/lib/generators/hot_glue/templates/erb/_errors.erb +11 -0
  12. data/lib/generators/hot_glue/templates/erb/_flash_notices.erb +12 -0
  13. data/lib/generators/hot_glue/templates/erb/_form.erb +8 -0
  14. data/lib/generators/hot_glue/templates/erb/_line.erb +10 -0
  15. data/lib/generators/hot_glue/templates/erb/_list.erb +19 -0
  16. data/lib/generators/hot_glue/templates/erb/_new_button.erb +3 -0
  17. data/lib/generators/hot_glue/templates/erb/_new_form.erb +8 -0
  18. data/lib/generators/hot_glue/templates/erb/_show.erb +8 -0
  19. data/lib/generators/hot_glue/templates/erb/create.turbo_stream.erb +18 -0
  20. data/lib/generators/hot_glue/templates/erb/destroy.turbo_stream.erb +3 -0
  21. data/lib/generators/hot_glue/templates/erb/edit.erb +30 -0
  22. data/lib/generators/hot_glue/templates/erb/edit.turbo_stream.erb +3 -0
  23. data/lib/generators/hot_glue/templates/erb/index.erb +10 -0
  24. data/lib/generators/hot_glue/templates/erb/new.erb +1 -0
  25. data/lib/generators/hot_glue/templates/erb/update.turbo_stream.erb +10 -0
  26. data/lib/generators/hot_glue/templates/{_errors.haml → haml/_errors.haml} +0 -0
  27. data/lib/generators/hot_glue/templates/{_flash_notices.haml → haml/_flash_notices.haml} +0 -0
  28. data/lib/generators/hot_glue/templates/{_form.haml → haml/_form.haml} +0 -0
  29. data/lib/generators/hot_glue/templates/{_line.haml → haml/_line.haml} +0 -0
  30. data/lib/generators/hot_glue/templates/{_list.haml → haml/_list.haml} +0 -0
  31. data/lib/generators/hot_glue/templates/{_new_button.haml → haml/_new_button.haml} +0 -0
  32. data/lib/generators/hot_glue/templates/{_new_form.haml → haml/_new_form.haml} +0 -0
  33. data/lib/generators/hot_glue/templates/{_show.haml → haml/_show.haml} +0 -0
  34. data/lib/generators/hot_glue/templates/{create.turbo_stream.haml → haml/create.turbo_stream.haml} +0 -0
  35. data/lib/generators/hot_glue/templates/{destroy.turbo_stream.haml → haml/destroy.turbo_stream.haml} +0 -0
  36. data/lib/generators/hot_glue/templates/{edit.haml → haml/edit.haml} +0 -0
  37. data/lib/generators/hot_glue/templates/{edit.turbo_stream.haml → haml/edit.turbo_stream.haml} +0 -0
  38. data/lib/generators/hot_glue/templates/{index.haml → haml/index.haml} +0 -0
  39. data/lib/generators/hot_glue/templates/{new.haml → haml/new.haml} +0 -0
  40. data/lib/generators/hot_glue/templates/{update.turbo_stream.haml → haml/update.turbo_stream.haml} +0 -0
  41. data/lib/hot-glue.rb +6 -20
  42. data/lib/hotglue/version.rb +1 -1
  43. metadata +36 -17
@@ -0,0 +1,9 @@
1
+
2
+
3
+ module HotGlue
4
+ class Slim < TemplateBase
5
+
6
+
7
+
8
+ end
9
+ end
@@ -1,6 +1,10 @@
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
@@ -42,13 +46,7 @@ module HotGlue
42
46
 
43
47
 
44
48
 
45
- def field_output(col, type = nil, width, col_identifier )
46
49
 
47
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
48
- = f.text_field :#{col.to_s}, value: @#{singular}.#{col.to_s}, size: #{width}, class: 'form-control', type: '#{type}'
49
- %label.form-text
50
- #{col.to_s.humanize}\n"
51
- end
52
50
  end
53
51
 
54
52
 
@@ -80,12 +78,23 @@ module HotGlue
80
78
  class_option :no_paginate, type: :boolean, default: false
81
79
  class_option :big_edit, type: :boolean, default: false
82
80
  class_option :show_only, type: :string, default: ""
81
+ class_option :markup, type: :string, default: "erb"
82
+
83
+
83
84
 
84
- def initialize(*meta_args) #:nodoc:
85
+ # def erb_replace_ampersands
86
+ # if @template_builder.is_a?(HotGlue::ErbTemplate)
87
+ # @output_buffer.gsub!('\%', '%')
88
+ # end
89
+ # end
90
+
91
+
92
+
93
+ def initialize(*meta_args)
85
94
  super
86
95
 
87
96
  begin
88
- object = eval(class_name)
97
+ @the_object = eval(class_name)
89
98
  rescue StandardError => e
90
99
  message = "*** Oops: It looks like there is no object for #{class_name}. Please define the object + database table first."
91
100
  raise(HotGlue::Error, message)
@@ -95,6 +104,18 @@ module HotGlue
95
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.")
96
105
  end
97
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
+
98
119
 
99
120
  args = meta_args[0]
100
121
  @singular = args.first.tableize.singularize # should be in form hello_world
@@ -121,7 +142,6 @@ module HotGlue
121
142
  @show_only += options['show_only'].split(",").collect(&:to_sym)
122
143
  end
123
144
 
124
- auth_assoc = @auth.gsub("current_","")
125
145
 
126
146
  @god = options['god'] || options['gd'] || false
127
147
  @specs_only = options['specs_only'] || false
@@ -168,15 +188,21 @@ module HotGlue
168
188
  end
169
189
  end
170
190
 
191
+ identify_object_owner
192
+ setup_fields
193
+ end
194
+
195
+ def identify_object_owner
196
+ auth_assoc = @auth && @auth.gsub("current_","")
171
197
 
172
198
  if !@object_owner_sym.empty?
173
199
  auth_assoc_field = auth_assoc + "_id"
174
200
  assoc = eval("#{singular_class}.reflect_on_association(:#{@object_owner_sym})")
175
201
 
176
202
  if assoc
177
- ownership_field = assoc.name.to_s + "_id"
203
+ @ownership_field = assoc.name.to_s + "_id"
178
204
  elsif !@nest
179
- 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."
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."
180
206
 
181
207
  else
182
208
  if @god
@@ -197,6 +223,11 @@ module HotGlue
197
223
  raise(HotGlue::Error, exit_message)
198
224
  end
199
225
  end
226
+ end
227
+
228
+
229
+ def setup_fields
230
+ auth_assoc = @auth && @auth.gsub("current_","")
200
231
 
201
232
  if !@include_fields
202
233
  @exclude_fields.push :id, :created_at, :updated_at, :encrypted_password,
@@ -205,15 +236,14 @@ module HotGlue
205
236
  :confirmation_token, :confirmed_at,
206
237
  :confirmation_sent_at, :unconfirmed_email
207
238
 
208
- @exclude_fields.push(auth_assoc_field.to_sym) if !auth_assoc_field.nil?
209
- @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?
210
241
 
211
242
 
212
- @columns = object.columns.map(&:name).map(&:to_sym).reject{|field| @exclude_fields.include?(field) }
243
+ @columns = @the_object.columns.map(&:name).map(&:to_sym).reject{|field| @exclude_fields.include?(field) }
213
244
 
214
245
  else
215
- @columns = object.columns.map(&:name).map(&:to_sym).reject{|field| !@include_fields.include?(field) }
216
-
246
+ @columns = @the_object.columns.map(&:name).map(&:to_sym).reject{|field| !@include_fields.include?(field) }
217
247
  end
218
248
 
219
249
  @columns.each do |col|
@@ -221,7 +251,7 @@ module HotGlue
221
251
  @show_only << col
222
252
  end
223
253
 
224
- if object.columns_hash[col.to_s].type == :integer
254
+ if @the_object.columns_hash[col.to_s].type == :integer
225
255
  if col.to_s.ends_with?("_id")
226
256
  # guess the association name label
227
257
  assoc_name = col.to_s.gsub("_id","")
@@ -260,8 +290,6 @@ module HotGlue
260
290
  end
261
291
  end
262
292
 
263
-
264
-
265
293
  #
266
294
  def formats
267
295
  [format]
@@ -290,11 +318,12 @@ module HotGlue
290
318
  template "system_spec.rb.erb", File.join("#{'spec/dummy/' if Rails.env.test?}spec/system#{namespace_with_dash}", "#{plural}_behavior_spec.rb")
291
319
  end
292
320
 
293
- template "_errors.haml", File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}", "_errors.haml")
321
+ template "#{@markup}/_errors.#{@markup}", File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}", "_errors.#{@markup}")
294
322
  end
295
323
 
296
324
  def list_column_headings
297
- @columns.map(&:to_s).map{|col_name| ' .col ' + col_name.humanize}.join("\n")
325
+ @template_builder.list_column_headings(columns: @columns)
326
+
298
327
  end
299
328
 
300
329
  def columns_spec_with_sample_data
@@ -365,11 +394,6 @@ module HotGlue
365
394
  @auth_identifier
366
395
  end
367
396
 
368
-
369
- # def path_helper_full
370
- # "#{@namespace+"_" if @namespace}#{(@nested_args.join("_") + "_" if @nested_args.any?)}#{singular}_path"
371
- # end
372
-
373
397
  def path_helper_args
374
398
  if @nested_args.any?
375
399
  [(@nested_args).collect{|a| "@#{a}"} , singular].join(",")
@@ -462,40 +486,60 @@ module HotGlue
462
486
  end
463
487
 
464
488
  def all_objects_variable
465
-
466
- # needs the authenticated root user
467
- # "#{@auth}.#{ @nested_args.map{|a| "#{@nested_args_plural[a]}.find(@#{a})"}.join('.') + "." if @nested_args.any?}#{plural}"
468
-
469
489
  all_objects_root + ".page(params[:page])"
470
-
471
490
  end
472
491
 
473
492
  def auth_object
474
493
  @auth
475
494
  end
476
495
 
477
-
478
496
  def no_devise_installed
479
497
  !Gem::Specification.sort_by{ |g| [g.name.downcase, g.version] }.group_by{ |g| g.name }['devise']
480
498
  end
481
499
 
500
+ # def erb_replace_ampersands!(filename = nil)
501
+ #
502
+ # return if filename.nil?
503
+ # file = File.open(filename, "r")
504
+ # contents = file.read
505
+ # file.close
506
+ #
507
+ # file = File.open(filename, "w")
508
+ # file.write( contents.gsub('\%', '%'))
509
+ # file.close
510
+ # end
511
+
482
512
 
483
513
 
484
514
 
485
515
 
486
516
  def copy_view_files
487
517
  return if @specs_only
488
- haml_views.each do |view|
518
+ all_views.each do |view|
489
519
  formats.each do |format|
490
- filename = cc_filename_with_extensions(view, "haml")
491
- template filename, File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}", controller_file_path, filename)
520
+ source_filename = cc_filename_with_extensions("#{@markup}/#{view}", "#{@markup}")
521
+ dest_filename = cc_filename_with_extensions("#{view}", "#{@markup}")
522
+ dest_filepath = File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}",
523
+ controller_file_path, dest_filename)
524
+
525
+
526
+ template source_filename, dest_filepath
527
+ gsub_file dest_filepath, '\%', '%'
528
+
492
529
  end
493
530
  end
494
531
 
495
532
  turbo_stream_views.each do |view|
496
533
  formats.each do |format|
497
- filename = cc_filename_with_extensions(view, 'turbo_stream.haml')
498
- template filename, File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}", controller_file_path, filename)
534
+ source_filename = cc_filename_with_extensions( "#{@markup}/#{view}.turbo_stream.#{@markup}")
535
+ dest_filename = cc_filename_with_extensions("#{view}", "turbo_stream.#{@markup}")
536
+ dest_filepath = File.join("#{'spec/dummy/' if Rails.env.test?}app/views#{namespace_with_dash}",
537
+ controller_file_path, dest_filename)
538
+
539
+
540
+ template source_filename, dest_filepath
541
+ gsub_file dest_filepath, '\%', '%'
542
+
499
543
  end
500
544
  end
501
545
  end
@@ -516,7 +560,7 @@ module HotGlue
516
560
  end
517
561
  end
518
562
 
519
- def haml_views
563
+ def all_views
520
564
  res = %w(index edit _form _line _list _show _errors)
521
565
 
522
566
  unless @no_create
@@ -548,180 +592,23 @@ module HotGlue
548
592
  end
549
593
 
550
594
  def all_form_fields
551
- col_identifier = " .col"
552
- col_spaces_prepend = " "
553
-
554
- res = @columns.map { |col|
555
-
556
- if @show_only.include?(col)
557
-
558
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
559
- = @#{singular}.#{col.to_s}
560
- %label.form-text
561
- #{col.to_s.humanize}\n"
562
- else
563
-
564
-
565
- type = eval("#{singular_class}.columns_hash['#{col}']").type
566
- limit = eval("#{singular_class}.columns_hash['#{col}']").limit
567
- sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
568
-
569
- case type
570
- when :integer
571
- # look for a belongs_to on this object
572
- if col.to_s.ends_with?("_id")
573
- assoc_name = col.to_s.gsub("_id","")
574
- assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
575
- if assoc.nil?
576
- exit_message= "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
577
- exit
578
- end
579
- display_column = derrive_reference_name(assoc.class_name)
580
-
581
-
582
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{assoc_name.to_s})}\"}
583
- #{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')
584
- #{col_spaces_prepend}%label.small.form-text.text-muted
585
- #{col_spaces_prepend} #{col.to_s.humanize}"
586
-
587
- else
588
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if @#{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
589
- #{col_spaces_prepend}= f.text_field :#{col.to_s}, value: #{singular}.#{col.to_s}, class: 'form-control', size: 4, type: 'number'
590
- #{col_spaces_prepend}%label.form-text
591
- #{col_spaces_prepend} #{col.to_s.humanize}\n"
592
- end
593
- when :string
594
- limit ||= 256
595
- if limit <= 256
596
- field_output(col, nil, limit, col_identifier)
597
- else
598
- text_area_output(col, limit, col_identifier)
599
- end
600
-
601
- when :text
602
- limit ||= 256
603
- if limit <= 256
604
- field_output(col, nil, limit, col_identifier)
605
- else
606
- text_area_output(col, limit, col_identifier)
607
- end
608
- when :float
609
- limit ||= 256
610
- field_output(col, nil, limit, col_identifier)
611
-
612
-
613
- when :datetime
614
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
615
- #{col_spaces_prepend}= datetime_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{col.to_s.humanize}', #{@auth ? @auth+'.timezone' : 'nil'})"
616
- when :date
617
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
618
- #{col_spaces_prepend}= date_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{col.to_s.humanize}', #{@auth ? @auth+'.timezone' : 'nil'})"
619
- when :time
620
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
621
- #{col_spaces_prepend}= time_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{col.to_s.humanize}', #{@auth ? @auth+'.timezone' : 'nil'})"
622
- when :boolean
623
- "#{col_identifier}{class: \"form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\"}
624
- #{col_spaces_prepend}%span
625
- #{col_spaces_prepend} #{col.to_s.humanize}
626
- #{col_spaces_prepend}= f.radio_button(:#{col.to_s}, '0', checked: #{singular}.#{col.to_s} ? '' : 'checked')
627
- #{col_spaces_prepend}= f.label(:#{col.to_s}, value: 'No', for: '#{singular}_#{col.to_s}_0')
628
-
629
- #{col_spaces_prepend}= f.radio_button(:#{col.to_s}, '1', checked: #{singular}.#{col.to_s} ? 'checked' : '')
630
- #{col_spaces_prepend}= f.label(:#{col.to_s}, value: 'Yes', for: '#{singular}_#{col.to_s}_1')
631
- "
632
- end
633
- end
634
- }.join("\n")
635
- return res
595
+ @template_builder.all_form_fields(
596
+ columns: @columns,
597
+ show_only: @show_only,
598
+ singular_class: singular_class,
599
+ singular: singular
600
+ )
636
601
  end
637
602
 
638
-
639
603
  def all_line_fields
640
- columns = @columns.count + 1
641
- perc_width = (100/columns).floor
642
-
643
- col_identifer = ".col"
644
- @columns.map { |col|
645
- type = eval("#{singular_class}.columns_hash['#{col}']").type
646
- limit = eval("#{singular_class}.columns_hash['#{col}']").limit
647
- sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
648
-
649
- case type
650
- when :integer
651
- # look for a belongs_to on this object
652
- if col.to_s.ends_with?("_id")
653
-
654
- assoc_name = col.to_s.gsub("_id","")
655
-
656
-
657
- assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
658
-
659
- if assoc.nil?
660
- exit_message = "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
661
- raise(HotGlue::Error,exit_message)
662
- end
663
-
664
- display_column = derrive_reference_name(assoc.class_name)
665
-
666
-
667
- "#{col_identifer}
668
- = #{singular}.#{assoc.name.to_s}.try(:#{display_column}) || '<span class=\"content alert-danger\">MISSING</span>'.html_safe"
669
-
670
- else
671
- "#{col_identifer}
672
- = #{singular}.#{col}"
673
- end
674
- when :float
675
- width = (limit && limit < 40) ? limit : (40)
676
- "#{col_identifer}
677
- = #{singular}.#{col}"
678
-
679
- when :string
680
- width = (limit && limit < 40) ? limit : (40)
681
- "#{col_identifer}
682
- = #{singular}.#{col}"
683
- when :text
684
- "#{col_identifer}
685
- = #{singular}.#{col}"
686
- when :datetime
687
- "#{col_identifer}
688
- - unless #{singular}.#{col}.nil?
689
- = #{singular}.#{col}.in_time_zone(current_timezone).strftime('%m/%d/%Y @ %l:%M %p ') + timezonize(current_timezone)
690
- - else
691
- %span.alert-danger
692
- MISSING
693
- "
694
- when :date
695
- ".cell
696
- - unless #{singular}.#{col}.nil?
697
- = #{singular}.#{col}
698
- - else
699
- %span.alert-danger
700
- MISSING
701
- "
702
- when :time
703
- "#{col_identifer}
704
- - unless #{singular}.#{col}.nil?
705
- = #{singular}.#{col}.in_time_zone(current_timezone).strftime('%l:%M %p ') + timezonize(current_timezone)
706
- - else
707
- %span.alert-danger
708
- MISSING
709
- "
710
- when :boolean
711
- "#{col_identifer}
712
- - if #{singular}.#{col}.nil?
713
- %span.alert-danger
714
- MISSING
715
- - elsif #{singular}.#{col}
716
- YES
717
- - else
718
- NO
719
- "
720
- end
721
- }.join("\n")
604
+ @template_builder.all_line_fields(
605
+ columns: @columns,
606
+ show_only: @show_only,
607
+ singular_class: singular_class,
608
+ singular: singular
609
+ )
722
610
  end
723
611
 
724
-
725
612
  def controller_descends_from
726
613
  if defined?(@namespace.titlecase + "::BaseController")
727
614
  @namespace.titlecase + "::BaseController"
@@ -774,13 +661,9 @@ module HotGlue
774
661
  end
775
662
  end
776
663
 
777
-
778
-
779
- def paginate
780
- "- if #{plural}.respond_to?(:total_pages)
781
- = paginate #{plural}"
782
- end
783
-
664
+ def paginate
665
+ @template_builder.paginate(plural: plural)
666
+ end
784
667
  private # thor does something fancy like sending the class all of its own methods during some strange run sequence
785
668
  # does not like public methods
786
669