class Formular::Element::Select

Public Instance Methods

option_tags() click to toggle source

convert the collection array into option tags also supports option groups when the array is nested example 1:

[1,“True”], [0,“False”]

>

<option value=“1”>true</option><option value=“0”>false</option> example 2: [

["Genders", [["m", "Male"], ["f", "Female"]]],
["Booleans", [[1,"true"], [0,"false"]]]

] => <optgroup label=“Genders”>

<option value="m">Male</option>
<option value="f">Female</option>

</optgroup> <optgroup label=“Booleans”>

<option value="1">true</option>
<option value="0">false</option>

</optgroup>

# File lib/formular/elements.rb, line 219
def option_tags
  collection_to_options(options[:collection])
end

Private Instance Methods

collection_to_options(collection) click to toggle source
# File lib/formular/elements.rb, line 251
def collection_to_options(collection)
  collection.map do |item|
    if item.last.is_a?(Array)
      opts = { label: item.first, content: collection_to_options(item.last) }

      Formular::Element::OptGroup.new(opts).to_s
    else
      item_to_option(item)
    end
  end.join ''
end
inject_placeholder(collection) click to toggle source

same handling as simple form prompt: a nil value option appears if we have no selected option include blank: includes our nil value option regardless (useful for optional fields)

# File lib/formular/elements.rb, line 234
def inject_placeholder(collection)
  placeholder = if options[:include_blank]
                  placeholder_option(options[:include_blank])
                elsif options[:prompt] && options[:value].nil?
                  placeholder_option(options[:prompt])
                end

  collection.unshift(placeholder) if placeholder

  collection
end
item_is_selected(option_val, current_val, multiple) click to toggle source
# File lib/formular/elements.rb, line 278
def item_is_selected(option_val, current_val, multiple)
  return option_val == current_val.to_s unless multiple && current_val.is_a?(Array)

  current_val.map(&:to_s).include?(option_val) # TODO Perf improvement here - do we need the map?
end
item_to_option(item) click to toggle source
# File lib/formular/elements.rb, line 263
def item_to_option(item)
  opts = if item.is_a?(Array) && item.size > 2
           item.pop
         else
           {}
         end

  opts[:value] = html_escape(item.send(options[:value_method]))
  opts[:content] = item.send(options[:label_method])

  opts[:selected] = 'selected' if item_is_selected(opts[:value], options[:value], options[:multiple])

  Formular::Element::Option.new(opts).to_s
end
name_array_if_multiple(name) click to toggle source

only append the [] to name if the multiple option is set

# File lib/formular/elements.rb, line 225
def name_array_if_multiple(name)
  return unless name

  options[:multiple] ? "#{name}[]" : name
end
placeholder_option(value) click to toggle source
# File lib/formular/elements.rb, line 246
def placeholder_option(value)
  text = value.is_a?(String) ? html_escape(value) : ""
  [text, ""]
end