Class: Aurita::GUI::Options_Field

Inherits:
Form_Field show all
Defined in:
lib/aurita-gui/form/options_field.rb

Overview

Abstract base class for all form elements containing options, like Select_Field, Radio_Field, Checkbox_Field or any custom implementation.

Usage:

r = Radio_Field.new(:options => { 1 => 'first', 
                                  2 => 'second', 
                                  3 => 'third' },
                    :label => 'Which one?', 
                    :value => 1)

Same as

r = Radio_Field(:option_range  => (1..3)
                :option_labels => ['First', 'Second', 'Third']
                :value => 1,
                :label => 'Which one?')

Set a selected value using parameter :value

r = Radio_Field.new(:value => 42, :name => :amount, 
                    :label => 'Select amount')
r.value = 23

If there may be more than one selected field, e.g. for Checkbox_Field, There are many ways to define options:

select = Select_Field.new(:name => :category, :label => 'Category') 
select.options = { 1 => 'first', 2 => 'second' } 
select.add_option(3 => 'third')
select[3] = HTML.option(:value => 4) { 'fourth' }
select[4] = { 5 => 'fifth' }

Setting option values and labels

There are a zillion ways to set option values and labels. The following examples all use Select_Field, but this behaviour applies to all derivates of Options_Field.

If there are no option labels set, option values will be displayed directly:

s1 = Select_Field.new(:name => :test, 
                      :label => 'Priority', 
                      :options => (1..10)) # Option labels are 0..10

Set option values and labels at once using a hash:

s1 = Select_Field.new(:name => :test, 
                      :label => 'Pick one', 
                      :options => { 1 => 'eins', 2 => 'zwei', 3 => 'drei' })

Set option values as array, labels as hash:

s1 = Select_Field.new(:name => :test, 
                      :label => 'Pick one', 
                      :option_labels => [ 'foo', 'bar', 'wombat' ] }, 
                      :option_values => [ 1,2,3 ])

Ranges are ok, too:

s1 = Select_Field.new(:name => :test, 
                      :label => 'Pick one', 
                      :option_labels => [ 'foo', 'bar', 'wombat' ], 
                      :options => (1..3))

Change option labels using an array. Option labels will be assigned in order, so options has label etc.

s1.option_labels = [ 'first', 'second', 'third' ]

Change option labels using a hash. Compared to using an array, this is useful in case you don’t know the order of option values. You can also rename some or all option labels this way.

s1.option_labels = { 1 => 'ras', 2 => 'dwa', 3 => 'tri' }

Of yourse you can replace all option values and their labels at once by overwriting the options field:

s1.label = 'Choose'
s1.options = { 1 => :foo, 2 => :bar, 3 => :wombat }

Instance Attribute Summary collapse

Attributes inherited from Form_Field

#data_type, #form, #hidden, #hint, #invalid, #label, #required, #type

Attributes inherited from Element

#attrib, #force_closing_tag, #gui_element_id, #parent, #tag

Instance Method Summary collapse

Methods inherited from Form_Field

#disable!, #disabled=, #editable!, #enable!, #hidden?, #hide!, #invalid!, #invalid?, #optional!, #readonly!, #readonly=, #readonly?, #required!, #required?, #show!, #to_hidden_field, #to_s

Methods inherited from Element

#+, #<<, #add_class, #aurita_gui_element, #clear_floating, #css_classes, #find_by_dom_id, #get_content, #has_content?, #id, #id=, #inspect, #js_init_code, #length, #method_missing, #recurse, #remove_class, #set_content, #string, #swap, #to_ary, #touch, #touched?, #type=, #untouch

Methods included from Marshal_Helper_Class_Methods

#marshal_load

Methods included from Marshal_Helper

#marshal_dump

Constructor Details

#initialize(params, &block) ⇒ Options_Field

Returns a new instance of Options_Field.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/aurita-gui/form/options_field.rb', line 109

def initialize(params, &block)
  @option_labels     = params[:option_labels]
  @option_labels   ||= []
  @option_values     = params[:option_values]
  @option_values   ||= []
  @value             = params[:value]

  set_options(params[:options]) if params[:options]

  if block_given? then
    yield.each { |option|
      add_option(option)
    }
  elsif params[:options] and !params[:option_labels] then
    add_option(params[:options])
  end
  params.delete(:options)
  # Option fields don't have a value attribute themselves
  params.delete(:value) 
  params.delete(:option_values)
  params.delete(:option_labels)
  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

#option_labelsObject

Returns the value of attribute option_labels.



106
107
108
# File 'lib/aurita-gui/form/options_field.rb', line 106

def option_labels
  @option_labels
end

#option_valuesObject

Returns the value of attribute option_values.



106
107
108
# File 'lib/aurita-gui/form/options_field.rb', line 106

def option_values
  @option_values
end

#valueObject

Returns the value of attribute value.



107
108
109
# File 'lib/aurita-gui/form/options_field.rb', line 107

def value
  @value
end

Instance Method Details

#[](index) ⇒ Object



200
201
202
# File 'lib/aurita-gui/form/options_field.rb', line 200

def [](index)
  @option_elements[index]
end

#[]=(index, option_element) ⇒ Object



203
204
205
# File 'lib/aurita-gui/form/options_field.rb', line 203

def []=(index, option_element)
  @option_elements[index] = option_element
end

#add_option(option = {}) ⇒ Object



164
165
166
167
168
169
170
171
172
# File 'lib/aurita-gui/form/options_field.rb', line 164

def add_option(option={})
  if option.kind_of? Array then
    @option_values += option
  elsif option.kind_of? Range then
    @option_values += option.to_a
  elsif option.kind_of? Hash then
  #  @option_elements << options
  end
end

#contentObject



217
218
219
# File 'lib/aurita-gui/form/options_field.rb', line 217

def content
  option_elements()
end

#elementObject

Raises:



207
208
209
# File 'lib/aurita-gui/form/options_field.rb', line 207

def element
  raise Form_Error.new('Method #element from Abstract class Options_Field has not been overloaded in ' << self.class.to_s)
end

#option_hashObject



139
140
141
# File 'lib/aurita-gui/form/options_field.rb', line 139

def option_hash
  options().to_hash
end

#optionsObject



133
134
135
136
137
# File 'lib/aurita-gui/form/options_field.rb', line 133

def options
  @option_labels = @option_values.dup unless @option_labels.length > 0
  @option_labels.fields = @option_values.map { |v| v.to_s } 
  @option_labels
end

#options=(options) ⇒ Object Also known as: set_options



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/aurita-gui/form/options_field.rb', line 174

def options=(options)
  if options.kind_of? ArrayFields then
    @option_values = options.fields
    @option_labels = options.values
  elsif options.kind_of? Array then
    @option_values = options
    @option_labels = options
  elsif options.kind_of? Range then
    @option_values = options.to_a
    @option_labels = options.to_a
  elsif options.kind_of? Hash then
    @option_values = []
    @option_labels = []
    options.sort.each { |pair|
      @option_values << pair[0]
      @option_labels << pair[1]
    }
  end
end

#readonly_elementObject



211
212
213
214
215
# File 'lib/aurita-gui/form/options_field.rb', line 211

def readonly_element
  opt = options()
  return HTML.div(@attrib) { opt[@value] } if (opt && @value.to_s != '' && opt[@value])
  return HTML.div(@attrib) { @value } 
end