Class: Aurita::GUI::Form_Field_Wrapper

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

Overview

Default decorator for form fields. Decorates single entry of form to <li> element, setting CSS classes and DOM ids. To use your own field decorator, derive it from Aurita::GUI::Element (maybe indirectly via one of its derivates) and define its constructor to expect a single Form_Field instance. The field decorator has to wrap the actual form field.

See the source code of Aurita::GUI::Form_Field_Wrapper for a simple implementation.

Tell a form to use a specific field decorator by

the_form.field_decorator = My_Field_Decorator

To use your own implementation as defaut, overload Form.initialize like

class My_Form < Aurita::GUI::Form
  def initialize(params={}, &block)
    super(params, &block)
    @field_decorator = My_Field_Decorator
  emd
end

Or write a factory (*hint hint*) like:

class Form_Factory
  def self.form(params={}, &block)
    form = Aurita::GUI::Form.new(params, &block)
    form.field_decorator = My_Field_Decorator
    return form
  end
end

See also: Form_Content_Wrapper (pretty much the same).

Instance Attribute Summary collapse

Attributes inherited from Element

#attrib, #force_closing_tag, #parent, #tag

Instance Method Summary collapse

Methods inherited from Element

#+, #<<, #[], #[]=, #add_class, #clear_floating, #css_classes, #find_by_dom_id, #get_content, #has_content?, #id, #id=, #method_missing, #recurse, #remove_class, #set_content, #string, #swap, #to_ary, #type=

Methods included from Marshal_Helper_Class_Methods

#marshal_load

Methods included from Marshal_Helper

#marshal_dump

Constructor Details

#initialize(field) ⇒ Form_Field_Wrapper

Returns a new instance of Form_Field_Wrapper.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/aurita-gui/form.rb', line 66

def initialize(field)
  label_params = false
  if field.label then
    label_params = { :for => field.dom_id, :force_closing_tag => true }
    label_params[:id] = field.dom_id.to_s + '_label' if field.dom_id
    label = field.label
    @content = [ HTML.label(label_params) { label }, field ]
  else 
    @content = field
  end
  field.dom_id = field.name.to_s.gsub('.','_') unless field.dom_id
  css_classes = field.class.to_s.split('::')[-1].downcase + '_wrap form_field' 
  css_classes << ' required' if field.required?
  css_classes << ' invalid' if field.invalid?
  params = { :tag => :li, 
             :content => @content, 
             :id => field.dom_id.to_s + '_wrap', 
             :class => css_classes }
  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

#fieldObject

Returns the value of attribute field.



64
65
66
# File 'lib/aurita-gui/form.rb', line 64

def field
  @field
end