Class: Kiss::Form::MultiChoiceField

Inherits:
Field show all
Defined in:
lib/kiss/form/field.rb

Overview

Subclass for single-value field types that present and take their value from a set of options.

Direct Known Subclasses

MultiValueField, RadioField, SelectField, SubmitField

Constant Summary

Constants inherited from Field

Field::CURRENCY_SYMBOLS

Instance Method Summary collapse

Methods inherited from Field

#add_error, #content_tag_html, #debug, #element_html, #errors_html, #html, #input_tag_html, #method_missing, #param, #require_value, #reset, #set_value_to_hash, #set_value_to_object, #tag_html, #tag_start_html, #tip_html, #type, #value, #value_string, #value_to_s

Constructor Details

#initialize(*args, &block) ⇒ MultiChoiceField

Returns a new instance of MultiChoiceField.



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/kiss/form/field.rb', line 310

def initialize(*args, &block)
  @_options_display_transform = :to_s
  
  super(*args, &block)
  
  @_display_format = Kiss::Format.lookup(@_display_format)
  
  if @_options
    if @_options[0].is_a?(Array)
      @_options_value_key ||= 0
      (@_options_display_key ||= (@_options[0].size == 1) ? 0 : 1)
    elsif defined?(Kiss::Model) && @_options[0].is_a?(Kiss::Model)
      model_klass = @_options[0].class
      @_options_value_key ||= model_klass.value_column
      @_options_display_key ||= model_klass.display_column
    elsif @_options[0].is_a?(Hash)
      @_options_value_key ||= :id
      @_options_display_key ||= :name
    end
  end
  
  if @_other
    @_other_field = @_form.create_field( { :name => @_name + '.other' }.merge(@_other) )
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Kiss::Form::Field

Instance Method Details

#column_layout(elements_html) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/kiss/form/field.rb', line 376

def column_layout(elements_html)
  if elements_html.empty?
    ''
  elsif @_columns
    layout_columns = [@_columns, elements_html.size].min
    num_elements_per_column = ((elements_html.size + layout_columns - 1) / layout_columns).to_i
    layout_columns = ((elements_html.size + num_elements_per_column - 1) / num_elements_per_column).to_i
  
    style = "style=\"width: #{(100 / layout_columns).to_i - 1}%\""
    
    '<table class="kiss_field_columns"><tr>' +
      (0...layout_columns).map do |i|
        "<td #{style}>" + elements_html[i * num_elements_per_column, num_elements_per_column].join('<br/>') + "</td>"
      end.join + '</table>'
  else
    elements_html.map {|h| "<nobr>#{h}</nobr>"}.join('&nbsp; ')
  end
end

#display_to_s(value) ⇒ Object



341
342
343
# File 'lib/kiss/form/field.rb', line 341

def display_to_s(value)
  value ? @_display_format.value_to_s(value).send(@_options_display_transform) : ''
end

#form=(new_form) ⇒ Object



336
337
338
339
# File 'lib/kiss/form/field.rb', line 336

def form=(new_form)
  super(new_form)
  @_other_field.form = new_form if @_other_field
end

#has_option_value?(v) ⇒ Boolean

Returns:

  • (Boolean)


369
370
371
372
373
374
# File 'lib/kiss/form/field.rb', line 369

def has_option_value?(v)
  !(@_options_value_key ?
    @_options.select {|o| value_to_s(o[@_options_value_key]) == v } :
    @_options.select {|o| value_to_s(o) == v }
  ).empty?
end

#option_pairsObject



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/kiss/form/field.rb', line 351

def option_pairs
  pairs = if @_options_value_key
    if @_options_display_key.is_a?(Proc)
      @_options.map {|option| [ option[@_options_value_key], @_options_display_key.call(option) ]}
    else
      @_options.map {|option| [
        option[@_options_value_key] || option.send(@_options_value_key), 
        option[@_options_display_key] || option.send(@_options_display_key)
      ]}
    end
  else
    @_display_format = @_format
    @_options.map {|option| [ option, option ]}
  end
  
  pairs
end

#options_keys(value, display) ⇒ Object Also known as: option_keys



345
346
347
348
# File 'lib/kiss/form/field.rb', line 345

def options_keys(value, display)
  @_options_value_key = value
  @_options_display_key = display
end

#other_field_htmlObject



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/kiss/form/field.rb', line 406

def other_field_html
  return '' unless @_other
  
  other_checked = @_value && !option_pairs.any? {|v, d| v == @_value }
  
  (@_columns ? '<br/>' : '&nbsp; ') + [
    input_tag_html(
      { :value => 'other', :html => { :id => @_name+'.other' } },
      other_checked ? 'checked' : ''
    ),
    @_other[:label] || 'Other',
    ': ',
    @_currency.to_s,
    input_tag_html({
      :type => :text,
      :name => @_name+'.other',
      :value => other_checked ? value_to_s(@_value) : nil,
      :html => {
        :onfocus => "document.getElementById('#{@_name}.other').checked = true"
      }
    }.merge(@_other))
  ].join
end

#validateObject



395
396
397
398
399
400
401
402
403
404
# File 'lib/kiss/form/field.rb', line 395

def validate
  if @_other && param == 'other'
    @_param = @_form.params[@_name+'.other']
  end
  super('select')
  
  if @_value =~ /\S/ && !has_option_value?(@_value)
    add_error "Invalid selection" 
  end
end