|
| 1 | +module ActiveAdminDatetimepicker |
| 2 | + module Base |
| 3 | + mattr_accessor :default_datetime_picker_options do |
| 4 | + {} |
| 5 | + end |
| 6 | + |
| 7 | + mattr_accessor :format do |
| 8 | + '%Y-%m-%d %H:%M' |
| 9 | + end |
| 10 | + |
| 11 | + def html_class |
| 12 | + 'date-time-picker' |
| 13 | + end |
| 14 | + |
| 15 | + def input_html_data |
| 16 | + {} |
| 17 | + end |
| 18 | + |
| 19 | + def input_html_options(input_name = nil) |
| 20 | + options = {} |
| 21 | + options[:class] = [self.options[:class], html_class].compact.join(' ') |
| 22 | + options[:data] ||= input_html_data |
| 23 | + options[:data].merge!(datepicker_options: datetime_picker_options) |
| 24 | + options[:value] ||= input_value(input_name) |
| 25 | + options[:maxlength] = 19 |
| 26 | + options |
| 27 | + end |
| 28 | + |
| 29 | + def input_value(input_name = nil) |
| 30 | + val = object.public_send(input_name || method) |
| 31 | + return DateTime.new(val.year, val.month, val.day, val.hour, val.min).strftime(format) if val.is_a?(Time) |
| 32 | + val.to_s |
| 33 | + end |
| 34 | + |
| 35 | + def datetime_picker_options |
| 36 | + @datetime_picker_options ||= begin |
| 37 | + # backport support both :datepicker_options AND :datetime_picker_options |
| 38 | + options = self.options.fetch(:datepicker_options, {}) |
| 39 | + options = self.options.fetch(:datetime_picker_options, options) |
| 40 | + options = Hash[options.map { |k, v| [k.to_s.camelcase(:lower), v] }] |
| 41 | + _default_datetime_picker_options.merge(options) |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + protected |
| 46 | + |
| 47 | + def _default_datetime_picker_options |
| 48 | + res = default_datetime_picker_options.map do |k, v| |
| 49 | + if v.respond_to?(:call) || v.is_a?(Proc) |
| 50 | + [k, v.call] |
| 51 | + else |
| 52 | + [k, v] |
| 53 | + end |
| 54 | + end |
| 55 | + Hash[res] |
| 56 | + end |
| 57 | + end |
| 58 | +end |
| 59 | + |
0 commit comments