Class: DpickerInput
- Inherits:
-
SimpleForm::Inputs::StringInput
- Object
- SimpleForm::Inputs::StringInput
- DpickerInput
- Defined in:
- app/inputs/dpicker_input.rb
Overview
Custom simple_form input type for integration with bootstrap-datetimepicker
To use it, simply add :as => :dpicker to your input and it will automatically deal with all the I18n issues, making automatic conversion from Rails I18n formats to the one expected by bootstrap-datetimepicker.
With no options, it behaves as a date only widget (you must call datetimepicker javascript method with minView option set to 2) and add the date-without-time css class to the widget.
With :with_time => true, support for time selection and a date-with-time css class will be added.
Of course, it only fixes the view issues, you still have to take care of date parsing on the controller.
See http://www.malot.fr/bootstrap-datetimepicker/ for acceptable date formats
Instance Method Summary collapse
Instance Method Details
#input(_wrapper_options = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'app/inputs/dpicker_input.rb', line 23 def input( = {}) [:size] ||= limit if limit [:maxlength] ||= limit if limit [:type] ||= 'text' ['data-date-autoclose'] = true format = [:format] || :default value = if [:value] [:value] elsif object.send(attribute_name) object.send(attribute_name) end if [:with_time] ['data-date-format'] = js_format(I18n.t("time.formats.#{format}")) else ['data-date-format'] = js_format(I18n.t("date.formats.#{format}")) value = value.to_date unless value.blank? end [:value] = I18n.l(value, format: format) unless value.blank? if lang = [:language] ['data-date-language'] = lang elsif lang = I18n.locale.to_s ['data-date-language'] = lang end ['data-date-weekstart'] = [:week_start] if [:week_start] ['data-date-startDate'] = I18n.l([:start_date].to_date, format: format) if [:start_date] ['data-date-endDate'] = I18n.l([:end_date].to_date, format: format) if [:end_date] ['data-date-autoclose'] = [:autoclose] if [:autoclose] # input_html_options["data-date-startView"] = options[:start_view] if options[:start_view] @builder.text_field(attribute_name, ) end |
#input_html_classes ⇒ Object
59 60 61 62 63 64 65 |
# File 'app/inputs/dpicker_input.rb', line 59 def input_html_classes if [:with_time] super.unshift('datepicker', 'date-with-time') else super.unshift('datepicker', 'date-without-time') end end |