Class: DatepickerInput

Inherits:
SimpleForm::Inputs::StringInput
  • Object
show all
Defined in:
lib/simple-form-datepicker.rb

Overview

Based on: stackoverflow.com/questions/5007785/how-do-i-write-a-cleaner-date-picker-input-for-simpleform

Works well with JQueryUI’s datepicker and I18n.

Instance Method Summary collapse

Instance Method Details

#inputObject

Builds the input text field and an hidden field with class attribute_name-alt



32
33
34
35
# File 'lib/simple-form-datepicker.rb', line 32

def input
  @builder.text_field(attribute_name, input_html_options) + \
  @builder.hidden_field(attribute_name, value: input_html_options[:value], class: attribute_name.to_s + "-alt")
end

#input_html_classesObject

Adds the “datepicker” class to the input element



27
28
29
# File 'lib/simple-form-datepicker.rb', line 27

def input_html_classes
  super.push('datepicker')
end

#input_html_optionsObject

Builds options for the dateicker input, sets it’s value using Rails’s date format and adds behaviour: ‘datepicker’ to its date-atrributes.



16
17
18
19
20
21
22
23
24
# File 'lib/simple-form-datepicker.rb', line 16

def input_html_options
  value = object.send(attribute_name)
  options = {
    value: value.nil? ? nil : value.strftime("%Y-%m-%d"),
    data: { behaviour: 'datepicker' }  # for example
  }
  # add all html option you need...
  super.merge options
end