Class: Formbuilder::ResponseFieldPrice

Inherits:
ResponseField
  • Object
show all
Defined in:
app/models/formbuilder/response_field_price.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, #min_max_validations, #normalize_response, #options_array, #render_entry_text, #transform_raw_value

Instance Method Details

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



31
32
33
# File 'app/models/formbuilder/response_field_price.rb', line 31

def render_entry(value, opts = {})
  "$#{sprintf('%.2f', "#{value['dollars']}.#{value['cents']}".to_f)}"
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
# File 'app/models/formbuilder/response_field_price.rb', line 11

def render_input(value, opts = {})
  """
    <div class='input-line'>
      <span class='above-line'>$</span>

      <span class='dollars'>
        <input type='text' name='response_fields[#{self[:id]}][dollars]' id='response_fields_#{self[:id]}' value='#{value['dollars']}' />
        <label>Dollars</label>
      </span>

      <span class='above-line'>.</span>

      <span class='cents'>
        <input type='text' name='response_fields[#{self[:id]}][cents]' value='#{value['cents']}' maxlength='2' />
        <label>Cents</label>
      </span>
    </div>
  """
end

#sortable_value(value) ⇒ Object



46
47
48
# File 'app/models/formbuilder/response_field_price.rb', line 46

def sortable_value(value)
  "#{value['dollars'] || '0'}.#{value['cents'] || '0'}".to_f
end

#validate_response(value) ⇒ Object

format: [dollars] [cents] only one is required, and it must consist only of numbers



37
38
39
40
41
42
43
44
# File 'app/models/formbuilder/response_field_price.rb', line 37

def validate_response(value)
  if value.select { |k, v| k.in?(['dollars', 'cents']) && v.present? }
          .find { |k, v| (Float(v) rescue nil).nil? }
          .present?

    "isn't a valid price."
  end
end