Class: Aurita::GUI::Form_Field

Inherits:
Element
  • Object
show all
Defined in:
lib/aurita-gui/form/form_field.rb

Overview

Class Form_Field is an abstract base class for built-in form fields (Input_Field, Hidden_Field, Radio_Field, Checkbox_Field, Select_Field, Textarea_Field) or any custom form field type. It is a wrapper for GUI::Element, extending it by parameters @name, @label and @value.

Usage:

i = Input_Field.new(:name => :description, 
                    :label => 'Description', 
                    :value => 'Lorem ipsum dolor')

Apart from this special attributes, Form_Field instances behave like any GUI::Element:

i.onclick = "alert('i have been clicked');"
i.class   = 'css_class'

Instance Attribute Summary collapse

Attributes inherited from Element

#attrib, #content, #parent, #tag

Instance Method Summary collapse

Methods inherited from Element

#+, #[], #[]=, #clear_floating, #dom_id, #dom_id=, #each, #empty?, #id, #id=, #length, #method_missing, #to_ary

Constructor Details

#initialize(params) ⇒ Form_Field

Returns a new instance of Form_Field.

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/aurita-gui/form/form_field.rb', line 33

def initialize(params)
  # @value  = params[:value]
  raise Form_Error.new('Must provide parameter :name for ' << self.class.to_s) unless params[:name]
  @form   = params[:parent]
  @form ||= params[:form]
  @label  = params[:label]
  # Get value from params unless set by derived constructor: 
  @value  = params[:value] unless @value 
  # Do not delete parameter value, as it is a 
  # standard for <input> elements. 
  # Field types not supporting the value attribute
  # (Textarea_Field, Option_Field, ...)
  # must delete it themselves. 
  @readonly = false
  params.delete(:form)
  params.delete(:parent)
  params.delete(:label)
  params[:parent] = @form
  super(params)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Aurita::GUI::Element

Instance Attribute Details

#formObject

Returns the value of attribute form.



31
32
33
# File 'lib/aurita-gui/form/form_field.rb', line 31

def form
  @form
end

#labelObject

Returns the value of attribute label.



31
32
33
# File 'lib/aurita-gui/form/form_field.rb', line 31

def label
  @label
end

#typeObject

Returns the value of attribute type.



31
32
33
# File 'lib/aurita-gui/form/form_field.rb', line 31

def type
  @type
end

#valueObject

Returns the value of attribute value.



31
32
33
# File 'lib/aurita-gui/form/form_field.rb', line 31

def value
  @value
end

Instance Method Details

#disable!Object



82
83
84
# File 'lib/aurita-gui/form/form_field.rb', line 82

def disable! 
  @attrib[:disabled] = true
end

#editable!Object



72
73
74
# File 'lib/aurita-gui/form/form_field.rb', line 72

def editable!
  @reaonly = false
end

#elementObject

Raises:



59
60
61
# File 'lib/aurita-gui/form/form_field.rb', line 59

def element
  raise Form_Error.new('Cannot render abstract class Form_Field')
end

#enable!Object



85
86
87
# File 'lib/aurita-gui/form/form_field.rb', line 85

def enable! 
  @attrib.delete(:disabled)
end

#readonly!Object



75
76
77
78
# File 'lib/aurita-gui/form/form_field.rb', line 75

def readonly!
  @attrib[:class] = @attrib[:class].to_s << ' readonly'
  @readonly = true
end

#readonly?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/aurita-gui/form/form_field.rb', line 79

def readonly? 
  @readonly
end

#readonly_elementObject



62
63
64
# File 'lib/aurita-gui/form/form_field.rb', line 62

def readonly_element
  HTML.div(@attrib) { @value }
end

#to_sObject Also known as: string



66
67
68
69
# File 'lib/aurita-gui/form/form_field.rb', line 66

def to_s
  return element().string unless @readonly
  return readonly_element().string
end