Class: Lore::GUI::Klass_Select

Inherits:
Form_Element show all
Defined in:
lib/lore/gui/form_element.rb

Overview

This klass builts a select box for instances of a given klass. Needed for aggregates, has_a relationships etc. Subclass of Form_Element.

Instance Attribute Summary

Attributes inherited from Form_Element

#attribute_id, #attribute_label, #attribute_name, #attribute_range, #attribute_table, #attribute_value, #id, #mode, #on_change, #on_click, #style, #style_class, #template, #template_file

Instance Method Summary collapse

Methods inherited from Form_Element

for, #label, #onblur, #onfocus, #print, #set_attribute_style, #set_attribute_value, #set_mode, #setup

Constructor Details

#initialize(_klass, _attrib_table, _attrib_name, _attrib_label) ⇒ Klass_Select

{{{



568
569
570
571
# File 'lib/lore/gui/form_element.rb', line 568

def initialize(_klass, _attrib_table, _attrib_name, _attrib_label)
  setup(_attrib_table, _attrib_name, _attrib_label, nil, nil)
  @klass = _klass
end

Instance Method Details

#stringObject



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/lore/gui/form_element.rb', line 573

def string
  
  # predefs: 
  range         = Array.new
  sublabels     = Array.new
  foreign_label = key_string = nil
  foreign_table = @klass.table_name
  selectables = @klass.select { |foreign|
    foreign.where(true)
    foreign.order_by(@klass.get_primary_keys[foreign_table].first, :asc)
  }
  
  selectables.each { |foreign| 
#       foreign_label = foreign.get_attribute_values[foreign_table][@klass.get_label.split('.')[-1]]
    foreign_label = ''
    if @klass.get_labels.nil? then
      raise ::Exception.new('Specify a label for has_a - related model klasses (here: ' << @klass.to_s + ') via "use_label".')
    end
    @klass.get_labels.each { |label_attrib|
      foreign_label << foreign.get_attribute_values[foreign_table][label_attrib.split('.')[-1]].to_s << ' '
    }
    sublabels.push(foreign_label)

    @klass.get_primary_keys[foreign_table].uniq.each { |keys|
      key_string = ''
      keys.each { |key|
        # concatenate combined primary keys like 'id--id2' -> '3--4'
        if key_string != '' then key_string += '--' end
        key_string += foreign.get_attribute_values[foreign_table][key]
      }
      range.push(key_string)
    }
  }

  select_element = Select.new(@attribute_table, 
                              @attribute_name, 
                              @attribute_label, 
                              range, nil, sublabels)
  select_element.set_attribute_value(@attribute_value)
  select_element.set_attribute_style(@style)
  select_element.set_mode(@mode)

  return select_element.string

end