hot-glue 0.3.8 → 0.4.2

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