lore 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. data/LICENSE +19 -0
  2. data/README +74 -0
  3. data/aspect.rb +80 -0
  4. data/behaviours/lockable.rb +41 -0
  5. data/behaviours/movable.rb +54 -0
  6. data/behaviours/versioned.rb +24 -0
  7. data/benchmark.rb +193 -0
  8. data/bits.rb +52 -0
  9. data/cache/abstract_entity_cache.rb +82 -0
  10. data/cache/bits.rb +22 -0
  11. data/cache/cacheable.rb +202 -0
  12. data/cache/cached_entities.rb +116 -0
  13. data/cache/file_index.rb +35 -0
  14. data/cache/mmap_entity_cache.rb +67 -0
  15. data/clause.rb +528 -0
  16. data/connection.rb +155 -0
  17. data/custom_functions.sql +14 -0
  18. data/exception/ambiguous_attribute.rb +14 -0
  19. data/exception/cache_exception.rb +30 -0
  20. data/exception/invalid_klass_parameters.rb +63 -0
  21. data/exception/invalid_parameter.rb +42 -0
  22. data/exception/unknown_typecode.rb +19 -0
  23. data/file_index.sql +56 -0
  24. data/gui/erb_template.rb +79 -0
  25. data/gui/erb_template_helpers.rhtml +19 -0
  26. data/gui/form.rb +314 -0
  27. data/gui/form_element.rb +676 -0
  28. data/gui/form_generator.rb +151 -0
  29. data/gui/templates/button.rhtml +2 -0
  30. data/gui/templates/checkbox.rhtml +3 -0
  31. data/gui/templates/checkbox_row.rhtml +1 -0
  32. data/gui/templates/file.rhtml +2 -0
  33. data/gui/templates/file_readonly.rhtml +3 -0
  34. data/gui/templates/form_element.rhtml +5 -0
  35. data/gui/templates/form_element_horizontal.rhtml +3 -0
  36. data/gui/templates/form_element_listed.rhtml +8 -0
  37. data/gui/templates/form_table.rhtml +3 -0
  38. data/gui/templates/form_table_blank.rhtml +3 -0
  39. data/gui/templates/form_table_horizontal.rhtml +8 -0
  40. data/gui/templates/password.rhtml +2 -0
  41. data/gui/templates/password_readonly.rhtml +3 -0
  42. data/gui/templates/radio.rhtml +1 -0
  43. data/gui/templates/radio_row.rhtml +1 -0
  44. data/gui/templates/select.rhtml +23 -0
  45. data/gui/templates/text.rhtml +2 -0
  46. data/gui/templates/text_readonly.rhtml +3 -0
  47. data/gui/templates/textarea.rhtml +3 -0
  48. data/gui/templates/textarea_readonly.rhtml +4 -0
  49. data/lore.gemspec +40 -0
  50. data/lore.rb +94 -0
  51. data/migration.rb +48 -0
  52. data/model.rb +139 -0
  53. data/model_factory.rb +202 -0
  54. data/model_shortcuts.rb +16 -0
  55. data/query_shortcuts.rb +367 -0
  56. data/reserved_methods.txt +3 -0
  57. data/result.rb +100 -0
  58. data/symbol.rb +58 -0
  59. data/table_accessor.rb +1926 -0
  60. data/table_deleter.rb +115 -0
  61. data/table_inserter.rb +168 -0
  62. data/table_instance.rb +384 -0
  63. data/table_selector.rb +314 -0
  64. data/table_updater.rb +155 -0
  65. data/test/README +31 -0
  66. data/test/env.rb +5 -0
  67. data/test/lore_test.log +8218 -0
  68. data/test/model.rb +142 -0
  69. data/test/prepare.rb +37 -0
  70. data/test/tc_aspect.rb +58 -0
  71. data/test/tc_cache.rb +80 -0
  72. data/test/tc_clause.rb +104 -0
  73. data/test/tc_deep_inheritance.rb +49 -0
  74. data/test/tc_factory.rb +57 -0
  75. data/test/tc_filter.rb +37 -0
  76. data/test/tc_form.rb +32 -0
  77. data/test/tc_model.rb +86 -0
  78. data/test/tc_prepare.rb +45 -0
  79. data/test/tc_refined_query.rb +88 -0
  80. data/test/tc_table_accessor.rb +265 -0
  81. data/test/test.log +181 -0
  82. data/test/test_db.sql +400 -0
  83. data/test/ts_lore.rb +49 -0
  84. data/types.rb +55 -0
  85. data/validation/message.rb +60 -0
  86. data/validation/parameter_validator.rb +104 -0
  87. data/validation/reason.rb +54 -0
  88. data/validation/type_validator.rb +91 -0
  89. data/validation.rb +65 -0
  90. metadata +170 -0
@@ -0,0 +1,676 @@
1
+
2
+ require('lore/gui/erb_template')
3
+
4
+ module Lore
5
+ module GUI
6
+
7
+ # Form element that can be handled by Cuba::GUI::Form.
8
+ class Form_Element
9
+ # {{{
10
+
11
+ attr_accessor :attribute_table, :attribute_name, :attribute_id, :attribute_range, :attribute_value, :attribute_label, :template, :on_click, :on_change, :id, :mode, :style_class, :template_file, :style
12
+
13
+ def self.for(clause, label, value='', range=nil)
14
+ attrib = clause.to_s.split('.')
15
+ if attrib.length == 3 then
16
+ new(attrib[0..1].join('.'), attrib[2], label, range, value)
17
+ else
18
+ new(nil, clause.to_s, label, range, value)
19
+ end
20
+ end
21
+
22
+ # Expects table name and attribute this element is referring to, as well as
23
+ # label to be used for it.
24
+ # _attrib_range is an array of option values, e.g. needed for select boxes or radio
25
+ # groups, _attrib_value is the elements current value if existing.
26
+ def setup(_attrib_table, _attrib_name, _attrib_label,
27
+ _attrib_range=nil, _attrib_value='')
28
+
29
+ @attribute_table = _attrib_table
30
+ @attribute_label = _attrib_label
31
+ @attribute_name = _attrib_name.to_s
32
+ @attribute_range = _attrib_range
33
+ @attribute_value = _attrib_value
34
+ @attribute_id = _attrib_table.to_s.gsub('.','_') + '__' << _attrib_name.to_s
35
+ @mode = :mutable
36
+ @template = ERB_Template
37
+ @template_file = nil
38
+
39
+ @style = ''
40
+ @style_class = :lore
41
+
42
+ @id = ''
43
+ @on_click = ''
44
+ @on_change = ''
45
+
46
+ if @attribute_range.instance_of? Range then
47
+ @attribute_range = @attribute_range.to_a
48
+ end
49
+
50
+ end
51
+
52
+ # Set value for this element.
53
+ def set_attribute_value(val)
54
+ @attribute_value = val
55
+ end
56
+
57
+ # Set style class for element.
58
+ def set_attribute_style(style)
59
+ @style = style
60
+ end
61
+
62
+ # Set mode for this element.
63
+ # Modes are :mutable and :readonly.
64
+ def set_mode(mode=:mutable)
65
+ @mode = mode
66
+ end
67
+
68
+ # Returns this element as html string. To be overloaded
69
+ # by subclasses.
70
+ def string
71
+ ''
72
+ end
73
+
74
+ # Returns attribute label as string.
75
+ def label
76
+ if @attribute_label.nil? then '' else @attribute_label end
77
+ end
78
+
79
+ # Prints string()
80
+ def print
81
+ puts string()
82
+ end
83
+
84
+ def onfocus(attribute_id)
85
+ begin
86
+ context_help = "Element.addClassName('#{attribute_id.gsub('.','_')}_wrap', 'focussed' ); Element.addClassName('#{attribute_id.gsub('.','_')}', 'focussed' );"
87
+ rescue ::Exception => e
88
+ context_help = ''
89
+ end
90
+
91
+ return context_help
92
+ end
93
+
94
+ def onblur(attribute_id)
95
+ begin
96
+ context_help = "Element.removeClassName('#{attribute_id.gsub('.','_')}_wrap', 'focussed' ); Element.removeClassName('#{attribute_id.gsub('.','_')}', 'focussed' );"
97
+ rescue ::Exception => e
98
+ context_help = ''
99
+ end
100
+
101
+ return context_help
102
+ end
103
+
104
+ end # class }}}
105
+
106
+ class Custom_Element < Form_Element
107
+
108
+ def initialize(_table, _attrib_name, _attrib_label, _attrib_range=nil, _attrib_value=nil)
109
+ setup(_table, _attrib_name, _attrib_label, _attrib_range, _attrib_value)
110
+ end
111
+
112
+ # To be overloaded
113
+ def string
114
+ 'Custom_Element#string not redefined!'
115
+ end
116
+
117
+ end # class Custom_Element
118
+
119
+ # Form text element (<input type="text" ...>).
120
+ # Subclass of Form_Element.
121
+ class Text < Form_Element
122
+ # {{{
123
+
124
+ def initialize(_table, _attrib_name, _attrib_label, _attrib_range=nil, _attrib_value=nil)
125
+ setup(_table, _attrib_name, _attrib_label, _attrib_range, _attrib_value)
126
+ @template_file = 'text.rhtml'
127
+ @readonly_template_file = 'text_readonly.rhtml'
128
+ end
129
+
130
+ def set_length(length)
131
+ @length = length
132
+ end
133
+
134
+ def string
135
+ # to avoid value=""
136
+ if @attribute_value == '' then @attribute_value = nil end
137
+
138
+ if @mode == :mutable then
139
+ template = @template.new(@template_file)
140
+
141
+ if @attribute_table.to_s == '' then
142
+ name = @attribute_name
143
+ else
144
+ name = @attribute_table + '.' + @attribute_name
145
+ end
146
+ if @length.nil? then
147
+ data = {
148
+ :id => @attribute_id,
149
+ :class => @style_class.to_s,
150
+ :name => name,
151
+ :value => @attribute_value,
152
+ :style => @style,
153
+ :onfocus => onfocus("#{@attribute_table.to_s.gsub('.','_')}__#{@attribute_name.to_s}"),
154
+ :onblur => onblur("#{@attribute_table.to_s.gsub('.','_')}__#{@attribute_name.to_s}")
155
+ }
156
+ else
157
+ data = {
158
+ :id => @attribute_id,
159
+ :class => @style_class.to_s,
160
+ :name => name,
161
+ :maxlength => @length,
162
+ :value => @attribute_value,
163
+ :style => @style,
164
+ :onfocus => onfocus("#{@attribute_table.to_s.gsub('.','_')}__#{@attribute_name.to_s}"),
165
+ :onblur => onblur("#{@attribute_table.to_s.gsub('.','_')}__#{@attribute_name.to_s}")
166
+ }
167
+ end
168
+
169
+ elsif @mode == :readonly then
170
+ template = @template.new(@readonly_template_file)
171
+ data = { :value => @attribute_value }
172
+ end
173
+ template.set_data(:attributes => data)
174
+ template.string()
175
+ end
176
+
177
+ end # class }}}
178
+
179
+ class Password < Text
180
+ def initialize(_table, _attrib_name, _attrib_label, _attrib_range=nil, _attrib_value=nil)
181
+ setup(_table, _attrib_name, _attrib_label, _attrib_range, _attrib_value)
182
+ @template_file = 'password.rhtml'
183
+ @readonly_template_file = 'password_readonly.rhtml'
184
+ end
185
+ end
186
+
187
+ # Form file element (<input type="file" ...>).
188
+ # Subclass of Form_Element.
189
+ class File < Form_Element
190
+ # {{{
191
+
192
+ def initialize(_table, _attrib_name, _attrib_label, _attrib_range=nil, _attrib_value=nil)
193
+ setup(_table, _attrib_name, _attrib_label, _attrib_range, _attrib_value)
194
+ end
195
+
196
+ def string
197
+ if @mode == :mutable then
198
+ template = @template.new('file.rhtml')
199
+ elsif @mode == :readonly then
200
+ template = @template.new('text_readonly.rhtml')
201
+ end
202
+ # to avoid value=""
203
+ if @attribute_value == '' then @attribute_value = nil end
204
+ if @mode == :mutable then
205
+ if @attribute_table.to_s == '' then
206
+ name = @attribute_name
207
+ else
208
+ name = @attribute_table + '.' + @attribute_name
209
+ end
210
+
211
+ data = {
212
+ :id => @attribute_id,
213
+ :class => @style_class.to_s,
214
+ :name => name,
215
+ :value => @attribute_value,
216
+ :style => @style
217
+ }
218
+ elsif @mode == :readonly then
219
+ data[:value] = @attribute_value
220
+ end
221
+ template.set_data(:attributes => data)
222
+ template.string()
223
+ end
224
+
225
+ end # class }}}
226
+
227
+ # Form textarea element (<textarea ...></textarea>).
228
+ # Subclass of Form_Element.
229
+ class Textarea < Form_Element
230
+ # {{{
231
+
232
+ @@textarea_count = 0
233
+
234
+ def initialize(_table, _attrib_name, _attrib_label, _attrib_range=nil, _attrib_value=nil)
235
+ setup(_table, _attrib_name, _attrib_label, _attrib_range, _attrib_value)
236
+ end
237
+
238
+ def self.reset_counter
239
+ @@textarea_count = 0
240
+ end
241
+
242
+ def string
243
+
244
+ @@textarea_count += 1
245
+ @attribute_id = 'lore_textarea_' << @@textarea_count.to_s if @mode == :mutable
246
+
247
+ if @attribute_value.nil? then @attribute_value = '' end
248
+
249
+ if @mode == :mutable then
250
+ template = @template.new('textarea.rhtml')
251
+ elsif @mode == :readonly then
252
+ template = @template.new('textarea_readonly.rhtml')
253
+ end
254
+
255
+ data = {
256
+ :attributes => {
257
+ :id => @attribute_id,
258
+ :class => @style_class.to_s,
259
+ :name => @attribute_table+'.'+@attribute_name,
260
+ :style => @style,
261
+ :onfocus => onfocus("#{@attribute_id}"),
262
+ :onblur => onblur("#{@attribute_id}"),
263
+ },
264
+ :value => @attribute_value
265
+ }
266
+ # data[:attributes][:id] = 'lore_textarea_' << @@textarea_count.to_s if @mode == :mutable
267
+ template.set_data(data)
268
+ template.string()
269
+ end
270
+
271
+ end # class }}}
272
+
273
+ # Form date element (three select boxes for year/month/day).
274
+ # Subclass of Form_Element.
275
+ class Date < Form_Element
276
+ # {{{
277
+
278
+ @@date_field_counter = 1
279
+
280
+ def initialize(_table, _attrib_name, _attrib_label, _attrib_range=nil, _attrib_value=nil)
281
+ setup(_table, _attrib_name, _attrib_label, _attrib_range, _attrib_value)
282
+ end
283
+
284
+ def string
285
+
286
+ @@date_field_counter += 1
287
+ idx = @@date_field_counter.to_s
288
+ if @mode == :mutable then
289
+ return '<input class="lore_date" type="text" name="'+@attribute_name+'" value="'+@attribute_value.to_s+'" id="date_field_'+idx+'" />
290
+ <input class="lore_button_varwidth" type="button" value="..." id="date_trigger_'+idx+'" onclick="open_calendar(\'date_field_'+idx+'\',\'date_trigger_'+idx+'\');" />'
291
+ else
292
+ return '<nobr>' << @attribute_value.to_s.gsub(' ','.') + '</nobr>'
293
+ end
294
+
295
+ if @attribute_range.nil? then
296
+ @attribute_range = (2005..2008).to_a
297
+ else
298
+ @attribute_range = @attribute_range.to_a
299
+ end
300
+
301
+ select_year = Select.new( @attribute_table,
302
+ @attribute_name+'--year',
303
+ nil,
304
+ ['']+@attribute_range,
305
+ @attribute_value.to_s[0..3])
306
+ select_month = Select.new( @attribute_table,
307
+ @attribute_name+'--month',
308
+ nil,
309
+ ['']+('01'..'12').to_a,
310
+ @attribute_value.to_s[5..6])
311
+ select_day = Select.new( @attribute_table,
312
+ @attribute_name+'--day',
313
+ nil,
314
+ ['']+('01'..'31').to_a,
315
+ @attribute_value.to_s[8..9])
316
+
317
+ select_year.set_attribute_value(@attribute_value.to_s[0..3])
318
+ select_month.set_attribute_value(@attribute_value.to_s[5..6])
319
+ select_day.set_attribute_value(@attribute_value.to_s[8..9])
320
+
321
+ select_year.set_mode(@mode)
322
+ select_month.set_mode(@mode)
323
+ select_day.set_mode(@mode)
324
+
325
+ return select_day.string+select_month.string+select_year.string
326
+ end
327
+
328
+ end # class }}}
329
+
330
+ # Form radio element (<input type="radio" ...>).
331
+ # Subclass of Form_Element.
332
+ class Radio < Form_Element
333
+ # {{{
334
+
335
+ def initialize(_table, _attrib_name, _attrib_label, _attrib_range=nil, _attrib_value=nil)
336
+ setup(_table, _attrib_name, _attrib_label, _attrib_range, _attrib_value)
337
+
338
+ if _attrib_label.instance_of? Array then
339
+ @attribute_label = _attrib_label[0]
340
+ @attribute_sublabel = _attrib_label[1]
341
+ else
342
+ @attribute_label = _attrib_label.to_s
343
+ @attribute_sublabel = nil
344
+ end
345
+
346
+ end # def
347
+
348
+ def string
349
+
350
+ if @mode == :mutable then
351
+
352
+ template = @template.new('radio.rhtml')
353
+ radios = ''
354
+ radio_index = 0
355
+
356
+ if @attribute_label.nil? or @attribute_label == '' then
357
+ label = ''
358
+ end
359
+
360
+ if @attribute_value.nil? or @attribute_value == '' then
361
+ @attribute_value = @attribute_range[0]
362
+ end
363
+
364
+ throw 'attribute range must not be nil for Cuba::GUI::Radio instance. ' if @attribute_range.nil?
365
+
366
+ @attribute_range.each { |value|
367
+
368
+ if !@attribute_sublabel.nil? and
369
+ !@attribute_sublabel[value].nil? then
370
+ label = @attribute_sublabel[value]
371
+ else
372
+ label = value
373
+ end
374
+ label = Lang[label.intern]
375
+
376
+ data = {
377
+ :id => @attribute_id,
378
+ :class => @style_class.to_s + '_radio',
379
+ :name => @attribute_table+'.'+@attribute_name,
380
+ :onfocus => onfocus("#{@attribute_table.gsub('.','_')}__#{@attribute_name}"),
381
+ :onblur => onblur("#{@attribute_table.gsub('.','_')}__#{@attribute_name}"),
382
+ :value => value,
383
+ :label => label
384
+ }
385
+ data[:checked] = 'checked' if value == @attribute_value
386
+
387
+ template.set_data(:attributes => data)
388
+ radios += template.string.gsub("\n",'')
389
+ radio_index += 1
390
+ }
391
+
392
+ elsif @mode == :readonly then
393
+
394
+ template = @template.new('text_readonly.rhtml')
395
+ if !@attribute_sublabel.nil? and
396
+ !@attribute_sublabel[@attribute_value].nil? then
397
+ label = @attribute_sublabel[@attribute_value]
398
+ else
399
+ label = @attribute_value
400
+ end
401
+ label = Lang[label.intern]
402
+ data = {:value => label}
403
+ template.set_data(:attributes => data)
404
+ radios = template.string
405
+
406
+ end
407
+
408
+ row_template = @template.new('radio_row.rhtml')
409
+ row_template.set_data(:radios => radios)
410
+ row_template.string
411
+
412
+ end # def
413
+
414
+ end # class }}}
415
+
416
+ # Form checkbox element (<input type="checkbox" ...>).
417
+ # Subclass of Form_Element.
418
+ class Checkbox < Form_Element
419
+ # {{{
420
+
421
+ def initialize(_table, _attrib_name, _attrib_label, _attrib_range=nil, _attrib_value=nil)
422
+ setup(_table, _attrib_name, _attrib_label, _attrib_range, _attrib_value)
423
+
424
+ if _attrib_label.instance_of? Array then
425
+ @attribute_label = _attrib_label[0]
426
+ @attribute_sublabel = _attrib_label[1]
427
+ else
428
+ @attribute_label = _attrib_label.to_s
429
+ @attribute_sublabel = nil
430
+ end
431
+ @style_class = :lore_checkbox
432
+
433
+ end # def
434
+
435
+ def string
436
+
437
+ if @mode == :mutable then
438
+
439
+ template = @template.new('checkbox.rhtml')
440
+ checkboxes = ''
441
+ checkbox_index = 0
442
+
443
+ if @attribute_label.nil? or @attribute_label == '' then label = '' end
444
+
445
+ @attribute_range.each { |value|
446
+
447
+ if !@attribute_sublabel.nil? and
448
+ !@attribute_sublabel[value].nil? then
449
+ label = @attribute_sublabel[value]
450
+ else
451
+ # Allow label to be empty, don't use value
452
+ # if not set.
453
+ # label = value
454
+ end
455
+
456
+ if value == @attribute_value then
457
+ data = {
458
+ :id => @attribute_id,
459
+ :class => @style_class.to_s,
460
+ :name => @attribute_table+'.'+@attribute_name,
461
+ :onfocus => onfocus("#{@attribute_table}__#{@attribute_name}"),
462
+ :onblur => onblur("#{@attribute_table}__#{@attribute_name}"),
463
+ :value => value,
464
+ :checked => 'checked'
465
+ }
466
+ else
467
+ data = {
468
+ :id => @attribute_id,
469
+ :class => @style_class.to_s,
470
+ :onfocus => onfocus("#{@attribute_table}__#{@attribute_name}"),
471
+ :onblur => onblur("#{@attribute_table}__#{@attribute_name}"),
472
+ :name => @attribute_table+'.'+@attribute_name,
473
+ :value => value
474
+ }
475
+ end
476
+ template.set_data(:attributes => data)
477
+ checkboxes += template.string
478
+ checkboxes += label.to_s
479
+ checkbox_index += 1
480
+ }
481
+
482
+ elsif @mode == :readonly then
483
+
484
+ template = @template.new('text_readonly.rhtml')
485
+ if !@attribute_sublabel.nil? and
486
+ !@attribute_sublabel[@attribute_value].nil? then
487
+ label = @attribute_sublabel[@attribute_value]
488
+ else
489
+ label = @attribute_value
490
+ end
491
+ data = {:label => label}
492
+ template.set_data(data)
493
+ checkboxes = template.string
494
+
495
+ end
496
+
497
+ row_template = @template.new('checkbox_row.rhtml')
498
+ row_template.set_data(:checkboxes => checkboxes)
499
+ row_template.string
500
+ end # def
501
+
502
+ end # class }}}
503
+
504
+ # Form select box element (<input type="select" ...><option ...>).
505
+ # Subclass of Form_Element.
506
+ class Select < Form_Element
507
+ # {{{
508
+
509
+ attr_accessor :on_change, :on_blur, :on_focus, :id, :attribute_id
510
+
511
+ def initialize(_table, _attrib_name, _attrib_label, _attrib_range=nil, _attrib_value=nil, sublabels=nil)
512
+ setup(_table, _attrib_name, _attrib_label, _attrib_range, _attrib_value)
513
+ @labels = sublabels
514
+ end # def
515
+
516
+ def string
517
+
518
+ if !@labels.instance_of? Array
519
+ then @labels = @attribute_range
520
+ end
521
+
522
+ if @mode == :readonly then
523
+
524
+ template = @template.new('text_readonly.rhtml')
525
+ index = @attribute_range.index(@attribute_value.to_s)
526
+ if index.nil? then index = @attribute_range.index(@attribute_value.to_i) end
527
+
528
+ label = @labels[index.to_i]
529
+ data = { :attributes => { :value => label }}
530
+
531
+ elsif @mode == :mutable or @mode == nil then
532
+
533
+ @on_focus = onfocus("#{@attribute_table.to_s.gsub('.','_')}__#{@attribute_name.to_s}")
534
+ @on_blur = onblur("#{@attribute_table.to_s.gsub('.','_')}__#{@attribute_name.to_s}")
535
+
536
+ template = @template.new('select.rhtml')
537
+ data = {
538
+ :attributes => {
539
+ # :id => @id.to_s,
540
+ :id => @attribute_id,
541
+ :class => @style_class.to_s,
542
+ :style => @style,
543
+ :name => @attribute_name,
544
+ :onfocus => @on_focus.to_s,
545
+ :onChange => @on_change.to_s,
546
+ :onblur => @on_blur.to_s,
547
+ :value => @attribute_value
548
+ },
549
+ :range => @attribute_range,
550
+ :labels => @labels,
551
+ :value => @attribute_value
552
+ }
553
+ end
554
+ template.set_data(data)
555
+ template.string()
556
+
557
+ end
558
+
559
+ end # class }}}
560
+
561
+ # This klass builts a select box for instances of a
562
+ # given klass. Needed for aggregates, has_a relationships
563
+ # etc.
564
+ # Subclass of Form_Element.
565
+ class Klass_Select < Form_Element
566
+ # {{{
567
+
568
+ def initialize(_klass, _attrib_table, _attrib_name, _attrib_label)
569
+ setup(_attrib_table, _attrib_name, _attrib_label, nil, nil)
570
+ @klass = _klass
571
+ end
572
+
573
+ def string
574
+
575
+ # predefs:
576
+ range = Array.new
577
+ sublabels = Array.new
578
+ foreign_label = key_string = nil
579
+ foreign_table = @klass.table_name
580
+ selectables = @klass.select { |foreign|
581
+ foreign.where(true)
582
+ foreign.order_by(@klass.get_primary_keys[foreign_table].first, :asc)
583
+ }
584
+
585
+ selectables.each { |foreign|
586
+ # foreign_label = foreign.get_attribute_values[foreign_table][@klass.get_label.split('.')[-1]]
587
+ foreign_label = ''
588
+ if @klass.get_labels.nil? then
589
+ raise ::Exception.new('Specify a label for has_a - related model klasses (here: ' << @klass.to_s + ') via "use_label".')
590
+ end
591
+ @klass.get_labels.each { |label_attrib|
592
+ foreign_label << foreign.get_attribute_values[foreign_table][label_attrib.split('.')[-1]].to_s << ' '
593
+ }
594
+ sublabels.push(foreign_label)
595
+
596
+ @klass.get_primary_keys[foreign_table].uniq.each { |keys|
597
+ key_string = ''
598
+ keys.each { |key|
599
+ # concatenate combined primary keys like 'id--id2' -> '3--4'
600
+ if key_string != '' then key_string += '--' end
601
+ key_string += foreign.get_attribute_values[foreign_table][key]
602
+ }
603
+ range.push(key_string)
604
+ }
605
+ }
606
+
607
+ select_element = Select.new(@attribute_table,
608
+ @attribute_name,
609
+ @attribute_label,
610
+ range, nil, sublabels)
611
+ select_element.set_attribute_value(@attribute_value)
612
+ select_element.set_attribute_style(@style)
613
+ select_element.set_mode(@mode)
614
+
615
+ return select_element.string
616
+
617
+ end
618
+
619
+
620
+ end # class }}}
621
+
622
+ # Form button element (<input type="button" ... />).
623
+ # Subclass of Form_Element.
624
+ class Button < Form_Element
625
+ # {{{
626
+ attr_accessor :template
627
+
628
+ def initialize(_type, _value, _style_class=nil)
629
+ @button_type = _type
630
+ @button_label = _value
631
+ @attribute_value = _value
632
+ @style_class = _style_class
633
+ @template = ERB_Template
634
+ end
635
+
636
+ def string
637
+
638
+ template = @template.new('button.rhtml')
639
+
640
+ data = {
641
+ :attributes => {
642
+ :id => @attribute_id,
643
+ :class => @style_class.to_s,
644
+ :type => @button_type,
645
+ :value => @button_label
646
+ }
647
+ }
648
+
649
+ template.set_data(data)
650
+ template.string
651
+
652
+ end
653
+
654
+ end # class }}}
655
+
656
+ # Hidden form element (<input type="hidden" ... />).
657
+ # Subclass of Form_Element.
658
+ class Hidden < Form_Element
659
+ # {{{
660
+
661
+ def initialize(_table, _attrib_name)
662
+ setup(_table, _attrib_name, '', nil, nil)
663
+ end
664
+
665
+ def string()
666
+ attrib = @attribute_name.to_s
667
+ if !@attribute_table.nil? and @attribute_table != '' then
668
+ attrib = @attribute_table+'.'+attrib
669
+ end
670
+ '<input type="hidden" name="'+attrib+'" value="'+@attribute_value.to_s+'" />'
671
+ end
672
+
673
+ end # class }}}
674
+
675
+ end # module
676
+ end # module