Class: MultiValueInput

Inherits:
SimpleForm::Inputs::CollectionInput
  • Object
show all
Defined in:
app/inputs/multi_value_input.rb

Instance Method Summary collapse

Instance Method Details

#inputObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/inputs/multi_value_input.rb', line 3

def input
  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 do |value|
    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('', 'add')}
        </li>
      </ul>

  HTML
end

#labelObject

NOTE: There is a one to many relationship between the label and the input elements. Because of this we can’t use the “for” attribute on the label point to the “id” of the input. Instead we use the “aria-labelledby” attribute on the input to point to the “id” on the label.

It would be better to use @builder construct the element but the proper syntax escapes me.



39
40
41
42
43
44
# File 'app/inputs/multi_value_input.rb', line 39

def label
  attribute_label = template.get_label(attribute_name) rescue attribute_name.to_s.titleize
  <<-HTML
  <label id="#{label_id}" class="string #{label_classes}">#{attribute_label}</label>
  HTML
end