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, #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

Instance Method Details

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



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

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

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]' 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

#validate_response(value) ⇒ Object

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



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

def validate_response(value)
  value.select { |k, v| k.in?(['dollars', 'cents']) && v.present? }.each do |k, v|
    unless (Float(v) rescue 0) > 0
      return "isn't a valid price."
    end
  end

  return nil
end