Sha256: a9cbc38dfd99ba9e8c370e6dc937562cac9ecd42ee4d9d308108b1cbebe8a07c

Contents?: true

Size: 951 Bytes

Versions: 1

Compression:

Stored size: 951 Bytes

Contents

class Metallize::Form::MultiSelectList < Metallize::Form::Field

  extend Metallize::ElementMatcher

  attr_accessor :options

  def initialize node
    value    = []
    @options = []

    # parse
    node.find_elements(:tag_name, 'option').each do |n|
      @options << Metallize::Form::Option.new(n, self)
    end

    super node, value

  end

  elements_with :option

  # Select no options
  def select_none
    @value = []
    options.each { |o| o.untick }
  end

  def value=(values)
    select_none
    [values].flatten.each do |value|
      option = options.find { |o| o.value == value }
      if option.nil?
        @value.push(value)
      else
        option.node.click
      end
    end
  end

  # Get a list of all selected options
  def selected_options
    @options.find_all { |o| o.selected? }
  end

  def value
    value = []
    value.concat @value
    value.concat selected_options.map { |o| o.value }
    value.first
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
metallize-0.0.1 lib/metallize/form/multi_select_list.rb