class Formular::Element::Select
Public Instance Methods
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