hot-glue 0.5.6 → 0.5.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,34 +18,33 @@ module HotGlue
18
18
 
19
19
  def magic_button_output(path:, singular:, magic_buttons:, small_buttons: )
20
20
  magic_buttons.collect{ |button_name|
21
- "<%= form_with model: #{singular}, url: #{path}, html: {style: 'display: inline', data: {\"turbo-confirm\": 'Are you sure you want to #{button_name} this #{singular}?'}} do |f| %>
22
- <%= f.hidden_field :#{button_name}, value: \"#{button_name}\" %>
23
- <%= 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}' %>
24
- <% end %>"
21
+ "<%= form_with model: #{singular}, url: #{path}, html: {style: 'display: inline', data: {\"turbo-confirm\": 'Are you sure you want to #{button_name} this #{singular}?'}} do |f| %>" +
22
+ "<%= f.hidden_field :__#{button_name}, value: \"__#{button_name}\" %>" +
23
+ "<%= f.submit '#{button_name.titleize}'.html_safe, disabled: (#{singular}.respond_to?(:#{button_name}able?) && ! #{singular}.#{button_name}able? ), class: '#{singular}-button #{@layout_strategy.button_applied_classes} #{@layout_strategy.magic_button_classes}' %>" +
24
+ "<% end %>"
25
25
  }.join("\n")
26
26
  end
27
27
 
28
- def list_column_headings(*args)
29
- @columns = args[0][:columns]
30
- @column_width = args[0][:column_width]
31
- @col_identifier = args[0][:col_identifier]
32
-
28
+ def list_column_headings(col_identifier: , columns: , column_width:, singular: )
33
29
  col_style = @layout_strategy.column_headings_col_style
34
30
 
35
31
  result = columns.map{ |column|
36
- "<div class='#{col_identifier}'" + col_style + ">" + column.map(&:to_s).map{|col_name| "#{col_name.humanize}"}.join("<br />") + "</div>"
32
+ "<div class='#{col_identifier}' heading--#{singular}--#{column.join("-")} " + col_style + ">" +
33
+ column.map(&:to_s).map{|col_name| "#{col_name.humanize}"}.join("<br />") + "</div>"
37
34
  }.join("\n")
38
35
  return result
39
36
  end
40
37
 
41
38
 
42
39
  ################################################################
43
-
44
40
  # THE FORM
41
+ ################################################################
45
42
 
46
43
  def all_form_fields(*args)
44
+
47
45
  @columns = args[0][:columns]
48
46
  @show_only = args[0][:show_only]
47
+
49
48
  @singular_class = args[0][:singular_class]
50
49
  @ownership_field = args[0][:ownership_field]
51
50
  @form_labels_position = args[0][:form_labels_position]
@@ -53,25 +52,28 @@ module HotGlue
53
52
  @hawk_keys = args[0][:hawk_keys]
54
53
  @singular = args[0][:singular]
55
54
 
56
- column_classes = args[0][:col_identifier]
55
+ @alt_lookups = args[0][:alt_lookups]
57
56
 
57
+ column_classes = args[0][:col_identifier]
58
+ update_show_only = args[0][:update_show_only] || []
58
59
  singular = @singular
60
+
59
61
  result = columns.map{ |column|
60
- " <div class='#{column_classes}' >" +
62
+ " <div class='#{column_classes} cell--#{singular}--#{column.join("-")}' >" +
61
63
  column.map { |col|
64
+ type = eval("#{singular_class}.columns_hash['#{col}']").type
65
+ limit = eval("#{singular_class}.columns_hash['#{col}']").limit
66
+ sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
67
+
62
68
  field_result =
63
69
  if show_only.include?(col.to_sym)
64
- "<%= #{singular}.#{col} %>"
70
+ show_only_result(type: type, col: col, singular: singular)
65
71
  else
66
- type = eval("#{singular_class}.columns_hash['#{col}']").type
67
- limit = eval("#{singular_class}.columns_hash['#{col}']").limit
68
- sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
69
-
70
72
  case type
71
73
  when :integer
72
74
  integer_result(col)
73
75
  when :uuid
74
- uuid_result(col)
76
+ association_result(col)
75
77
  when :string
76
78
  string_result(col, sql_type, limit)
77
79
  when :text
@@ -96,11 +98,18 @@ module HotGlue
96
98
  else
97
99
  field_error_name = col
98
100
  end
99
-
101
+ show_only_open = ""
102
+ show_only_close = ""
103
+ if update_show_only.include?(col)
104
+ show_only_open = "<% if action_name == 'edit' %>" +
105
+ show_only_result(type: type, col: col, singular: singular) + "<% else %>"
106
+ show_only_close = "<% end %>"
107
+ end
100
108
  the_label = "\n<label class='small form-text text-muted'>#{col.to_s.humanize}</label>"
101
109
  add_spaces_each_line( "\n <span class='<%= \"alert-danger\" if #{singular}.errors.details.keys.include?(:#{field_error_name}) %>' #{'style="display: inherit;"'} >\n" +
102
110
  add_spaces_each_line( (@form_labels_position == 'before' ? the_label : "") +
103
- field_result + (@form_labels_position == 'after' ? the_label : "") , 4) +
111
+ show_only_open + field_result + show_only_close +
112
+ (@form_labels_position == 'after' ? the_label : "") , 4) +
104
113
  "\n </span>\n <br />", 2)
105
114
 
106
115
 
@@ -110,6 +119,14 @@ module HotGlue
110
119
  end
111
120
 
112
121
 
122
+ def show_only_result(type:, col: , singular: )
123
+ if type == :uuid || (type == :integer && col.ends_with?("_id"))
124
+ association_read_only_result(col)
125
+ else
126
+ "<%= #{singular}.#{col} %>"
127
+ end
128
+ end
129
+
113
130
  def integer_result(col)
114
131
  # look for a belongs_to on this object
115
132
  if col.to_s.ends_with?("_id")
@@ -119,13 +136,7 @@ module HotGlue
119
136
  end
120
137
  end
121
138
 
122
-
123
- def uuid_result(col)
124
- association_result(col)
125
- end
126
-
127
-
128
- def association_result(col)
139
+ def association_read_only_result(col)
129
140
  assoc_name = col.to_s.gsub("_id","")
130
141
  assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
131
142
  if assoc.nil?
@@ -134,7 +145,7 @@ module HotGlue
134
145
  end
135
146
 
136
147
  is_owner = col == ownership_field
137
- assoc_class_name = assoc.active_record.name
148
+ assoc_class_name = assoc.class_name.to_s
138
149
  display_column = HotGlue.derrive_reference_name(assoc_class_name)
139
150
 
140
151
  if @hawk_keys[assoc.foreign_key.to_sym]
@@ -143,12 +154,39 @@ module HotGlue
143
154
  else
144
155
  hawked_association = "#{assoc.class_name}.all"
145
156
  end
157
+ "<%= #{@singular}.#{assoc_name}.#{display_column} %>"
158
+ end
159
+
160
+ def association_result(col)
161
+ assoc_name = col.to_s.gsub("_id","")
162
+ assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
146
163
 
147
- (is_owner ? "<% unless @#{assoc_name} %>\n" : "") +
148
- " <%= f.collection_select(:#{col}, #{hawked_association}, :id, :#{display_column}, {prompt: true, selected: @#{singular}.#{col} }, class: 'form-control') %>\n" +
149
- (is_owner ? "<% else %>\n <%= @#{assoc_name}.#{display_column} %>" : "") +
150
- (is_owner ? "\n<% end %>" : "")
151
164
 
165
+ if @alt_lookups.keys.include?(col.to_s)
166
+ alt = @alt_lookups[col.to_s][:lookup_as]
167
+ "<%= f.text_field :__lookup_#{alt}, value: @#{singular}.#{assoc_name}.try(:#{alt}), placeholder: \"search by #{alt}\" %>"
168
+ else
169
+ if assoc.nil?
170
+ exit_message = "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
171
+ exit
172
+ end
173
+
174
+ is_owner = col == ownership_field
175
+ assoc_class_name = assoc.class_name.to_s
176
+ display_column = HotGlue.derrive_reference_name(assoc_class_name)
177
+
178
+ if @hawk_keys[assoc.foreign_key.to_sym]
179
+ hawk_definition = @hawk_keys[assoc.foreign_key.to_sym]
180
+ hawked_association = hawk_definition.join(".")
181
+ else
182
+ hawked_association = "#{assoc.class_name}.all"
183
+ end
184
+
185
+ (is_owner ? "<% unless @#{assoc_name} %>\n" : "") +
186
+ " <%= f.collection_select(:#{col}, #{hawked_association}, :id, :#{display_column}, {prompt: true, selected: @#{singular}.#{col} }, class: 'form-control') %>\n" +
187
+ (is_owner ? "<% else %>\n <%= @#{assoc_name}.#{display_column} %>" : "") +
188
+ (is_owner ? "\n<% end %>" : "")
189
+ end
152
190
  end
153
191
 
154
192
  def string_result(col, sql_type, limit)
@@ -182,17 +220,25 @@ module HotGlue
182
220
  end
183
221
 
184
222
  def boolean_result(col)
185
- " <br />" +
223
+ (form_labels_position == 'before' ? " <br />" : "") +
186
224
  " <%= f.radio_button(:#{col}, '0', checked: #{singular}.#{col} ? '' : 'checked') %>\n" +
187
225
  " <%= f.label(:#{col}, value: 'No', for: '#{singular}_#{col}_0') %>\n" +
188
226
  " <%= f.radio_button(:#{col}, '1', checked: #{singular}.#{col} ? 'checked' : '') %>\n" +
189
227
  " <%= f.label(:#{col}, value: 'Yes', for: '#{singular}_#{col}_1') %>\n" +
190
- ""
228
+ (form_labels_position == 'after' ? " <br />" : "")
229
+
191
230
  end
192
231
 
193
232
  def enum_result(col)
194
233
  enum_type = eval("#{singular_class}.columns.select{|x| x.name == '#{col}'}[0].sql_type")
195
- "<%= f.collection_select(:#{col}, enum_to_collection_select( #{singular_class}.defined_enums['#{enum_type}']), :key, :value, {selected: @#{singular}.#{col} }, class: 'form-control') %>"
234
+
235
+ if eval("defined? #{singular_class}.#{enum_type}_labels") == "method"
236
+ enum_definer = "#{singular_class}.#{enum_type}_labels"
237
+ else
238
+ enum_definer = "#{singular_class}.defined_enums['#{enum_type}']"
239
+ end
240
+
241
+ "<%= f.collection_select(:#{col}, enum_to_collection_select(#{enum_definer}), :key, :value, {selected: @#{singular}.#{col} }, class: 'form-control') %>"
196
242
  end
197
243
 
198
244
  ################################################################
@@ -225,10 +271,13 @@ module HotGlue
225
271
  style_with_flex_basis = @layout_strategy.style_with_flex_basis(perc_width)
226
272
 
227
273
  result = columns.map{ |column|
228
- "<div class='#{col_identifier}'#{style_with_flex_basis}>" +
274
+ "<div class='#{col_identifier} #{singular}--#{column.join("-")}'#{style_with_flex_basis}> " +
229
275
 
230
276
 
231
277
  column.map { |col|
278
+ if eval("#{singular_class}.columns_hash['#{col}']").nil?
279
+ raise "Can't find column '#{col}' on #{singular_class}, are you sure that is the column name?"
280
+ end
232
281
  type = eval("#{singular_class}.columns_hash['#{col}']").type
233
282
  limit = eval("#{singular_class}.columns_hash['#{col}']").limit
234
283
  sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
@@ -247,7 +296,8 @@ module HotGlue
247
296
  exit
248
297
  # raise(HotGlue::Error,exit_message)
249
298
  end
250
- assoc_class_name = assoc.active_record.name
299
+ assoc_class_name = assoc.class_name
300
+
251
301
  display_column = HotGlue.derrive_reference_name(assoc_class_name)
252
302
  "<%= #{singular}.#{assoc.name.to_s}.try(:#{display_column}) || '<span class=\"content alert-danger\">MISSING</span>'.html_safe %>"
253
303
 
@@ -265,8 +315,8 @@ module HotGlue
265
315
  exit
266
316
  # raise(HotGlue::Error,exit_message)
267
317
  end
268
- assoc_class_name = assoc.active_record.name
269
- display_column = HotGlue.derrive_reference_name(assoc_class_name)
318
+
319
+ display_column = HotGlue.derrive_reference_name(assoc.class_name.to_s)
270
320
  "<%= #{singular}.#{assoc.name.to_s}.try(:#{display_column}) || '<span class=\"content alert-danger\">MISSING</span>'.html_safe %>"
271
321
 
272
322
  when :float
@@ -309,11 +359,16 @@ module HotGlue
309
359
  when :enum
310
360
  enum_type = eval("#{singular_class}.columns.select{|x| x.name == '#{col}'}[0].sql_type")
311
361
 
362
+ if eval("defined? #{singular_class}.#{enum_type}_labels") == "method"
363
+ enum_definer = "#{singular_class}.#{enum_type}_labels"
364
+ else
365
+ enum_definer = "#{singular_class}.defined_enums['#{enum_type}']"
366
+ end
312
367
  "
313
368
  <% if #{singular}.#{col}.nil? %>
314
369
  <span class='alert-danger'>MISSING</span>
315
370
  <% else %>
316
- <%= #{singular_class}.defined_enums['#{enum_type}'][#{singular}.#{col}] %>
371
+ <%= #{enum_definer}[#{singular}.#{col}.to_sym] %>
317
372
  <% end %>
318
373
 
319
374
  "