Class: Formbuilder::ResponseFieldCheckboxes

Inherits:
ResponseField
  • Object
show all
Defined in:
app/models/formbuilder/response_field_checkboxes.rb

Constant Summary

Constants inherited from ResponseField

Formbuilder::ResponseField::ALLOWED_PARAMS

Instance Attribute Summary

Attributes inherited from ResponseField

#cid, #field_type, #input_field, #options_field, #serialized, #sort_as_numeric

Instance Method Summary collapse

Methods inherited from ResponseField

#audit_response, #has_length_validations?, #length_validations, #min_max_validations, #render_entry_for_table, #validate_response

Instance Method Details

#render_entry(value, opts = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/models/formbuilder/response_field_checkboxes.rb', line 40

def render_entry(value, opts = {})
  """
    <table class='response-table'>
      <thead>
        <tr>
          <th class='key'>Key</th>
          <th class='response'>Response</th>
        </tr>
      </thead>
      <tbody>
  """ +

  (value || {}).map do |k, v|
    """
      <tr class='#{v ? 'true' : 'false'}'>
        <td>#{k}</td>
        <td class='response'>#{v ? (k == 'Other' ? v : '<span class="icon-ok"></span>') : ''}</td>
      </tr>
    """
  end.join('') +

  """
      </tbody>
    </table>
  """
end

#render_input(value, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/models/formbuilder/response_field_checkboxes.rb', line 10

def render_input(value, opts = {})
  value ||= {}

  str = (self[:field_options]["options"] || []).each_with_index.map do |option, i|
    checked = value.present? ? value[option['label']] : (option['checked'] == 'true')

    """
      <label>
        <input type='checkbox' name='response_fields[#{self[:id]}][#{i}]' #{checked ? 'checked' : ''} value='on' />
        #{option['label']}
      </label>
    """
  end.join('')

  if self[:field_options]['include_other_option']
    str += """
      <div class='other-option'>
        <label>
          <input type='checkbox' name='response_fields[#{self[:id]}][other_checkbox]' #{value['Other'] ? 'checked' : ''} value='on' />
          Other
        </label>

        <input type='text' name='response_fields[#{self[:id]}][other]' value='#{value['Other']}' />
      </div>
    """
  end

  str
end