Class: Effective::FormBuilderInputs::EffectiveDatePicker

Inherits:
Effective::FormBuilderInput show all
Defined in:
app/models/effective/form_builder_inputs/effective_date_picker.rb

Instance Method Summary collapse

Methods inherited from Effective::FormBuilderInput

#field_name, #initialize, #options

Constructor Details

This class inherits a constructor from Effective::FormBuilderInput

Instance Method Details

#default_input_htmlObject



10
11
12
# File 'app/models/effective/form_builder_inputs/effective_date_picker.rb', line 10

def default_input_html
  {class: 'effective_date_picker date'}
end

#default_input_jsObject



6
7
8
# File 'app/models/effective/form_builder_inputs/effective_date_picker.rb', line 6

def default_input_js
  {format: 'YYYY-MM-DD', showTodayButton: true, showClear: true}
end

#html_optionsObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/effective/form_builder_inputs/effective_date_picker.rb', line 53

def html_options
  super.tap do |html_options|
    if js_options[:format] == default_input_js[:format] # Unless someone changed from the default
      html_options[:pattern] = '\d{4}(-\d{2})?(-\d{2})?' # Match default pattern defined above
    end

    if options[:date_linked] == false
      html_options[:class] << 'not-date-linked'
    end

  end
end

#js_optionsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/effective/form_builder_inputs/effective_date_picker.rb', line 32

def js_options
  opts = super
  return opts unless opts[:disabledDates]

  opts[:disabledDates] = Array(opts[:disabledDates]).map do |obj|
    if obj.respond_to?(:strftime)
      obj.strftime('%F')
    elsif obj.kind_of?(Range) && obj.first.respond_to?(:strftime)
      [obj.first].tap do |dates|
        dates << (dates.last + 1.day) until (dates.last + 1.day) > obj.last
      end
    elsif obj.kind_of?(String)
      obj
    else
      raise 'unexpected disabledDates data. Expected a DateTime, Range of DateTimes or String'
    end
  end.flatten.compact

  opts
end

#to_htmlObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/effective/form_builder_inputs/effective_date_picker.rb', line 14

def to_html
  if options[:input_group] == false
    return text_field_tag(field_name, value, tag_options)
  end

  (:div, class: 'input-group') do
    (:span, class: 'input-group-addon') do
      (:i, '', class: 'glyphicon glyphicon-calendar').html_safe
    end +
    text_field_tag(field_name, value, tag_options)
  end
end

#valueObject



27
28
29
30
# File 'app/models/effective/form_builder_inputs/effective_date_picker.rb', line 27

def value
  val = super
  val.kind_of?(Time) ? val.to_date : val
end