move recurrence text helpers into patterns. move next_date calculation into

respective models
This commit is contained in:
Reinier Balt 2014-02-27 16:01:01 +01:00
parent d8507bf8b7
commit b84adfc172
13 changed files with 291 additions and 289 deletions

View file

@ -35,6 +35,48 @@ module RecurringTodos
errors[:base] << "You must specify at least one day on which the todo recurs" unless something_set
end
def get_next_date(previous)
# determine start
if previous.nil?
start = start_from.nil? ? Time.zone.now : self.start_from
else
start = previous + 1.day
if start.wday() == 0
# we went to a new week , go to the nth next week and find first match
# that week. Note that we already went into the next week, so -1
start += (every_x_week-1).week
end
unless self.start_from.nil?
# check if the start_from date is later than previous. If so, use
# start_from as start to search for next date
start = self.start_from if self.start_from > previous
end
end
day = find_first_day_in_this_week(start)
return day unless day == -1
# we did not find anything this week, so check the nth next, starting from
# sunday
start = start + self.every_x_week.week - (start.wday()).days
start = find_first_day_in_this_week(start)
return start unless start == -1
raise Exception.new, "unable to find next weekly date (#{self.every_day})"
end
private
def find_first_day_in_this_week(start)
# check if there are any days left this week for the next todo
start.wday().upto 6 do |i|
return start + (i-start.wday()).days if on_xday(i)
end
-1
end
end
end