hot-glue 0.3.9 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,10 +5,8 @@ module HotGlue
5
5
  attr_accessor :singular
6
6
 
7
7
  def field_output(col, type = nil, width, col_identifier )
8
- "<div class='#{col_identifier} form-group <%='alert-danger' if @#{singular}.errors.details.keys.include?(:#{col.to_s})%>' > \n" +
9
8
  " <%= f.text_field :#{col.to_s}, value: @#{@singular}.#{col.to_s}, autocomplete: 'off', size: #{width}, class: 'form-control', type: '#{type}' %>\n "+
10
- " <label class='form-text' >#{col.to_s.humanize}</label>\n" +
11
- "</div>"
9
+ " <label class='form-text' >#{col.to_s.humanize}</label>\n"
12
10
  end
13
11
 
14
12
 
@@ -21,7 +19,7 @@ module HotGlue
21
19
  magic_buttons.collect{ |button_name|
22
20
  "<%= form_with model: #{singular}, url: #{path_helper_singular}(#{path_helper_args}) do |f| %>
23
21
  <%= f.hidden_field :#{button_name}, value: \"#{button_name}\" %>
24
- <%= f.submit '#{button_name.titleize}'.html_safe, disabled: (schedule.respond_to?(:#{button_name}able?) && ! schedule.#{button_name}able? ), data: {confirm: 'Are you sure you want to #{button_name} this #{singular}?'}, class: '#{singular}-button btn btn-primary ' %>
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 ' %>
25
23
  <% end %>"
26
24
  }.join("\n")
27
25
  end
@@ -32,28 +30,31 @@ module HotGlue
32
30
  lines = 5
33
31
  end
34
32
 
35
- "<div class=\"#{col_identifier} form-group \#{'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s})}\">" +
36
33
  "<%= f.text_area :#{col.to_s}, class: 'form-control', autocomplete: 'off', cols: 40, rows: '#{lines}' %>" +
37
- "<label class='form-text'>#{col.to_s.humanize}</label>"+
38
- "</div>"
39
-
34
+ "<label class='form-text'>#{col.to_s.humanize}</label>"
40
35
  end
41
36
 
42
37
  def list_column_headings(*args)
43
- columns = args[0][:columns]
38
+ layout_columns = args[0][:columns]
44
39
  column_width = args[0][:column_width]
45
40
  col_identifier = args[0][:col_identifier]
46
- if @layout == "hotglue"
41
+ layout = args[0][:layout]
42
+
43
+ if layout == "hotglue"
47
44
  col_style = " style='flex-basis: #{column_width}%'"
48
45
  else
49
46
  col_style = ""
50
47
  end
51
- columns.map(&:to_s).map{|col_name| "<div class='#{col_identifier}'" + col_style +">#{col_name.humanize}</div>"}.join("\n")
48
+
49
+ result = layout_columns.map{ |column|
50
+ "<div class='#{col_identifier}'" + col_style + ">" + column.map(&:to_s).map{|col_name| "#{col_name.humanize}"}.join("<br />") + "</div>"
51
+ }.join("\n")
52
+ return result
52
53
  end
53
54
 
54
55
 
55
56
  def all_form_fields(*args)
56
- columns = args[0][:columns]
57
+ layout_columns = args[0][:columns]
57
58
  show_only = args[0][:show_only]
58
59
  singular_class = args[0][:singular_class]
59
60
  col_identifier = args[0][:col_identifier]
@@ -63,99 +64,86 @@ module HotGlue
63
64
  @singular = args[0][:singular]
64
65
  singular = @singular
65
66
 
66
-
67
-
68
67
  col_spaces_prepend = " "
69
68
 
70
- res = columns.map { |col|
71
-
72
- if show_only.include?(col)
73
69
 
74
- "<div class=\"#{col_identifier} form-group <%= 'alert-danger' if #{singular}.errors.details.keys.include?(:#{col}) %>\">" +
75
- "<%= @#{singular}.#{col.to_s} %>" +
76
- "<label class='form-text'>#{col.to_s.humanize}</label>" +
77
- "</div>"
70
+ result = layout_columns.map{ |column|
71
+ "<div class='#{col_identifier}' >" +
78
72
 
79
- else
73
+ column.map { |col|
80
74
 
81
-
82
- type = eval("#{singular_class}.columns_hash['#{col}']").type
83
- limit = eval("#{singular_class}.columns_hash['#{col}']").limit
84
- sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
85
-
86
- case type
87
- when :integer
88
- # look for a belongs_to on this object
89
- if col.to_s.ends_with?("_id")
90
- assoc_name = col.to_s.gsub("_id","")
91
- assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
92
- if assoc.nil?
93
- exit_message= "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
94
- exit
75
+ field_result = if show_only.include?(col)
76
+ "<%= @#{singular}.#{col.to_s} %>" +
77
+ "<label class='form-text'>#{col.to_s.humanize}</label>"
78
+ else
79
+ type = eval("#{singular_class}.columns_hash['#{col}']").type
80
+ limit = eval("#{singular_class}.columns_hash['#{col}']").limit
81
+ sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
82
+
83
+ case type
84
+ when :integer
85
+ # look for a belongs_to on this object
86
+ if col.to_s.ends_with?("_id")
87
+ assoc_name = col.to_s.gsub("_id","")
88
+ assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
89
+ if assoc.nil?
90
+ exit_message= "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
91
+ exit
92
+ end
93
+ 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>"
96
+
97
+ 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>"
100
+
101
+ end
102
+ when :string
103
+ if sql_type == "varchar" || sql_type == "character varying"
104
+ field_output(col, nil, limit || 40, col_identifier)
105
+ else
106
+ text_area_output(col, 65536, col_identifier)
107
+ end
108
+
109
+ when :text
110
+ if sql_type == "varchar"
111
+ field_output(col, nil, limit, col_identifier)
112
+ else
113
+ text_area_output(col, 65536, col_identifier)
114
+ end
115
+ when :float
116
+ field_output(col, nil, 5, col_identifier)
117
+ when :datetime
118
+ "<%= datetime_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{ col.to_s.humanize }', #{@auth ? @auth+'.timezone' : 'nil'}) %>"
119
+ when :date
120
+ "<%= date_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{ col.to_s.humanize }', #{@auth ? @auth+'.timezone' : 'nil'}) %>"
121
+ when :time
122
+ "<%= time_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{ col.to_s.humanize }', #{@auth ? @auth+'.timezone' : 'nil'}) %>"
123
+ when :boolean
124
+ " " +
125
+ " <span>#{col.to_s.humanize}</span>" +
126
+ " <%= f.radio_button(:#{col.to_s}, '0', checked: #{singular}.#{col.to_s} ? '' : 'checked') %>\n" +
127
+ " <%= f.label(:#{col.to_s}, value: 'No', for: '#{singular}_#{col.to_s}_0') %>\n" +
128
+ " <%= f.radio_button(:#{col.to_s}, '1', checked: #{singular}.#{col.to_s} ? 'checked' : '') %>\n" +
129
+ " <%= f.label(:#{col.to_s}, value: 'Yes', for: '#{singular}_#{col.to_s}_1') %>\n" +
130
+ ""
131
+ when :enum
132
+ 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>"
95
135
  end
96
- display_column = HotGlue.derrive_reference_name(assoc.class_name)
97
-
98
- "<div class='#{col_identifier} form-group <%= 'alert-danger' if #{singular}.errors.details.keys.include?(:#{assoc_name.to_s}) %>' >
99
- <%= f.collection_select(:#{col.to_s}, #{assoc.class_name}.all, :id, :#{display_column}, {prompt: true, selected: @#{singular}.#{col.to_s} }, class: 'form-control') %>
100
- <label class='small form-text text-muted'>#{col.to_s.humanize}</label></div>"
101
-
102
- else
103
- "<div class=\"#{col_identifier} form-group <%= 'alert-danger' if #{singular}.errors.details.keys.include?(:#{col}) %> \" >
104
- <%= f.text_field :#{col.to_s}, value: #{singular}.#{col.to_s}, class: 'form-control', size: 4, type: 'number' %>
105
- <label class='small form-text text-muted'>#{col.to_s.humanize}</label></div>"
106
-
107
- end
108
- when :string
109
- if sql_type == "varchar" || sql_type == "character varying"
110
- field_output(col, nil, limit || 40, col_identifier)
111
- else
112
- text_area_output(col, 65536, col_identifier)
113
- end
114
-
115
- when :text
116
- if sql_type == "varchar"
117
- field_output(col, nil, limit, col_identifier)
118
- else
119
- text_area_output(col, 65536, col_identifier)
120
- end
121
- when :float
122
- field_output(col, nil, 5, col_identifier)
123
- when :datetime
124
-
125
-
126
- "<div class='col form-group <%='alert-danger' if @#{singular}.errors.details.keys.include?(:#{col.to_s})%>' > \n" +
127
- "<%= datetime_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{ col.to_s.humanize }', #{@auth ? @auth+'.timezone' : 'nil'}) %>" +
128
- "</div>"
129
- when :date
130
- "<div class='col form-group <%='alert-danger' if @#{singular}.errors.details.keys.include?(:#{col.to_s})%>' > \n" +
131
- "<%= date_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{ col.to_s.humanize }', #{@auth ? @auth+'.timezone' : 'nil'}) %>" +
132
- "</div>"
133
- when :time
134
- "<div class='col form-group <%='alert-danger' if @#{singular}.errors.details.keys.include?(:#{col.to_s})%>' > \n" +
135
- "<%= time_field_localized(f, :#{col.to_s}, #{singular}.#{col.to_s}, '#{ col.to_s.humanize }', #{@auth ? @auth+'.timezone' : 'nil'}) %>" +
136
- "</div>"
137
-
138
- when :boolean
139
- "<div class='col form-group <%='alert-danger' if @#{singular}.errors.details.keys.include?(:#{col.to_s})%>' > \n" +
140
- " <span>#{col.to_s.humanize}</span>" +
141
- " <%= f.radio_button(:#{col.to_s}, '0', checked: #{singular}.#{col.to_s} ? '' : 'checked') %>\n" +
142
- " <%= f.label(:#{col.to_s}, value: 'No', for: '#{singular}_#{col.to_s}_0') %>\n" +
143
- " <%= f.radio_button(:#{col.to_s}, '1', checked: #{singular}.#{col.to_s} ? 'checked' : '') %>\n" +
144
- " <%= f.label(:#{col.to_s}, value: 'Yes', for: '#{singular}_#{col.to_s}_1') %>\n" +
145
- "</div>"
146
- when :enum
147
- enum_name = "enum_name"
148
- # byebug
149
- enum_type = eval("#{singular_class}.columns.select{|x| x.name == '#{col.to_s}'}[0].sql_type")
150
- "<div class='#{col_identifier} form-group <%= 'alert-danger' if #{singular}.errors.details.keys.include?(:#{col.to_s}) %>' >
151
- <%= f.collection_select(:#{col.to_s}, enum_to_collection_select( #{singular_class}.defined_enums['#{enum_type}']), :key, :value, {prompt: true, selected: @#{singular}.#{col.to_s} }, class: 'form-control') %>
152
- <label class='small form-text text-muted'>#{col.to_s.humanize}</label></div>"
136
+ end
153
137
 
138
+ if (type == :integer) && col.to_s.ends_with?("_id")
139
+ field_error_name = col.to_s.gsub("_id","")
140
+ else
141
+ field_error_name = col.to_s
154
142
  end
155
-
156
- 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>"
157
145
  }.join("\n")
158
- return res
146
+ return result
159
147
  end
160
148
 
161
149
 
@@ -167,116 +155,106 @@ module HotGlue
167
155
  end
168
156
 
169
157
  def all_line_fields(*args)
170
- columns = args[0][:columns]
158
+ layout_columns = args[0][:columns]
171
159
  show_only = args[0][:show_only]
172
160
  singular_class = args[0][:singular_class]
173
161
  singular = args[0][:singular]
174
162
  perc_width = args[0][:perc_width]
175
163
  layout = args[0][:layout]
176
164
 
177
- columns_count = columns.count + 1
165
+ columns_count = layout_columns.count + 1
178
166
  perc_width = (perc_width).floor
179
167
 
180
168
  if layout == "bootstrap"
181
- col_identifer = "col"
169
+ col_identifer = "col-md-2"
182
170
  style_with_flex_basis = ""
183
171
  else
184
172
  style_with_flex_basis = " style='flex-basis: #{perc_width}%'"
185
173
  col_identifer = "scaffold-cell"
186
174
  end
187
175
 
176
+ result = layout_columns.map{ |column|
177
+ "<div class='#{col_identifer}'#{style_with_flex_basis}>" +
188
178
 
189
- columns.map { |col|
190
- type = eval("#{singular_class}.columns_hash['#{col}']").type
191
- limit = eval("#{singular_class}.columns_hash['#{col}']").limit
192
- sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
193
179
 
194
- case type
195
- when :integer
196
- # look for a belongs_to on this object
197
- if col.to_s.ends_with?("_id")
180
+ column.map { |col|
181
+ type = eval("#{singular_class}.columns_hash['#{col}']").type
182
+ limit = eval("#{singular_class}.columns_hash['#{col}']").limit
183
+ sql_type = eval("#{singular_class}.columns_hash['#{col}']").sql_type
198
184
 
199
- assoc_name = col.to_s.gsub("_id","")
185
+ case type
186
+ when :integer
187
+ # look for a belongs_to on this object
188
+ if col.to_s.ends_with?("_id")
200
189
 
190
+ assoc_name = col.to_s.gsub("_id","")
201
191
 
202
- assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
203
192
 
204
- if assoc.nil?
205
- exit_message = "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
206
- puts exit_message
207
- exit
208
- # raise(HotGlue::Error,exit_message)
209
- end
193
+ assoc = eval("#{singular_class}.reflect_on_association(:#{assoc_name})")
210
194
 
211
- display_column = HotGlue.derrive_reference_name(assoc.class_name)
195
+ if assoc.nil?
196
+ exit_message = "*** Oops. on the #{singular_class} object, there doesn't seem to be an association called '#{assoc_name}'"
197
+ puts exit_message
198
+ exit
199
+ # raise(HotGlue::Error,exit_message)
200
+ end
212
201
 
213
- "<div class='#{col_identifer}'#{style_with_flex_basis}>
214
- <%= #{singular}.#{assoc.name.to_s}.try(:#{display_column}) || '<span class=\"content alert-danger\">MISSING</span>'.html_safe %>
215
- </div>"
202
+ display_column = HotGlue.derrive_reference_name(assoc.class_name)
216
203
 
217
- else
218
- "<div class='#{col_identifer}'#{style_with_flex_basis}>
219
- <%= #{singular}.#{col}%></div>"
220
- end
221
- when :float
222
- width = (limit && limit < 40) ? limit : (40)
223
- "<div class='#{col_identifer}'#{style_with_flex_basis}>
224
- <%= #{singular}.#{col}%></div>"
225
- when :string
226
- width = (limit && limit < 40) ? limit : (40)
227
- "<div class='#{col_identifer}'#{style_with_flex_basis} >
228
- <%= #{singular}.#{col} %>
229
- </div>"
230
- when :text
231
- "<div class='#{col_identifer}'#{style_with_flex_basis}>
232
- <%= #{singular}.#{col} %>
233
- </div>"
234
- when :datetime
235
-
236
- "<div class='#{col_identifer}' #{style_with_flex_basis} >
237
- <% unless #{singular}.#{col}.nil? %>
238
- <%= #{singular}.#{col}.in_time_zone(current_timezone).strftime('%m/%d/%Y @ %l:%M %p ') + timezonize(current_timezone) %>
239
- <% else %>
240
- <span class='alert-danger'>MISSING</span>
241
- <% end %>
242
- </div>"
243
- when :date
244
- "<div class='#{col_identifer}' #{style_with_flex_basis} >
245
- <% unless #{singular}.#{col}.nil? %>
246
- <%= #{singular}.#{col} %>
204
+ "<%= #{singular}.#{assoc.name.to_s}.try(:#{display_column}) || '<span class=\"content alert-danger\">MISSING</span>'.html_safe %>"
205
+
206
+ else
207
+ "<%= #{singular}.#{col}%>"
208
+ end
209
+ when :float
210
+ width = (limit && limit < 40) ? limit : (40)
211
+ "<%= #{singular}.#{col}%>"
212
+ when :string
213
+ width = (limit && limit < 40) ? limit : (40)
214
+ "<%= #{singular}.#{col} %>"
215
+ when :text
216
+ "<%= #{singular}.#{col} %>"
217
+ when :datetime
218
+ "<% unless #{singular}.#{col}.nil? %>
219
+ <%= #{singular}.#{col}.in_time_zone(current_timezone).strftime('%m/%d/%Y @ %l:%M %p ') + timezonize(current_timezone) %>
247
220
  <% else %>
248
221
  <span class='alert-danger'>MISSING</span>
249
- <% end %>
250
- </div>"
251
- when :time
252
- "<div class='#{col_identifer}' #{style_with_flex_basis} >
253
- <% unless #{singular}.#{col}.nil? %>
254
- <%= #{singular}.#{col}.in_time_zone(current_timezone).strftime('%l:%M %p ') + timezonize(current_timezone) %>
255
- <% else %>
256
- <span class='alert-danger'>MISSING</span>
257
- <% end %>
258
- </div>
259
- "
260
- when :boolean
261
- "<div class='#{col_identifer}' #{style_with_flex_basis} >
262
- <% if #{singular}.#{col}.nil? %>
263
- <span class='alert-danger'>MISSING</span>
264
- <% elsif #{singular}.#{col} %>
265
- YES
266
- <% else %>
267
- NO
268
- <% end %>
269
- </div>
270
- " when :enum
271
- "<div class='#{col_identifer}' #{style_with_flex_basis} >
272
- <% if #{singular}.#{col}.nil? %>
273
- <span class='alert-danger'>MISSING</span>
274
- <% else %>
275
- <%= #{singular}.#{col} %>
276
- <% end %>
277
- </div>
278
- "
279
- end #end of switch
222
+ <% end %>"
223
+ when :date
224
+ "<% unless #{singular}.#{col}.nil? %>
225
+ <%= #{singular}.#{col} %>
226
+ <% else %>
227
+ <span class='alert-danger'>MISSING</span>
228
+ <% end %>"
229
+ when :time
230
+ "<% unless #{singular}.#{col}.nil? %>
231
+ <%= #{singular}.#{col}.in_time_zone(current_timezone).strftime('%l:%M %p ') + timezonize(current_timezone) %>
232
+ <% else %>
233
+ <span class='alert-danger'>MISSING</span>
234
+ <% end %>"
235
+ when :boolean
236
+ "
237
+ <% if #{singular}.#{col}.nil? %>
238
+ <span class='alert-danger'>MISSING</span>
239
+ <% elsif #{singular}.#{col} %>
240
+ YES
241
+ <% else %>
242
+ NO
243
+ <% end %>
244
+
245
+ " when :enum
246
+ enum_type = eval("#{singular_class}.columns.select{|x| x.name == '#{col.to_s}'}[0].sql_type")
247
+
248
+ "
249
+ <% if #{singular}.#{col}.nil? %>
250
+ <span class='alert-danger'>MISSING</span>
251
+ <% else %>
252
+ <%= #{singular_class}.defined_enums['#{enum_type}'][#{singular}.#{col}] %>
253
+ <% end %>
254
+
255
+ "
256
+ end #end of switch
257
+ }.join("<br />") + "</div>"
280
258
  }.join("\n")
281
259
  end
282
260
  end
@@ -31,6 +31,7 @@ module HotGlue
31
31
  show_only = args[0][:show_only]
32
32
  singular_class = args[0][:singular_class]
33
33
 
34
+
34
35
  # TODO: CLEAN ME
35
36
  @singular = args[0][:singular]
36
37
  singular = @singular
@@ -137,11 +138,15 @@ module HotGlue
137
138
  show_only = args[0][:show_only]
138
139
  singular_class = args[0][:singular_class]
139
140
  singular = args[0][:singular]
141
+ layout = args[0][:layout]
140
142
 
141
143
  columns_count = columns.count + 1
142
144
  perc_width = (100/columns_count).floor
143
-
144
- col_identifer = ".col"
145
+ if @layout == 'bootstrap'
146
+ col_identifer = ".col-md-2"
147
+ else
148
+ col_identifer = ".col"
149
+ end
145
150
  columns.map { |col|
146
151
  type = eval("#{singular_class}.columns_hash['#{col}']").type
147
152
  limit = eval("#{singular_class}.columns_hash['#{col}']").limit