Class: Formbuilder::ResponseFieldDate

Inherits:
ResponseField
  • Object
show all
Defined in:
app/models/formbuilder/response_field_date.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



36
37
38
# File 'app/models/formbuilder/response_field_date.rb', line 36

def render_entry(value, opts = {})
  "#{value['month']}/#{value['day']}/#{value['year']}"
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
# File 'app/models/formbuilder/response_field_date.rb', line 11

def render_input(value, opts = {})
  """
    <div class='input-line'>
      <span class='month'>
        <input type='text' name='response_fields[#{self[:id]}][month]' value='#{value['month']}' id='response_fields_#{self[:id]}' maxlength='2' />
        <label>MM</label>
      </span>

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

      <span class='day'>
        <input type='text' name='response_fields[#{self[:id]}][day]' value='#{value['day']}' maxlength='2' />
        <label>DD</label>
      </span>

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

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

#sortable_value(value) ⇒ Object



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

def sortable_value(value)
  ['year', 'month', 'day'].each { |x| return 0 unless value[x].try(:present?) }
  DateTime.new(value['year'].to_i, value['month'].to_i, value['day'].to_i).to_i rescue 0
end

#validate_response(value) ⇒ Object



40
41
42
43
44
# File 'app/models/formbuilder/response_field_date.rb', line 40

def validate_response(value)
  if value['year'].to_i == 0 || !(DateTime.new(value['year'].to_i, value['month'].to_i, value['day'].to_i) rescue false)
    "isn't a valid date."
  end
end