From f21908a2a0bf349dee694ba971b23a7d5f7e9984 Mon Sep 17 00:00:00 2001 From: Reinier Balt Date: Wed, 29 Oct 2008 16:40:45 +0100 Subject: [PATCH] Fixes corner case where checking a monthly recurring todo complete on the same day the todo comes from tickler, a new todo is created and not put in the tickler for next month, but for today --- app/controllers/todos_controller.rb | 10 +++--- test/functional/todos_controller_test.rb | 46 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index b4cd1935..6bfbc7e3 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -781,10 +781,8 @@ class TodosController < ApplicationController # check for next todo either from the due date or the show_from date date_to_check = todo.due.nil? ? todo.show_from : todo.due - # if both due and show_from are nil, check for a next todo with yesterday - # as reference point. We pick yesterday so that new todos for today will - # be created instead of new todos for tomorrow. - date_to_check = Time.zone.now-1.day if date_to_check.nil? + # if both due and show_from are nil, check for a next todo from now + date_to_check = Time.zone.now if date_to_check.nil? if recurring_todo.active? && recurring_todo.has_next_todo(date_to_check) @@ -792,7 +790,9 @@ class TodosController < ApplicationController # the past. This is to make sure we do not get older todos for overdue # todos. I.e. checking a daily todo that is overdue with 5 days will # create a new todo which is overdue by 4 days if we don't shift the - # date. Discard the time part in the compare + # date. Discard the time part in the compare. We pick yesterday so that + # new todos due for today will be created instead of new todos for + # tomorrow. date = date_to_check.at_midnight >= Time.zone.now.at_midnight ? date_to_check : Time.zone.now-1.day new_recurring_todo = create_todo_from_recurring_todo(recurring_todo, date) diff --git a/test/functional/todos_controller_test.rb b/test/functional/todos_controller_test.rb index 0b5d8b1a..9804b0b5 100644 --- a/test/functional/todos_controller_test.rb +++ b/test/functional/todos_controller_test.rb @@ -406,6 +406,52 @@ class TodosControllerTest < Test::Rails::TestCase # check that the todo is in the tickler assert !next_todo.show_from.nil? end + + def test_toggle_check_on_rec_todo_show_from_today + login_as(:admin_user) + + # link todo_1 and recurring_todo_1 + recurring_todo_1 = RecurringTodo.find(1) + todo_1 = Todo.find_by_recurring_todo_id(1) + today = Time.now.utc.at_midnight + + # change recurrence pattern to monthly and set show_from to today + recurring_todo_1.target = 'show_from_date' + recurring_todo_1.recurring_period = 'monthly' + recurring_todo_1.recurrence_selector = 0 + recurring_todo_1.every_other1 = today.day + recurring_todo_1.every_other2 = 1 + recurring_todo_1.save + + # mark todo_1 as complete by toggle_check, this gets rid of todo_1 that was + # not correctly created from the adjusted recurring pattern we defined + # above. + xhr :post, :toggle_check, :id => todo_1.id, :_source_view => 'todo' + todo_1.reload + assert todo_1.completed? + + # locate the new todo. This todo is created from the adjusted recurring + # pattern defined in this test + new_todo = Todo.find(:first, :conditions => {:recurring_todo_id => recurring_todo_1.id, :state => 'active'}) + assert !new_todo.nil? + + # mark new_todo as complete by toggle_check + xhr :post, :toggle_check, :id => new_todo.id, :_source_view => 'todo' + new_todo.reload + assert todo_1.completed? + + # locate the new todo in tickler + new_todo = Todo.find(:first, :conditions => {:recurring_todo_id => recurring_todo_1.id, :state => 'deferred'}) + assert !new_todo.nil? + + assert_equal "Call Bill Gates every day", new_todo.description + # check that the new todo is not the same as todo_1 + assert_not_equal todo_1.id, new_todo.id + + # check that the new_todo is in the tickler to show next month + assert !new_todo.show_from.nil? + assert_equal Time.utc(today.year, today.month+1, today.day), new_todo.show_from + end def test_check_for_next_todo login_as :admin_user