Class FormTestHelper::Select
In: lib/form_test_helper.rb
Parent: Field

Methods

Public Class methods

[Source]

     # File lib/form_test_helper.rb, line 351
351:     def initialize(tags)
352:       @options = tags.first.select("option").collect {|option_tag| Option.new(self, option_tag) }
353:       super
354:     end

Public Instance methods

[Source]

     # File lib/form_test_helper.rb, line 356
356:     def initial_value
357:       selected_options = @options.select(&:initially_selected)
358:       case selected_options.size
359:       when 1
360:         selected_options.first.value
361:       when 0 # If no option is selected, browsers generally use the first
362:         @options.first.value
363:       else
364:         # When multiple options selected but the multiple attribute is not specified, 
365:         # Firefox selects the last of the options.
366:         selected_options.last.value
367:       end
368:     end

If value is a label, return the real value. If not an option, raise error.

[Source]

     # File lib/form_test_helper.rb, line 387
387:     def lookup_in_options(value)
388:       if options.include?(value)
389:         return value
390:       elsif options_are_labeled? && pair = options.detect {|option| option.include?(value.to_s) }
391:         return pair.last
392:       else
393:         raise "Value '#{value}' isn't one of the options for #{self.name}."
394:       end
395:     end

[Source]

     # File lib/form_test_helper.rb, line 370
370:     def options
371:       if options_are_labeled?
372:         @options.collect do |option|
373:           [option.label, option.value]
374:         end
375:       else
376:         @options.collect(&:value)
377:       end
378:     end

True if options are like <option value="4">Spain</option> rather than <option>Spain</option> or <option value="Spain">Spain</option>

[Source]

     # File lib/form_test_helper.rb, line 382
382:     def options_are_labeled?
383:       @options.any? {|option| option.label }
384:     end

[Source]

     # File lib/form_test_helper.rb, line 397
397:     def value=(value)
398:       @value = lookup_in_options(value)
399:     end

[Validate]