Class: Aurita::GUI::Lore_Model_Select_Field

Inherits:
Select_Field
  • Object
show all
Defined in:
lib/lore/gui/lore_model_select_field.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, params, &block) ⇒ Lore_Model_Select_Field

Returns a new instance of Lore_Model_Select_Field.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/lore/gui/lore_model_select_field.rb', line 9

def initialize(model, params, &block)
  @model        = model
  @filter       = params[:filter]
  @filter     ||= true
  if block_given? then
    @select_block = block
  else
    @select_block = Proc.new { |clause| clause.where(@filter) }
  end
  params.delete(:filter)
  super(params)
end

Instance Method Details

#option_elementsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lore/gui/lore_model_select_field.rb', line 22

def option_elements
  elements = []
  options  = []

  foreign_table = @model.table_name
  selectables = @model.select(&@select_block)
  selectables.each { |foreign| 
    foreign_label = ''
    if @model.get_labels.nil? then
      raise Aurita::GUI::Form_Error.new('Specify a label for has_a - related model klasses (here: ' << @model.to_s + ') via "use_label".')
    end
    @model.get_labels.each { |label_attrib|
      foreign_label << foreign.get_attribute_values[foreign_table][label_attrib.split('.')[-1]].to_s << ' '
    }

    @model.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]
      }
      options << { key_string => foreign_label }
    }
  }
  
  options.each { |map| 
    map.each_pair { |k,v|
      elements << HTML.option(:value => k) { v }
    }
  }
  elements
end