Class: DatepickerWithTimeInput

Inherits:
Object
  • Object
show all
Includes:
Formtastic::Inputs::Base
Defined in:
lib/hellobase/formtastic/datepicker_with_time_input.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_virtual_attributes(klass, base) ⇒ Object



14
15
16
17
18
19
20
21
22
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
# File 'lib/hellobase/formtastic/datepicker_with_time_input.rb', line 14

def self.define_virtual_attributes(klass, base)
  attrs = virtual_attributes(base)
  ivars = attrs.map {|a| :"@#{a}" }
  method = :"_dpwt_set_#{base}"

  klass.class_eval do
    define_method attrs[0] do
      send(base)&.strftime('%Y-%m-%d')
    end

    define_method attrs[1] do
      send(base)&.strftime('%H')
    end

    define_method attrs[2] do
      send(base)&.strftime('%M')
    end

    attrs.each_with_index do |a, i|
      define_method :"#{a}=" do |val|
        instance_variable_set ivars[i], val
        send(method)

        val
      end
    end

    define_method method do
      send :"#{base}=",
        ivars.any? {|v| instance_variable_get(v).blank? } ?
          nil :
          [
            instance_variable_get(ivars[0]),
            [
              instance_variable_get(ivars[1]),
              instance_variable_get(ivars[2])
            ].join(':')
          ].join(' ')
    end

    private method
  end
end

.virtual_attributes(base) ⇒ Object



10
11
12
# File 'lib/hellobase/formtastic/datepicker_with_time_input.rb', line 10

def self.virtual_attributes(base)
  [:"_dpwt_#{base}_d", :"_dpwt_#{base}_h", :"_dpwt_#{base}_m"]
end

Instance Method Details

#to_htmlObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hellobase/formtastic/datepicker_with_time_input.rb', line 58

def to_html
  datepicker_html = ActiveAdmin::Inputs::DatepickerInput.new(builder, template, object, object_name, :"_dpwt_#{method}_d", datepicker_options).to_html
  hour_select_html = Formtastic::Inputs::SelectInput.new(builder, template, object, object_name, :"_dpwt_#{method}_h", hour_select_options).to_html
  minute_select_html = Formtastic::Inputs::SelectInput.new(builder, template, object, object_name, :"_dpwt_#{method}_m", minute_select_options).to_html
  zone_display_html = options[:time_zone] ? template.(:span, options[:time_zone]) : nil

  wrapper_contents = [
    replace_li_tag(datepicker_html),
    replace_li_tag(hour_select_html),
    replace_li_tag(minute_select_html),
    zone_display_html,
    error_html,
    hint_html,
  ].compact.join("\n").html_safe

  template.(:li, wrapper_contents, wrapper_html_options)
end