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
# File 'app/inputs/date_time_input.rb', line 2

def input(wrapper_options)
  date_format = options[:date_format] || '%m/%d/%Y'
  raw_value = object.public_send(attribute_name)
  raw_value = raw_value.strftime(date_format) if raw_value.present?

  disabled = options[:disabled] || false

  field = @builder.text_field(attribute_name, id: "#{attribute_name}_datetimepicker", class: 'form-control', value: raw_value, disabled: disabled, 'data-toggle': 'datetimepicker', 'data-target': "##{attribute_name}_datetimepicker")

  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
      template.(:i, '', :class => add_on_class, 'data-toggle': 'datetimepicker', 'data-target': "##{attribute_name}_datetimepicker")
    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