vidl-toolbox 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/toolbox/config.rb +6 -3
- data/lib/toolbox/rendering.rb +174 -98
- data/lib/toolbox/version.rb +1 -1
- metadata +1 -1
data/lib/toolbox/config.rb
CHANGED
@@ -145,6 +145,7 @@ module Toolbox
|
|
145
145
|
:model_name => { :required => true, :type => [String] },
|
146
146
|
:url => {:type => [String]},
|
147
147
|
:direct_link => { :type => [TrueClass, FalseClass], :default => false},
|
148
|
+
:active => { :type => [Fixnum], :default => 1},
|
148
149
|
:css_class => { :type => [String]}
|
149
150
|
|
150
151
|
end
|
@@ -157,16 +158,18 @@ module Toolbox
|
|
157
158
|
:label => { :type => [String] },
|
158
159
|
:name => { :required => true, :type => [Symbol] },
|
159
160
|
:model_name => { :required => true, :type => [String] },
|
160
|
-
:type => { :type => [Symbol], :default => :textfield, :values => [:select, :collection_select, :auto_complete, :textfield, :radio, :date, :check_box]},
|
161
|
+
:type => { :type => [Symbol], :default => :textfield, :values => [:select, :collection_select, :auto_complete, :textfield, :textarea, :radio, :date, :check_box]},
|
161
162
|
:url => { :type => [Symbol]},
|
162
|
-
:size => { :type => [Fixnum]},
|
163
|
+
:size => { :type => [Fixnum, String]},
|
163
164
|
:values => { :type => [Array]},
|
164
|
-
:info => { :type => [String]}
|
165
|
+
:info => { :type => [String]},
|
166
|
+
:css_class => { :type => [String]}
|
165
167
|
end
|
166
168
|
|
167
169
|
# Holds the configuration for a non-editable field
|
168
170
|
class FieldConfig < Config
|
169
171
|
define_options :model_method => { :type => [Symbol, Proc]},
|
172
|
+
:render_method => { :type => [Symbol, Proc]},
|
170
173
|
:hide_empty => { :default => false },
|
171
174
|
:suppress_label => { :default => false },
|
172
175
|
:suppress_link => { :default => false },
|
data/lib/toolbox/rendering.rb
CHANGED
@@ -108,6 +108,7 @@ module Toolbox
|
|
108
108
|
end
|
109
109
|
val = rec
|
110
110
|
end
|
111
|
+
val = nil if val == ''
|
111
112
|
val
|
112
113
|
end
|
113
114
|
|
@@ -117,6 +118,7 @@ module Toolbox
|
|
117
118
|
# - html: if true, returns the value in html (escaped etc.)
|
118
119
|
# The following options are used:
|
119
120
|
# - :model_method => an alternative method or block of the object to get the display value
|
121
|
+
# - :render_method => an (helper/view) method or block that returns the rendered value (parameters: object, val, html)
|
120
122
|
# - :hide_empty: returns nil if the display value is empty. Otherwise, an empty string
|
121
123
|
# will be returned (or in case of html)
|
122
124
|
# - :suppress_link: if the display value is an ActiveRecord usually a link to it will
|
@@ -129,21 +131,15 @@ module Toolbox
|
|
129
131
|
# :local_date format
|
130
132
|
def render_value(object, html = true)
|
131
133
|
val = value(object)
|
132
|
-
|
133
|
-
if
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
end
|
140
|
-
end
|
141
|
-
else # map empty array to nil
|
142
|
-
value_text nil, html, object
|
134
|
+
unless val == nil && @widget_config.hide_empty
|
135
|
+
if @widget_config.render_method
|
136
|
+
render_own object, val, html
|
137
|
+
elsif val.is_a? Array
|
138
|
+
render_array object, val, html
|
139
|
+
else
|
140
|
+
value_text val, html, object
|
143
141
|
end
|
144
|
-
|
145
|
-
value_text val, html, object
|
146
|
-
end
|
142
|
+
end
|
147
143
|
end
|
148
144
|
|
149
145
|
protected
|
@@ -177,6 +173,30 @@ module Toolbox
|
|
177
173
|
end
|
178
174
|
text
|
179
175
|
end
|
176
|
+
|
177
|
+
private
|
178
|
+
|
179
|
+
def render_own (object, val, html)
|
180
|
+
if @widget_config.render_method.is_a? Proc
|
181
|
+
@widget_config.render_method.call @view, object, val, html
|
182
|
+
else
|
183
|
+
@view.send @widget_config.render_method, object, val, html
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
def render_array (object, val, html)
|
188
|
+
if val.size > 0
|
189
|
+
if @widget_config.join
|
190
|
+
val.map { |v| value_text(v, html, object) }.join(@widget_config.join.to_s)
|
191
|
+
else
|
192
|
+
@view.content_tag 'ul' do
|
193
|
+
val.map { |v| @view.content_tag 'li', value_text(v, html, object) }.join
|
194
|
+
end
|
195
|
+
end
|
196
|
+
else # map empty array to nil
|
197
|
+
value_text nil, html, object
|
198
|
+
end
|
199
|
+
end
|
180
200
|
|
181
201
|
end
|
182
202
|
|
@@ -185,59 +205,106 @@ module Toolbox
|
|
185
205
|
def render_control(form, rec, has_error = false)
|
186
206
|
options = {}
|
187
207
|
options[:class] = 'error' if has_error
|
208
|
+
options[:class] = (options[:class] || '') + ' ' + @widget_config.css_class if @widget_config.css_class
|
188
209
|
options[:id] = "dialog_#{form.tag_id @widget_config.name}" if @dialog
|
189
210
|
options[:title] = @view.send(:h, @widget_config.info) if @widget_config.info
|
190
211
|
case @widget_config.type
|
191
212
|
when :select
|
192
|
-
form
|
213
|
+
render_select form, rec, options
|
193
214
|
when :collection_select
|
194
|
-
form
|
195
|
-
@widget_config.values,
|
196
|
-
@widget_config.model_method,
|
197
|
-
@widget_config.text_method,
|
198
|
-
{},
|
199
|
-
options.merge({ :multiple => true })
|
215
|
+
render_collection_select form, rec, options
|
200
216
|
when :auto_complete
|
201
|
-
|
202
|
-
#id = "#{t}_#{@widget_config.name.to_s}"
|
203
|
-
id = options[:id] || form.tag_id(@widget_config.name)
|
204
|
-
name = @widget_config.name.to_s.foreign_key.to_sym
|
205
|
-
opt = {}
|
206
|
-
opt[:id] = "dialog_#{form.tag_id name}" if @dialog
|
207
|
-
s = form.hidden_field name, opt
|
208
|
-
s += form.text_field @widget_config.name, options.merge(:size => (@widget_config.size || 40))
|
209
|
-
s += @view.content_tag("div", "", :id => "#{id}_auto_complete", :class => "auto_complete")
|
210
|
-
s += @view.auto_complete_field(id,
|
211
|
-
:url => @view.send(@widget_config.url),
|
212
|
-
:method => :get,
|
213
|
-
:param_name => :autocomplete_query,
|
214
|
-
:select => 'autocomplete_name',
|
215
|
-
:after_update_element => 'auto_complete_update_hidden'
|
216
|
-
)
|
217
|
-
s
|
217
|
+
render_autocomplete form, rec, options
|
218
218
|
when :radio
|
219
|
-
|
220
|
-
@widget_config.values.each do |val|
|
221
|
-
s += form.radio_button @widget_config.name, val[1].to_s, options
|
222
|
-
s += @view.content_tag('span', val[0])
|
223
|
-
end
|
224
|
-
s
|
219
|
+
render_radio form, rec, options
|
225
220
|
when :check_box
|
226
|
-
form
|
221
|
+
render_checkbox form, rec, options
|
227
222
|
when :date
|
228
|
-
form
|
223
|
+
render_date form, rec, options
|
229
224
|
when :textfield
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
text = val.to_s(:local_datetime) if val.instance_of? ActiveSupport::TimeWithZone
|
235
|
-
form.text_field @widget_config.name, options.merge({:size => (@widget_config.size || 40), :value => text})
|
236
|
-
else
|
225
|
+
render_textfield form, rec, options
|
226
|
+
when :textarea
|
227
|
+
render_textarea form, rec, options
|
228
|
+
else
|
237
229
|
raise "Unkown control type #{@widget_config.type}"
|
238
230
|
end
|
239
231
|
end
|
240
232
|
|
233
|
+
private
|
234
|
+
|
235
|
+
def render_select form, rec, options
|
236
|
+
form.select @widget_config.name, @widget_config.values, {}, options
|
237
|
+
end
|
238
|
+
|
239
|
+
def render_collection_select form, rec, options
|
240
|
+
form.collection_select @widget_config.name,
|
241
|
+
@widget_config.values,
|
242
|
+
@widget_config.model_method,
|
243
|
+
@widget_config.text_method,
|
244
|
+
{},
|
245
|
+
options.merge({ :multiple => true })
|
246
|
+
end
|
247
|
+
|
248
|
+
def render_autocomplete form, rec, options
|
249
|
+
t = rec.class.name.underscore
|
250
|
+
#id = "#{t}_#{@widget_config.name.to_s}"
|
251
|
+
id = options[:id] || form.tag_id(@widget_config.name)
|
252
|
+
name = @widget_config.name.to_s.foreign_key.to_sym
|
253
|
+
opt = {}
|
254
|
+
opt[:id] = "dialog_#{form.tag_id name}" if @dialog
|
255
|
+
s = form.hidden_field name, opt
|
256
|
+
s += form.text_field @widget_config.name, options.merge(:size => (@widget_config.size || 40))
|
257
|
+
s += @view.content_tag("div", "", :id => "#{id}_auto_complete", :class => "auto_complete")
|
258
|
+
s += @view.auto_complete_field(id,
|
259
|
+
:url => @view.send(@widget_config.url),
|
260
|
+
:method => :get,
|
261
|
+
:param_name => :autocomplete_query,
|
262
|
+
:select => 'autocomplete_name',
|
263
|
+
:after_update_element => 'auto_complete_update_hidden')
|
264
|
+
s
|
265
|
+
end
|
266
|
+
|
267
|
+
def render_radio form, rec, options
|
268
|
+
s = ''
|
269
|
+
@widget_config.values.each do |val|
|
270
|
+
s += form.radio_button @widget_config.name, val[1].to_s, options
|
271
|
+
s += @view.content_tag('span', val[0])
|
272
|
+
end
|
273
|
+
s
|
274
|
+
end
|
275
|
+
|
276
|
+
def render_checkbox form, rec, options
|
277
|
+
form.check_box @widget_config.name, options
|
278
|
+
end
|
279
|
+
|
280
|
+
def render_date form, rec, options
|
281
|
+
form.calendar_date_select @widget_config.name, options.merge(:size => (@widget_config.size || 15))
|
282
|
+
end
|
283
|
+
|
284
|
+
def render_textfield form, rec, options
|
285
|
+
val = rec.send @widget_config.name
|
286
|
+
text = val.to_s
|
287
|
+
text = val.to_s(:local_date) if val.instance_of? Date
|
288
|
+
text = val.to_s(:local_time) if val.instance_of? Time
|
289
|
+
text = val.to_s(:local_datetime) if val.instance_of? ActiveSupport::TimeWithZone
|
290
|
+
form.text_field @widget_config.name, options.merge({:size => (@widget_config.size || 40), :value => text})
|
291
|
+
end
|
292
|
+
|
293
|
+
def render_textarea form, rec, options
|
294
|
+
size = @widget_config.size || '40x10'
|
295
|
+
val = rec.send @widget_config.name
|
296
|
+
if val
|
297
|
+
row = 0
|
298
|
+
col = 0
|
299
|
+
val.each_line do |line|
|
300
|
+
row = row.succ
|
301
|
+
col = [col, line.length].max
|
302
|
+
end
|
303
|
+
size = "#{col}x#{row}"
|
304
|
+
end
|
305
|
+
form.text_area @widget_config.name, options.merge({:size => size})
|
306
|
+
end
|
307
|
+
|
241
308
|
end
|
242
309
|
|
243
310
|
class ActionRenderer < Renderer
|
@@ -266,56 +333,65 @@ module Toolbox
|
|
266
333
|
model_name = @widget_config.model_name
|
267
334
|
return nil if action == current_action
|
268
335
|
end
|
269
|
-
css_class = @widget_config.
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
@view.
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
end
|
298
|
-
when 'list'
|
299
|
-
@view.link_to label, @view.send("#{model_name.pluralize}_path".to_sym), :class => css_class
|
300
|
-
when 'edit'
|
301
|
-
url = @view.edit_polymorphic_path rec
|
302
|
-
if context
|
303
|
-
@view.link_to_remote label, { :url => url, :method => :get }, {:class => css_class }
|
304
|
-
else
|
305
|
-
@view.link_to label, url
|
306
|
-
end
|
307
|
-
else
|
308
|
-
url = @widget_config.url || @view.formatted_polymorphic_path([rec, action]) #eval("@view.#{action}_#{model_name}_path(rec)")
|
309
|
-
if context
|
310
|
-
if @widget_config.direct_link
|
311
|
-
@view.link_to label, url, :class => css_class
|
336
|
+
css_class = @widget_config.active == 1 ? 'enabled ' : 'disabled '
|
337
|
+
css_class += @widget_config.css_class || action
|
338
|
+
|
339
|
+
if @widget_config.active == 1
|
340
|
+
case action
|
341
|
+
when 'separator'
|
342
|
+
@widget_config.name if context
|
343
|
+
when 'new'
|
344
|
+
# copy the _id params to save the context
|
345
|
+
pars = {}
|
346
|
+
pars.merge! context if context
|
347
|
+
# delete self-reference
|
348
|
+
pars.delete model_name + '_id'
|
349
|
+
# set the object as parameter
|
350
|
+
pars[rec.class.name.underscore + '_id'] = rec #if foreign_action
|
351
|
+
url = @view.send("#{action}_#{model_name}_path".to_sym, pars)
|
352
|
+
if context || foreign_action
|
353
|
+
@view.link_to_remote label, { :url => url, :method => :get }, {:class => css_class }
|
354
|
+
else
|
355
|
+
@view.link_to label, url, {:class => css_class}
|
356
|
+
end
|
357
|
+
when 'show'
|
358
|
+
@view.link_to label, @view.polymorphic_path(rec), :class => css_class
|
359
|
+
when 'destroy'
|
360
|
+
msg = @view.send :_, 'Are you sure?'
|
361
|
+
if context
|
362
|
+
url = @view.polymorphic_path(rec)
|
363
|
+
@view.link_to_remote label, { :url => url, :method => :delete, :confirm => msg }, {:class => css_class }
|
312
364
|
else
|
365
|
+
@view.link_to label, rec, :confirm => msg, :method => :delete, :class => css_class
|
366
|
+
end
|
367
|
+
when 'list'
|
368
|
+
@view.link_to label, @view.send("#{model_name.pluralize}_path".to_sym), :class => css_class
|
369
|
+
when 'edit'
|
370
|
+
url = @view.edit_polymorphic_path rec
|
371
|
+
if context
|
313
372
|
@view.link_to_remote label, { :url => url, :method => :get }, {:class => css_class }
|
373
|
+
else
|
374
|
+
@view.link_to label, url
|
375
|
+
end
|
376
|
+
else
|
377
|
+
url = @widget_config.url || @view.formatted_polymorphic_path([rec, action]) #eval("@view.#{action}_#{model_name}_path(rec)")
|
378
|
+
if context
|
379
|
+
if @widget_config.direct_link
|
380
|
+
@view.link_to label, url, :class => css_class
|
381
|
+
else
|
382
|
+
@view.link_to_remote label, { :url => url, :method => :get }, {:class => css_class }
|
383
|
+
end
|
384
|
+
else
|
385
|
+
@view.link_to label, url
|
314
386
|
end
|
387
|
+
end # case
|
388
|
+
else # if disabled
|
389
|
+
if context
|
390
|
+
@view.content_tag :a, label, :href => '#', :class => css_class
|
315
391
|
else
|
316
|
-
|
392
|
+
label
|
317
393
|
end
|
318
|
-
end
|
394
|
+
end
|
319
395
|
end
|
320
396
|
end
|
321
397
|
end
|
data/lib/toolbox/version.rb
CHANGED