Class: Lore::GUI::Form_Generator

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

Overview

A factory rendering Aurita::GUI:Form instances for Aurita::Model classes.

Usage:

generator = Lore_Form_Generator.new(Some_Lore_Model)
generator.params = { :action => '/aurita/dispatch', :onsubmit => "alert('submitting'); " }
generator.generate
puts generator.form

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass = nil) ⇒ Form_Generator

Returns a new instance of Form_Generator.



24
25
26
27
28
29
30
# File 'lib/lore/gui/form_generator.rb', line 24

def initialize(klass=nil)
  @klass           = klass
  @labels          = {}
  @params          = {}
  @custom_elements = {}
  @form            = false
end

Instance Attribute Details

#custom_elementsObject

Returns the value of attribute custom_elements.



22
23
24
# File 'lib/lore/gui/form_generator.rb', line 22

def custom_elements
  @custom_elements
end

#formObject (readonly)

Returns the value of attribute form.



21
22
23
# File 'lib/lore/gui/form_generator.rb', line 21

def form
  @form
end

#klassObject (readonly)

Returns the value of attribute klass.



21
22
23
# File 'lib/lore/gui/form_generator.rb', line 21

def klass
  @klass
end

#labelsObject

Returns the value of attribute labels.



22
23
24
# File 'lib/lore/gui/form_generator.rb', line 22

def labels
  @labels
end

#paramsObject

Returns the value of attribute params.



22
23
24
# File 'lib/lore/gui/form_generator.rb', line 22

def params
  @params
end

Instance Method Details

#generateObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/lore/gui/form_generator.rb', line 37

def generate
  @form = Aurita::GUI::Form.new(@params)

  @klass.get_attributes.each_pair { |table, attributes|
    attributes.each { |attribute|
      label_tag    = table.gsub('.','--') << '--' << attribute
      label        = @labels[label_tag]
      label      ||= label_tag
      full_attrib  = table + '.' << attribute
      field_name   = full_attrib.gsub('.','_')
      form_element = false
      
      # @custom_elements is a hash mapping attribute names to
      # Custom_Element instances. 
      if ((@custom_elements[table]) and
          (@custom_elements[table][attribute])) then
        
        form_element = @custom_elements[table][attribute].new(table, attribute, label)
        
      elsif (@klass.get_primary_keys[table].nil? or                      # Ignore primary key attributes
             !@klass.get_primary_keys[table].include? attribute) and        
            (@klass.get_implicit_attributes[table].nil? or               # Ignore implicit attributes
             !@klass.get_implicit_attributes[table].include? attribute) and 
            (@klass.get_has_a_klasses.nil? or                            # Ignore attributes aggregated via has_a associations (added later)
             @klass.get_has_a_klasses[table+'.'+attribute].nil?) and        
            (@klass.get_hidden_attributes[table].nil? or                 # Ignore otherwise hidden attributes
             !@klass.get_hidden_attributes[table].include? attribute)
      then
      # Attribute has to be added to form, according to data type
        case @klass.get_attribute_types[table][attribute]
        when Lore::PG_BOOL
          form_element = Radio_Field.new(:label => label, 
                                         :name  => field_name, 
                                         :options => { 't' => 'yes', 'f' => 'no' } )
          
        when Lore::PG_SMALLINT || Lore::PG_INT
          form_element = Input_Field.new(:label => label, :name => field_name)
        when Lore::PG_VARCHAR
          form_element = Input_Field.new(:label => label, :name => field_name)
        when Lore::PG_TEXT
          form_element = Textarea_Field.new(:label => label, :name => field_name)
        when Lore::PG_TIMESTAMP_TIMEZONE
          form_element = Datetime_Field.new(:label => label, :name => field_name, :date_format => 'dmy', :time_format => 'hms', :year_range => (2009..2020))
        when Lore::PG_DATE
          form_element = Date_Field.new(:label => label, :name => field_name, :date_format => 'dmy', :year_range => (2009..2020))
        else
          form_element = Input_Field.new(:label => label, :name => field_name)
        end

      elsif (!@klass.get_has_a_klasses.nil? and
             !@klass.get_has_a_klasses[full_attrib].nil?)
      then 
        foreign_klass   = @klass.get_has_a_klasses[full_attrib]
        form_element    = Model_Select_Field.new(foreign_klass, :label => label, :name => field_name)

      elsif (!@klass.get_aggregate_klasses.nil? and
             !@klass.get_aggregate_klasses[full_attrib].nil?)
      then 
        foreign_klass   = @klass.get_aggregate_klasses[full_attrib]
        form_element    = Model_Select_Field.new(foreign_klass, :label => label, :name => field_name)
        
        # Attribute is explixit (expected/required) but not 
        # catched before -> Add attribute as hidden field: 
      elsif (!@klass.get_explicit_attributes[table].nil? and 
             @klass.get_explicit_attributes[table].include? attribute)
      then
        form_element    = Hidden_Field.new(:name => field_name)

      elsif (!@klass.get_implicit_attributes[table].nil? and 
             @klass.get_implicit_attributes[table].include? attribute)
      then
        # Implicit field, ignored
      end

      @form.add(form_element) if form_element
    }
  }
end