Sha256: 03dabfb4e098d31ae99cd2f5d4bb710b196591c05398a627ab21a0f9637b31ea

Contents?: true

Size: 1.42 KB

Versions: 15

Compression:

Stored size: 1.42 KB

Contents

require 'simple_form'
class MultiValueInput < SimpleForm::Inputs::CollectionInput
  def input
    @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} multi-text-field"]
    options[:'aria-labelledby'] = label_id
    @rendered_first_element = true
    @builder.text_field(attribute_name, options)
  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

15 entries across 15 versions & 1 rubygems

Version Path
curate-0.6.6 app/inputs/multi_value_input.rb
curate-0.6.5 app/inputs/multi_value_input.rb
curate-0.6.4 app/inputs/multi_value_input.rb
curate-0.6.3 app/inputs/multi_value_input.rb
curate-0.6.1 app/inputs/multi_value_input.rb
curate-0.6.0 app/inputs/multi_value_input.rb
curate-0.5.6 app/inputs/multi_value_input.rb
curate-0.5.5 app/inputs/multi_value_input.rb
curate-0.5.4 app/inputs/multi_value_input.rb
curate-0.5.2 app/inputs/multi_value_input.rb
curate-0.5.1 app/inputs/multi_value_input.rb
curate-0.5.0 app/inputs/multi_value_input.rb
curate-0.4.2 app/inputs/multi_value_input.rb
curate-0.3.2 app/inputs/multi_value_input.rb
curate-0.3.1 app/inputs/multi_value_input.rb