Class: ScheduleInput

Inherits:
SimpleForm::Inputs::Base
  • Object
show all
Defined in:
lib/generators/schedulable/templates/inputs/schedule_input.rb

Instance Method Summary collapse

Instance Method Details

#input(wrapper_options) ⇒ Object



3
4
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/generators/schedulable/templates/inputs/schedule_input.rb', line 3

def input(wrapper_options)
  
  
  # I18n
  weekdays = Date::DAYNAMES.map(&:downcase)
  weekdays = weekdays.slice(1..7) << weekdays.slice(0)
  
  day_names = I18n.t('date.day_names', default: "")
  day_names = day_names.blank? ? weekdays.map { |day| day.capitalize } : day_names.slice(1..7) << day_names.slice(0)
  day_labels = Hash[weekdays.zip(day_names)]
  
  # Pass in default month names when missing in translations
  month_names = I18n.t('date.month_names', default: "")
  month_names = month_names.blank? ? Date::MONTHNAMES : month_names

  # Pass in default order when missing in translations
  date_order = I18n.t('date.order', default: "")
  date_order = date_order.blank? ? [:year, :month, :day] : date_order 
  
  date_options = {
    order: date_order,
    use_month_names: month_names
  }
  
  # Input html options
  input_html_options[:type] ||= input_type if html5?
  
  # Input options
  input_options[:interval] = !input_options[:interval].nil? ? input_options[:interval] : false
  input_options[:until] = !input_options[:until].nil? ? input_options[:until] : false
  input_options[:count] = !input_options[:count].nil? ? input_options[:count] : false

  @builder.simple_fields_for(:schedule, @builder.object.schedule || @builder.object.build_schedule) do |b|

    # Javascript element id
    field_id = b.object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/,"_").sub(/_$/,"")
    
    b.template.("div", {id: field_id}) do
      
      b.input(:rule, collection: ['singular', 'daily', 'weekly', 'monthly'], label_method: lambda { |v| I18n.t("schedulable.rules.#{v}", default: v.capitalize) }, label: false, include_blank: false) << 
      
      template.("div", {data: {group: 'singular'}}) do
        b.input :date, date_options
      end <<
      
      template.("div", {data: {group: 'weekly'}}) do
        b.input :day, collection: weekdays, label_method: lambda { |v| ("&nbsp;" + day_labels[v]).html_safe}, boolean_style: :nested, as: :check_boxes
      end <<
      
      template.("div", {data: {group: 'monthly'}}) do
        
        b.simple_fields_for :day_of_week, OpenStruct.new(b.object.day_of_week || {}) do |db|
          template.("div", class: 'form-group' + (b.object.errors[:day_of_week].any? ? " has-error" : "")) do
            b.label(:day_of_week, error: true) << 
            template.("div", nil, style: 'min-width: 280px; display: table') do
              template.("div", nil, style: 'display: table-row') do
                template.("span", nil, style: 'display: table-cell;') <<
                ['1st', '2nd', '3rd', '4th', 'last'].reduce(''.html_safe) { | content, item | 
                  content << template.("span", I18n.t("schedulable.monthly_week_names.#{item}", default: item.to_s), style: 'display: table-cell; text-align: center')
                }
              end <<
              weekdays.reduce(''.html_safe) do | content, weekday | 
                content << template.("div", nil, style: 'display: table-row') do 
                  template.("span", day_labels[weekday] || weekday, style: 'display: table-cell') <<
                  db.collection_check_boxes(weekday.to_sym, [1, 2, 3, 4, -1], lambda { |i| i} , lambda { |i| "&nbsp;".html_safe}, boolean_style: :inline, label: false, checked: db.object.send(weekday), inline_label: false, item_wrapper_tag: nil) do |cb|
                    template.("span", style: 'display: table-cell; text-align: center;') { cb.check_box(class: "check_box") }
                  end 
                end
              end
            end << 
            b.error(:day_of_week)
          end
        end
      end << 
      
      template.("div", data: {group: 'singular,daily,weekly,monthly'}) do
        b.input :time, date_options
      end << 
      
      (if input_options[:interval]
        template.("div", data: {group: 'daily,weekly,monthly'}) do
          b.input :interval
        end
      else
        b.input(:interval, as: :hidden, input_html: {value: 1})
      end) <<
      
      (if input_options[:until]
        template.("div", data: {group: 'daily,weekly,monthly'}) do
          b.input :until, date_options
        end
      else
        b.input(:until, as: :hidden, input_html: {value: nil})
      end) <<
      
      if input_options[:count]
        template.("div", data: {group: 'daily,weekly,monthly'}) do
          b.input :count
        end
      else
         b.input(:count, as: :hidden, input_html: {value: 0})
      end
      
      
      
    end <<
    
    template.javascript_tag(
      "(function() {" << 
      "  var container = document.querySelectorAll('##{field_id}'); container = container[container.length - 1]; " << 
      "  var select = container.querySelector(\"select[name*='rule']\"); " << 
      "  function update() {" <<
      "    var value = this.value;" << 
      "    [].slice.call(container.querySelectorAll(\"*[data-group]\")).forEach(function(elem) { " <<
      "      var groups = elem.getAttribute('data-group').split(',');" <<
      "      if (groups.indexOf(value) >= 0) {" <<
      "        elem.style.display = ''" << 
      "      } else {" <<
      "        elem.style.display = 'none'" << 
      "      }" <<
      "    });" <<
      "  }" << 
      "  if (jQuery) { jQuery(select).on('change', update); } else { select.addEventListener('change', update); }" <<
      "  update.call(select);" << 
      "})()"
    )
    
  end
  
  
end