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, #search_type, #serialized, #sort_as_numeric

Instance Method Summary collapse

Methods inherited from ResponseField

#audit_response, #before_response_destroyed, #has_length_validations?, #length_validations, #max, #maxlength, #min, #min_max_length_units, #min_max_validations, #minlength, #options_array, #validate_response

Instance Method Details

#normalize_response(value, all_responses) ⇒ Object



78
79
80
81
82
# File 'app/models/formbuilder/response_field_checkboxes.rb', line 78

def normalize_response(value, all_responses)
  options_array.each do |option_label|
    all_responses["#{self.id}_sortable_values_#{option_label}"] = value[option_label]
  end
end

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



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
66
# File 'app/models/formbuilder/response_field_checkboxes.rb', line 41

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_entry_text(value, opts = {}) ⇒ Object



68
69
70
71
72
# File 'app/models/formbuilder/response_field_checkboxes.rb', line 68

def render_entry_text(value, opts = {})
  (value || {}).map do |k, v|
    "#{k}: #{v ? (k == 'Other' ? v : 'y') : 'n'}"
  end.join("\n")
end

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



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
39
# File 'app/models/formbuilder/response_field_checkboxes.rb', line 11

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 class='fb-option'>
        <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='fb-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

#sortable_value(value) ⇒ Object



74
75
76
# File 'app/models/formbuilder/response_field_checkboxes.rb', line 74

def sortable_value(value)
  nil # see :normalize_response for override
end

#transform_raw_value(raw_value, entry, opts = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/formbuilder/response_field_checkboxes.rb', line 84

def transform_raw_value(raw_value, entry, opts = {})
  raw_value ||= {}

  {}.tap do |h|
    options_array.each_with_index do |label, index|
      h[label] = raw_value[index.to_s] == "on"
    end

    if raw_value['other_checkbox'] == 'on'
      entry.get_responses["#{self.id}_other"] = true
      h['Other'] = raw_value['other']
    end

    if h.find { |_, v| v }.present?
      entry.get_responses["#{self.id}_present"] = true
    else
      entry.get_responses.delete("#{self.id}_present")
    end
  end
end