Class: Caboose::CalendarEventGroup

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/caboose/calendar_event_group.rb

Constant Summary collapse

PERIOD_DAY =
'Day'
PERIOD_WEEK =
'Week'
PERIOD_MONTH =
'Month'
PERIOD_YEAR =
'Year'
REPEAT_BY_DAY_OF_MONTH =
'Day of month'
REPEAT_BY_DAY_OF_WEEK =
'Day of week'

Instance Method Summary collapse

Instance Method Details

#create_eventsObject


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
# File 'app/models/caboose/calendar_event_group.rb', line 29

def create_events      
  return if self.date_start.nil?      
  return if self.date_end.nil?      
  return if self.date_end < self.date_start      
  
  e = self.calendar_events.reorder(:begin_date).first      
  return if e.nil?
  
  dates = [e.begin_date.to_date]
  if self.period == 'Day'
    d = self.date_start
    while d <= self.date_end
      if !CalendarEvent.where("calendar_event_group_id = ? and cast(begin_date as date) = ?", self.id, d).exists?
        e.duplicate(d)
      end
      dates << d.to_date
      d = d + 1.day
    end
    
  elsif self.period == 'Week'
            
    d = self.date_start - self.date_start.strftime('%w').to_i.days
    while d <= self.date_end          
      (0..6).each do |i|            
        d = d + 1
        Caboose.log("d = #{d}")
        next if d < self.date_start
        break if d > self.date_end
        w = d.strftime('%w').to_i
        if (w == 0 && self.sun) || (w == 1 && self.mon) || (w == 2 && self.tue) || (w == 3 && self.wed) || (w == 4 && self.thu) || (w == 5 && self.fri) || (w == 6 && self.sat)
          Caboose.log("Found a day")
          if !CalendarEvent.where("calendar_event_group_id = ? and cast(begin_date as date) = ?", self.id, d).exists?
            e.duplicate(d)
          end
          dates << d.to_date
        end
      end
      d = d + (self.frequency-1).weeks
    end
    
  elsif self.period == 'Month'
    d = self.date_start
    if self.repeat_by == 'Day of month'     
      while d <= self.date_end           
        if !CalendarEvent.where("calendar_event_group_id = ? and cast(begin_date as date) = ?", self.id, d).exists?
          e.duplicate(d)
        end
        dates << d.to_date
        d = d + self.frequency.months  
      end
    elsif self.repeat_by == self::REPEAT_BY_DAY_OF_WEEK
      
      #d0 = DateTime.new(d.strftime('%Y'), d.strftime('%m'), 1)
      #w = d0.strftime('%w').to_i
      #i = 0
      #while d0 <= d
      #  i = i + 1 if d0.strftime('%w').to_i == w
      #  d0 = d0 + 1.day
      #end
      #
      ## Now set the i'th occurance of the w day of the week           
      #d = DateTime.new(d.strftime('%Y'), d.strftime('%m'), 1)
      #while d <= self.date_end
      #  d0 = d
      #  while d
      #    
      #    
      #  if !CalendarEvent.where("calendar_event_group_id = ? and cast(begin_date as date) = ?", self.id, d).exists?
      #    CalendarEvent.duplicate(d)
      #  end
      #  d = d + self.frequency.months  
      #end
      
    end
    
  elsif self.period == 'Year'
    d = self.date_start
    while d <= self.date_end
      if !CalendarEvent.where("calendar_event_group_id = ? and cast(begin_date as date) = ?", self.id, d).exists?
        e.duplicate(d)
      end
      dates << d.to_date
      d = d + 1.year
    end
    
  end
  
  # Get rid of events that shouldn't be in the group            
  CalendarEvent.where("calendar_event_group_id = ? and cast(begin_date as date) not in (?)", self.id, dates).destroy_all
              
end