Class: SimpleForm::Bootstrap::Inputs::DateTimeInput

Inherits:
Inputs::Base
  • Object
show all
Defined in:
lib/simple_form/bootstrap/inputs/date_time_input.rb

Overview

Inspired by this StackOverflow answer: stackoverflow.com/a/19018501 I have modified this to work with dates AND times, and also to work better with browsers with JavaScript disabled.

Instance Method Summary collapse

Instance Method Details

#input(_ = nil) ⇒ Object



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
# File 'lib/simple_form/bootstrap/inputs/date_time_input.rb', line 5

def input(_ = nil)
  text_field_options             = input_html_options.with_indifferent_access
  format                         = text_field_options.delete(:format) || default_format

  # Integrate with Bootstrap's styling
  text_field_options[:class]    << 'form-control'

  hidden_field_options           = text_field_options.deep_dup
  hidden_field_options[:class]  << 'bootstrap-datepicker'
  hidden_field_options[:class]  << 'bootstrap-timepicker' if input_type == :bootstrap_date_time
  hidden_field_options[:id]      = "#{attribute_name}_hidden"
  hidden_field_options[:value] ||= format_date(value(object), format)
  hidden_field_options[:data]    = { 'date-format' => strftime_to_momentjs_format(format) }

  hidden_field_group_classes     = hidden_field_options[:class].dup
  hidden_field_group_classes.delete('form-control')

  
  text_field_options[:value]   ||= value(object).try(:to_s)

  return_string = <<-END_INPUT
    <div class="input-group #{hidden_field_group_classes.join(' ')}" style="display: none">
      #{@builder.hidden_field(attribute_name, hidden_field_options.to_hash)}
      <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
    </div>
    #{@builder.text_field(attribute_name, text_field_options.to_hash)}
  END_INPUT
  return_string.html_safe
end