Class: DateTimeInput

Inherits:
SimpleForm::Inputs::Base
  • Object
show all
Defined in:
app/inputs/date_time_input.rb

Instance Method Summary collapse

Instance Method Details

#input(wrapper_options) ⇒ Object



2
3
4
5
6
7
8
9
10
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
41
# File 'app/inputs/date_time_input.rb', line 2

def input(wrapper_options)
  raw_value = parse_value(object.public_send(attribute_name), options[:date_format])

  disabled = options[:disabled] || false

  hash = { id: "#{attribute_name}_datetimepicker", class: 'form-control datetimepicker-input', value: raw_value, disabled: disabled, 'data-toggle': 'datetimepicker', 'data-target': "##{attribute_name}_datetimepicker" }
  data = options[:data] || {}
  data.keys.each do |d|
    hash["data-#{d.to_s.dasherize}".to_sym] = data[d]
  end
  field = @builder.text_field(attribute_name, hash)

  add_on_class = options[:add_on_class] || "fa fa-calendar"

  add_on = template.(:div, class: "input-group-prepend") do
    add_on = template.(:div, class: "input-group-text") do
      hash = { class: add_on_class, 'data-toggle': 'datetimepicker', 'data-target': "##{attribute_name}_datetimepicker" }
      template.(:i, '', hash)
    end
  end

  all = (:div, add_on + field, class: 'input-group')

  script = "".html_safe
  unless disabled then
    picker_options = options[:picker_options] || { "format": "MM/DD/YYYY" }

    script = """
      <script>
      $(document).ready(function() {
        $('##{attribute_name}_datetimepicker').datetimepicker(
          #{picker_options.to_json}
        );
      });
      </script>
    """.html_safe
  end

  all + script
end

#parse_value(raw_value, format) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'app/inputs/date_time_input.rb', line 43

def parse_value(raw_value, format)
  return nil unless raw_value.present?

  format ||=  case raw_value.class.name
              when 'Time', 'ActiveSupport::TimeWithZone'
                '%m/%d/%Y %H:%M %p'
              else
                '%m/%d/%Y'
              end
  raw_value.strftime(format)
end