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



42
43
44
# File 'app/models/formbuilder/response_field_price.rb', line 42

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
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/formbuilder/response_field_price.rb', line 11

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

  str = """
    <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>
  """

  unless self.field_options['disable_cents']
    str += """
      <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>
    """
  end

  str += """
    </div>
  """

  str
end

#sortable_value(value) ⇒ Object



57
58
59
# File 'app/models/formbuilder/response_field_price.rb', line 57

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



48
49
50
51
52
53
54
55
# File 'app/models/formbuilder/response_field_price.rb', line 48

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