Fix #1045. The tests broke because of this fix cfc6d117b8. This exposed a new corner case that I fixed and created a test for. Also a small refactoring.

This commit is contained in:
Reinier Balt 2010-07-29 16:37:22 +02:00
parent 9a243b015a
commit 3d75cd2457
3 changed files with 42 additions and 36 deletions

View file

@ -511,30 +511,12 @@ class RecurringTodo < ActiveRecord::Base
#
# assumes self.recurring_period == 'daily'
# determine start
if previous.nil?
start = self.start_from.nil? ? Time.zone.now : self.start_from
else
# use the next day
start = previous + 1.day
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
start = determine_start(previous, 1.day)
if self.only_work_days
if start.wday() >= 1 && start.wday() <= 5 # 1=monday; 5=friday
return start
else
if start.wday() == 0 # sunday
return start + 1.day
else # saturday
return start + 2.day
end
end
return start + 2.day if start.wday() == 6 # saturday
return start + 1.day if start.wday() == 0 # sunday
return start
else # every nth day; n = every_other1
# if there was no previous todo, do not add n: the first todo starts on
# today or on start_from
@ -742,11 +724,15 @@ class RecurringTodo < ActiveRecord::Base
protected
def determine_start(previous)
def determine_start(previous, offset=0.day)
# offset needs to be 1.day for daily patterns
if previous.nil?
start = self.start_from.nil? ? Time.zone.now : self.start_from
# skip to present
start = Time.zone.now if Time.zone.now > start
else
start = previous
start = previous + offset
unless self.start_from.nil?
# check if the start_from date is later than previous. If so, use

View file

@ -1,22 +1,26 @@
<%
def today
Time.zone.now.beginning_of_day.to_s(:db)
Time.zone.now.beginning_of_day.to_s(:db)
end
def next_week
1.week.from_now.beginning_of_day.to_s(:db)
1.week.from_now.beginning_of_day.to_s(:db)
end
def last_week
1.week.ago.beginning_of_day.to_s(:db)
1.week.ago.beginning_of_day.to_s(:db)
end
def two_weeks_ago
2.weeks.ago.beginning_of_day.to_s(:db)
2.weeks.ago.beginning_of_day.to_s(:db)
end
def two_weeks_hence
2.weeks.from_now.beginning_of_day.to_s(:db)
2.weeks.from_now.beginning_of_day.to_s(:db)
end
def way_back
Time.zone.local(2008,1,1)
end
%>
@ -29,7 +33,7 @@ call_bill_gates_every_day:
description: Call Bill Gates every day
notes: ~
state: active
start_from: <%= last_week %>
start_from: <%= way_back %>
ends_on: no_end_date
end_date: ~
number_of_occurences: ~
@ -83,7 +87,7 @@ call_bill_gates_every_week:
description: Call Bill Gates every week
notes: ~
state: active
start_from: <%= today %>
start_from: <%= way_back %>
ends_on: no_end_date
end_date: ~
number_of_occurences: ~
@ -110,7 +114,7 @@ check_with_bill_every_last_friday_of_month:
description: Check with Bill every last friday of the month
notes: ~
state: active
start_from: <%= today %>
start_from: <%= way_back %>
ends_on: no_end_date
end_date: ~
number_of_occurences: ~
@ -137,7 +141,7 @@ birthday_reinier:
description: Congratulate Reinier on his birthday
notes: ~
state: active
start_from: <%= today %>
start_from: <%= way_back %>
ends_on: no_end_date
end_date: ~
number_of_occurences: ~

View file

@ -207,11 +207,27 @@ class RecurringTodoTest < ActiveSupport::TestCase
# same month, after second wednesday
due_date = @yearly.get_due_date(Time.zone.local(2008,6,12)) # june 7th
assert_equal Time.zone.local(2009,6,10), due_date # june 10th
# test handling of nil
end
def test_next_todo_without_previous_todo
# test handling of nil as previous
#
# start_from is way_back
due_date1 = @yearly.get_due_date(nil)
due_date2 = @yearly.get_due_date(Time.now.utc + 1.day)
assert_equal due_date1, due_date2
# start_from is in the future
@yearly.start_from = Time.now.utc + 1.week
due_date1 = @yearly.get_due_date(nil)
due_date2 = @yearly.get_due_date(Time.now.utc + 1.day)
assert_equal due_date1, due_date2
# start_from is nil
@yearly.start_from = nil
due_date1 = @yearly.get_due_date(nil)
due_date2 = @yearly.get_due_date(Time.now.utc + 1.day)
assert_equal due_date1, due_date2
end
def test_last_sunday_of_march