Sha256: e09087f4281e603d31a1d545714348f4091b1368898def89a572486139392d74

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

class MultiValueInput < SimpleForm::Inputs::CollectionInput
  def input(wrapper_options)
    @rendered_first_element = false
    input_html_classes.unshift("string")
    #input_html_options[:type] ||= 'text'
    input_html_options[:name] ||= "#{object_name}[#{attribute_name}][]"
    markup = <<-HTML


        <ul class="listing">
    HTML

    collection.each_with_index do |value, i|
      unless value.to_s.strip.blank?
        markup << <<-HTML
          <li class="field-wrapper">
            #{build_text_field(value)}
          </li>
        HTML
      end
    end

    markup << <<-HTML
          <li class="field-wrapper">
            #{build_text_field('')}
          </li>
        </ul>

    HTML
  end

  private


  def build_text_field(value)
    options = input_html_options.dup

    options[:value] = value
    if @rendered_first_element
      options[:id] = nil
      options[:required] = nil
    else
      options[:id] ||= input_dom_id
    end
    options[:class] ||= []
    options[:class] += ["#{input_dom_id} form-control multi-text-field"]
    options[:'aria-labelledby'] = label_id
    @rendered_first_element = true
    if options.delete(:type) == 'textarea'.freeze
      @builder.text_area(attribute_name, options)
    else
      @builder.text_field(attribute_name, options)
    end
  end


  def label_id
    input_dom_id + '_label'
  end

  def input_dom_id
    input_html_options[:id] || "#{object_name}_#{attribute_name}"
  end

  def collection
    @collection ||= begin
      object.send(attribute_name)
    end
  end

  def multiple?; true; end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
worthwhile-0.1.2 app/inputs/multi_value_input.rb
worthwhile-0.1.1 app/inputs/multi_value_input.rb
worthwhile-0.1.0 app/inputs/multi_value_input.rb
worthwhile-0.0.3 app/inputs/multi_value_input.rb
worthwhile-0.0.2 app/inputs/multi_value_input.rb
worthwhile-0.0.1 app/inputs/multi_value_input.rb