hot-glue 0.4.9.2 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,14 @@
1
1
  module HotGlue
2
2
  class ErbTemplate < TemplateBase
3
3
 
4
+ attr_accessor :path, :singular, :singular_class,
5
+ :magic_buttons, :small_buttons,
6
+ :show_only, :column_width, :layout, :perc_width,
7
+ :ownership_field, :form_labels_position,
8
+ :inline_list_labels,
9
+ :columns, :column_width, :col_identifier, :singular,
10
+ :form_placeholder_labels, :hawk_keys
11
+
4
12
 
5
13
 
6
14
  def add_spaces_each_line(text, num_spaces)
@@ -8,16 +16,6 @@ module HotGlue
8
16
  text.lines.collect{|line| add_spaces + line}.join("")
9
17
  end
10
18
 
11
-
12
- # include GeneratorHelper
13
- attr_accessor :singular
14
-
15
- def field_output(col, type = nil, width, col_identifier )
16
- " <%= f.text_field :#{col}, value: @#{@singular}.#{col}, autocomplete: 'off', size: #{width}, class: 'form-control', type: '#{type}' %>\n "+
17
- "\n"
18
- end
19
-
20
-
21
19
  def magic_button_output(*args)
22
20
  path = args[0][:path]
23
21
  # path_helper_singular = args[0][:path_helper_singular]
@@ -34,20 +32,11 @@ module HotGlue
34
32
  }.join("\n")
35
33
  end
36
34
 
37
- def text_area_output(col, field_length, col_identifier )
38
- lines = field_length % 40
39
- if lines > 5
40
- lines = 5
41
- end
42
-
43
- "<%= f.text_area :#{col}, class: 'form-control', autocomplete: 'off', cols: 40, rows: '#{lines}' %>"
44
- end
45
-
46
35
  def list_column_headings(*args)
47
- layout_columns = args[0][:columns]
48
- column_width = args[0][:column_width]
49
- col_identifier = args[0][:col_identifier]
50
- layout = args[0][:layout]
36
+ @columns = args[0][:columns]
37
+ @column_width = args[0][:column_width]
38
+ @col_identifier = args[0][:col_identifier]
39
+ @layout = args[0][:layout]
51
40
 
52
41
  if layout == "hotglue"
53
42
  col_style = " style='flex-basis: #{column_width}%'"
@@ -55,28 +44,35 @@ module HotGlue
55
44
  col_style = ""
56
45
  end
57
46
 
58
- result = layout_columns.map{ |column|
47
+ result = columns.map{ |column|
59
48
  "<div class='#{col_identifier}'" + col_style + ">" + column.map(&:to_s).map{|col_name| "#{col_name.humanize}"}.join("<br />") + "</div>"
60
49
  }.join("\n")
61
50
  return result
62
51
  end
63
52
 
64
53
 
54
+ ################################################################
55
+
56
+ # THE FORM
57
+
65
58
  def all_form_fields(*args)
66
- layout_columns = args[0][:columns]
67
- show_only = args[0][:show_only]
68
- singular_class = args[0][:singular_class]
69
- col_identifier = args[0][:col_identifier]
70
- ownership_field = args[0][:ownership_field]
59
+ @columns = args[0][:columns]
60
+ @show_only = args[0][:show_only]
61
+ @singular_class = args[0][:singular_class]
62
+ @col_identifier = args[0][:col_identifier]
63
+ @ownership_field = args[0][:ownership_field]
64
+ @form_labels_position = args[0][:form_labels_position]
65
+ @form_placeholder_labels = args[0][:form_placeholder_labels]
66
+ @hawk_keys = args[0][:hawk_keys]
71
67
 
72
68
  @singular = args[0][:singular]
73
69
  singular = @singular
74
- result = layout_columns.map{ |column|
70
+ result = columns.map{ |column|
75
71
  " <div class='#{col_identifier}' >" +
76
72
  column.map { |col|
77
73
  field_result =
78
74
  if show_only.include?(col.to_sym)
79
- "<%= @#{singular}.#{col} %>"
75
+ "<%= #{singular}.#{col} %>"
80
76
  else
81
77
  type = eval("#{singular_class}.columns_hash['#{col}']").type
82
78
  limit = eval("#{singular_class}.columns_hash['#{col}']").limit
@@ -84,41 +80,11 @@ module HotGlue
84
80
 
85
81
  case type
86
82
  when :integer
87
- # look for a belongs_to on this object
88
- if col.to_s.ends_with?("_id")
89
- assoc_name = col.to_s.gsub("_id","")
90
- assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
91
- if assoc.nil?
92
- exit_message = "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
93
- exit
94
- end
95
-
96
- is_owner = col == ownership_field
97
- assoc_class_name = assoc.active_record.name
98
- display_column = HotGlue.derrive_reference_name(assoc_class_name)
99
-
100
- (is_owner ? "<% unless @#{assoc_name} %>\n" : "") +
101
- " <%= f.collection_select(:#{col}, #{assoc.class_name}.all, :id, :#{display_column}, {prompt: true, selected: @#{singular}.#{col} }, class: 'form-control') %>\n" +
102
- (is_owner ? "<% else %>\n <%= @#{assoc_name}.#{display_column} %>" : "") +
103
- (is_owner ? "\n<% end %>" : "")
104
-
105
- else
106
- "<%= f.text_field :#{col}, value: #{singular}.#{col}, class: 'form-control', size: 4, type: 'number' %>"
107
-
108
- end
83
+ integer_result(col)
109
84
  when :string
110
- if sql_type == "varchar" || sql_type == "character varying"
111
- field_output(col, nil, limit || 40, col_identifier)
112
- else
113
- text_area_output(col, 65536, col_identifier)
114
- end
115
-
85
+ string_result(col, sql_type, limit)
116
86
  when :text
117
- if sql_type == "varchar"
118
- field_output(col, nil, limit, col_identifier)
119
- else
120
- text_area_output(col, 65536, col_identifier)
121
- end
87
+ text_result(col, sql_type, limit)
122
88
  when :float
123
89
  field_output(col, nil, 5, col_identifier)
124
90
  when :datetime
@@ -128,18 +94,10 @@ module HotGlue
128
94
  when :time
129
95
  "<%= time_field_localized(f, :#{col}, #{singular}.#{col}, '#{ col.to_s.humanize }', #{@auth ? @auth+'.timezone' : 'nil'}) %>"
130
96
  when :boolean
131
- " " +
132
- " <span>#{col.to_s.humanize}</span>" +
133
- " <%= f.radio_button(:#{col}, '0', checked: #{singular}.#{col} ? '' : 'checked') %>\n" +
134
- " <%= f.label(:#{col}, value: 'No', for: '#{singular}_#{col}_0') %>\n" +
135
- " <%= f.radio_button(:#{col}, '1', checked: #{singular}.#{col} ? 'checked' : '') %>\n" +
136
- " <%= f.label(:#{col}, value: 'Yes', for: '#{singular}_#{col}_1') %>\n" +
137
- ""
97
+ boolean_result(col)
138
98
  when :enum
139
- enum_type = eval("#{singular_class}.columns.select{|x| x.name == '#{col}'}[0].sql_type")
140
- "<%= f.collection_select(:#{col}, enum_to_collection_select( #{singular_class}.defined_enums['#{enum_type}']), :key, :value, {selected: @#{singular}.#{col} }, class: 'form-control') %>"
99
+ enum_result(col)
141
100
  end
142
-
143
101
  end
144
102
 
145
103
  if (type == :integer) && col.to_s.ends_with?("_id")
@@ -148,16 +106,97 @@ module HotGlue
148
106
  field_error_name = col
149
107
  end
150
108
 
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) +
109
+ the_label = "\n<label class='small form-text text-muted'>#{col.to_s.humanize}</label>"
110
+ add_spaces_each_line( "\n <span class='<%= \"alert-danger\" if #{singular}.errors.details.keys.include?(:#{field_error_name}) %>' #{'style="display: inherit;"'} >\n" +
111
+ add_spaces_each_line( (@form_labels_position == 'before' ? the_label : "") +
112
+ field_result + (@form_labels_position == 'after' ? the_label : "") , 4) +
153
113
  "\n </span>\n <br />", 2)
154
114
 
115
+
155
116
  }.join("") + "\n </div>"
156
117
  }.join("\n")
157
118
  return result
158
119
  end
159
120
 
160
121
 
122
+ def integer_result(col)
123
+ # look for a belongs_to on this object
124
+ if col.to_s.ends_with?("_id")
125
+ assoc_name = col.to_s.gsub("_id","")
126
+ assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
127
+ if assoc.nil?
128
+ exit_message = "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
129
+ exit
130
+ end
131
+
132
+ is_owner = col == ownership_field
133
+ assoc_class_name = assoc.active_record.name
134
+ display_column = HotGlue.derrive_reference_name(assoc_class_name)
135
+
136
+ if @hawk_keys[assoc.foreign_key.to_sym]
137
+ hawk_definition = @hawk_keys[assoc.foreign_key.to_sym]
138
+ hawk_root = hawk_definition[0]
139
+ hawk_scope = hawk_definition[1]
140
+ hawked_association = "#{hawk_root}.#{hawk_scope}"
141
+ else
142
+ hawked_association = "#{assoc.class_name}.all"
143
+ end
144
+
145
+ (is_owner ? "<% unless @#{assoc_name} %>\n" : "") +
146
+ " <%= f.collection_select(:#{col}, #{hawked_association}, :id, :#{display_column}, {prompt: true, selected: @#{singular}.#{col} }, class: 'form-control') %>\n" +
147
+ (is_owner ? "<% else %>\n <%= @#{assoc_name}.#{display_column} %>" : "") +
148
+ (is_owner ? "\n<% end %>" : "")
149
+
150
+ else
151
+ " <%= f.text_field :#{col}, value: #{@singular}.#{col}, autocomplete: 'off', size: 4, class: 'form-control', type: 'number'" + (@form_placeholder_labels ? ", placeholder: '#{col.to_s.humanize}'" : "") + " %>\n " + "\n"
152
+ end
153
+ end
154
+
155
+ def string_result(col, sql_type, limit)
156
+ if sql_type == "varchar" || sql_type == "character varying"
157
+ field_output(col, nil, limit || 40, col_identifier)
158
+ else
159
+ text_area_output(col, 65536, col_identifier)
160
+ end
161
+ end
162
+
163
+
164
+ def text_result(col, sql_type, limit)
165
+ if sql_type == "varchar"
166
+ field_output(col, nil, limit, col_identifier)
167
+ else
168
+ text_area_output(col, 65536, col_identifier)
169
+ end
170
+ end
171
+
172
+ def field_output(col, type = nil, width, col_identifier )
173
+ " <%= f.text_field :#{col}, value: #{@singular}.#{col}, autocomplete: 'off', size: #{width}, class: 'form-control', type: '#{type}'" + (@form_placeholder_labels ? ", placeholder: '#{col.to_s.humanize}'" : "") + " %>\n " + "\n"
174
+ end
175
+
176
+ def text_area_output(col, field_length, col_identifier )
177
+ lines = field_length % 40
178
+ if lines > 5
179
+ lines = 5
180
+ end
181
+
182
+ "<%= f.text_area :#{col}, class: 'form-control', autocomplete: 'off', cols: 40, rows: '#{lines}'" + ( @form_placeholder_labels ? ", placeholder: '#{col.to_s.humanize}'" : "") + " %>"
183
+ end
184
+
185
+ def boolean_result(col)
186
+ " <br />" +
187
+ " <%= f.radio_button(:#{col}, '0', checked: #{singular}.#{col} ? '' : 'checked') %>\n" +
188
+ " <%= f.label(:#{col}, value: 'No', for: '#{singular}_#{col}_0') %>\n" +
189
+ " <%= f.radio_button(:#{col}, '1', checked: #{singular}.#{col} ? 'checked' : '') %>\n" +
190
+ " <%= f.label(:#{col}, value: 'Yes', for: '#{singular}_#{col}_1') %>\n" +
191
+ ""
192
+ end
193
+
194
+ def enum_result(col)
195
+ enum_type = eval("#{singular_class}.columns.select{|x| x.name == '#{col}'}[0].sql_type")
196
+ "<%= f.collection_select(:#{col}, enum_to_collection_select( #{singular_class}.defined_enums['#{enum_type}']), :key, :value, {selected: @#{singular}.#{col} }, class: 'form-control') %>"
197
+ end
198
+
199
+ ################################################################
161
200
 
162
201
  def paginate(*args)
163
202
  plural = args[0][:plural]
@@ -165,18 +204,24 @@ module HotGlue
165
204
  "<% if #{plural}.respond_to?(:total_pages) %><%= paginate(#{plural}) %> <% end %>"
166
205
  end
167
206
 
168
- def all_line_fields(*args)
169
- layout_columns = args[0][:columns]
170
- show_only = args[0][:show_only]
171
- singular_class = args[0][:singular_class]
172
- singular = args[0][:singular]
173
- perc_width = args[0][:perc_width]
174
- layout = args[0][:layout]
175
- col_identifier = args[0][:col_identifier] || (layout == "bootstrap" ? "col-md-2" : "scaffold-cell")
176
207
 
177
208
 
178
- columns_count = layout_columns.count + 1
179
- perc_width = (perc_width).floor
209
+
210
+ ################################################################
211
+
212
+
213
+ def all_line_fields(*args)
214
+ @columns = args[0][:columns]
215
+ @show_only = args[0][:show_only]
216
+ @singular_class = args[0][:singular_class]
217
+ @singular = args[0][:singular]
218
+ @perc_width = args[0][:perc_width]
219
+ @layout = args[0][:layout]
220
+ @col_identifier = args[0][:col_identifier] || (layout == "bootstrap" ? "col-md-2" : "scaffold-cell")
221
+ @inline_list_labels = args[0][:inline_list_labels] || 'omit'
222
+
223
+ columns_count = columns.count + 1
224
+ perc_width = (@perc_width).floor
180
225
 
181
226
  if layout == "bootstrap"
182
227
  style_with_flex_basis = ""
@@ -184,7 +229,7 @@ module HotGlue
184
229
  style_with_flex_basis = " style='flex-basis: #{perc_width}%'"
185
230
  end
186
231
 
187
- result = layout_columns.map{ |column|
232
+ result = columns.map{ |column|
188
233
  "<div class='#{col_identifier}'#{style_with_flex_basis}>" +
189
234
 
190
235
 
@@ -193,7 +238,7 @@ module HotGlue
193
238
  limit = eval("#{singular_class}.columns_hash['#{col}']").limit
194
239
  sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
195
240
 
196
- case type
241
+ field_output = case type
197
242
  when :integer
198
243
  # look for a belongs_to on this object
199
244
  if col.ends_with?("_id")
@@ -262,9 +307,15 @@ module HotGlue
262
307
  <%= #{singular_class}.defined_enums['#{enum_type}'][#{singular}.#{col}] %>
263
308
  <% end %>
264
309
 
265
- "
266
- end #end of switch
267
- }.join("<br />") + "</div>"
310
+ "
311
+ end #end of switch
312
+
313
+
314
+ label = "<br/><label class='small form-text text-muted'>#{col.to_s.humanize}</label>"
315
+
316
+ (inline_list_labels == 'before' ? label : "") + field_output +
317
+ (inline_list_labels == 'after' ? label : "")
318
+ }.join( "<br />") + "</div>"
268
319
  }.join("\n")
269
320
  end
270
321
  end