Module: ScheduleAttributes::Core

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord, Model
Defined in:
lib/schedule_attributes/core.rb

Instance Method Summary collapse

Instance Method Details

#schedule_attributesObject

TODO: use a proper form input model, not OpenStruct



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
134
135
136
137
138
139
140
141
# File 'lib/schedule_attributes/core.rb', line 53

def schedule_attributes
  atts = {}
  time_format = ScheduleAttributes.configuration.time_format
  schedule = read_schedule_field || ScheduleAttributes.default_schedule

  if schedule.start_time.seconds_since_midnight == 0 && schedule.end_time.nil?
    atts[:all_day] = true
  else
    atts[:start_time] = schedule.start_time.strftime(time_format)
    atts[:end_time]   = (schedule.end_time).strftime(time_format) if schedule.end_time
  end

  if rule = schedule.rrules.first
    atts[:repeat]     = 1
    atts[:start_date] = schedule.start_time.to_date
    atts[:date]       = Date.current # default for populating the other part of the form

    rule_hash = rule.to_hash
    atts[:interval] = rule_hash[:interval]

    case rule
    when IceCube::DailyRule
      atts[:interval_unit] = 'day'
    when IceCube::WeeklyRule
      atts[:interval_unit] = 'week'

      if rule_hash[:validations][:day]
        rule_hash[:validations][:day].each do |day_idx|
          atts[ ScheduleAttributes::DAY_NAMES[day_idx] ] = 1
        end
      end
    when IceCube::MonthlyRule
      atts[:interval_unit] = 'month'

      day_of_week = rule_hash[:validations][:day_of_week]
      day_of_month = rule_hash[:validations][:day_of_month]

      if day_of_week
        day_of_week = day_of_week.first.flatten
        atts[:ordinal_week] = day_of_week.first
        atts[:ordinal_unit] = 'week'
      elsif day_of_month
        atts[:ordinal_day]  = day_of_month.first
        atts[:ordinal_unit] = 'day'
      end
    when IceCube::YearlyRule
      atts[:interval_unit] = 'year'
    end

    if rule.until_time
      atts[:end_date] = rule.until_time.to_date
      atts[:ends] = 'eventually'
    else
      atts[:ends] = 'never'
    end

    if months = rule.validations_for(:month_of_year).map(&:month)
      atts[:yearly_start_month] = months.first
      atts[:yearly_end_month] = months.last

      # get leading & trailing days from exception rules
      schedule.exrules.each do |x|
        x.validations_for(:month_of_year).map(&:month).each do |m|
          days = x.validations_for(:day_of_month).map(&:day)

          if m == atts[:yearly_start_month]
            atts[:yearly_start_month_day] = days.last + 1 if days.first == 1
          end

          if m == atts[:yearly_end_month]
            if days.last == 31
              atts[:yearly_end_month_day] = days.first - 1
              atts[:yearly_start_month_day] ||= 1
            end
          end
        end
      end

    end
  else
    atts[:repeat]     = 0
    atts[:interval]   = 1
    atts[:date]       = schedule.rtimes.first.to_date
    atts[:dates]      = schedule.rtimes.map(&:to_date)
    atts[:start_date] = DateHelpers.today # default for populating the other part of the form
  end

  OpenStruct.new(atts.delete_if { |k,v| v.blank? })
end

#schedule_attributes=(options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/schedule_attributes/core.rb', line 30

def schedule_attributes=(options)
  input = ScheduleAttributes::Input.new(options)
  new_schedule = IceCube::Schedule.new(input.start_time || TimeHelpers.today)

  if input.repeat?
    parser = ScheduleAttributes::RuleParser[input.interval_unit || 'day'].new(input)
    new_schedule.add_recurrence_rule(parser.rule)
    parser.exceptions.each do |exrule|
      new_schedule.add_exception_rule(exrule)
    end
  else
    input.dates.each do |d|
      new_schedule.add_recurrence_time(d)
    end
  end

  new_schedule.duration = input.duration if input.duration

  write_schedule_field(new_schedule)
end