module Padrino::Helpers::FormHelpers::Options

Helpers to generate options list for select tag.

Public Instance Methods

extract_option_tags!(options) click to toggle source
# File lib/padrino-helpers/form_helpers/options.rb, line 8
def extract_option_tags!(options)
  state = extract_option_state!(options)
  option_tags = if options[:grouped_options]
    grouped_options_for_select(options.delete(:grouped_options), state)
  else
    options_for_select(extract_option_items!(options), state)
  end
  if prompt = options.delete(:include_blank)
    option_tags.unshift(blank_option(prompt))
  end
  option_tags
end

Private Instance Methods

blank_option(prompt) click to toggle source

Returns the blank option serving as a prompt if passed.

# File lib/padrino-helpers/form_helpers/options.rb, line 26
def blank_option(prompt)
  case prompt
  when nil, false
    nil
  when String
    content_tag(:option, prompt,       :value => '')
  when Array
    content_tag(:option, prompt.first, :value => prompt.last)
  else
    content_tag(:option, '',           :value => '')
  end
end
extract_option_items!(options) click to toggle source
# File lib/padrino-helpers/form_helpers/options.rb, line 86
def extract_option_items!(options)
  if options[:collection]
    collection, fields = options.delete(:collection), options.delete(:fields)
    collection.map{ |item| [ item.send(fields.first), item.send(fields.last) ] }
  else
    options.delete(:options) || []
  end        
end
extract_option_state!(options) click to toggle source
# File lib/padrino-helpers/form_helpers/options.rb, line 79
def extract_option_state!(options)
  {
    :selected => Array(options.delete(:value))|Array(options.delete(:selected))|Array(options.delete(:selected_options)),
    :disabled => Array(options.delete(:disabled_options))
  }
end
grouped_options_for_select(collection, state = {}) click to toggle source

Returns the optgroups with options tags for a select based on the given :grouped_options items.

# File lib/padrino-helpers/form_helpers/options.rb, line 68
def grouped_options_for_select(collection, state = {})
  collection.map do |item|
    caption = item.shift
    attributes = item.last.kind_of?(Hash) ? item.pop : {}
    value = item.flatten(1)
    attributes = value.pop if value.last.kind_of?(Hash)
    html_attributes = { :label => caption }.merge(attributes||{})
    content_tag(:optgroup, options_for_select(value, state), html_attributes)
  end
end
option_is_selected?(value, caption, selected_values) click to toggle source

Returns whether the option should be selected or not.

@example

option_is_selected?("red", "Red", ["red", "blue"])   => true
option_is_selected?("red", "Red", ["green", "blue"]) => false
# File lib/padrino-helpers/form_helpers/options.rb, line 46
def option_is_selected?(value, caption, selected_values)
  Array(selected_values).any? do |selected|
    [value.to_s, caption.to_s].include?(selected.to_s)
  end
end
options_for_select(option_items, state = {}) click to toggle source

Returns the options tags for a select based on the given option items.

# File lib/padrino-helpers/form_helpers/options.rb, line 55
def options_for_select(option_items, state = {})
  return [] if option_items.blank?
  option_items.map do |caption, value, attributes|
    html_attributes = { :value => value ||= caption }.merge(attributes||{})
    html_attributes[:selected] ||= option_is_selected?(value, caption, state[:selected])
    html_attributes[:disabled] ||= option_is_selected?(value, caption, state[:disabled])
    content_tag(:option, caption, html_attributes)
  end
end