Merge pull request #178 from TracksApp/rename-has-next-todo

Rename RecurringTodo#has_next_todo
This commit is contained in:
Matt Rogers 2013-04-27 09:52:58 -07:00
commit 8db5793bf6
3 changed files with 55 additions and 55 deletions

View file

@ -1115,7 +1115,7 @@ class TodosController < ApplicationController
# 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)
if recurring_todo.active? && recurring_todo.continues_recurring?(date_to_check)
# shift the reference date to yesterday if date_to_check is furher in
# the past. This is to make sure we do not get older todos for overdue

View file

@ -6,7 +6,7 @@ class RecurringTodo < ActiveRecord::Base
belongs_to :user
has_many :todos
scope :active, :conditions => { :state => 'active'}
scope :completed, :conditions => { :state => 'completed'}
@ -384,7 +384,7 @@ class RecurringTodo < ActiveRecord::Base
return I18n.t("todos.recurrence.pattern.every_day")
end
end
def weekly_recurrence_pattern
if every_other1 > 1
return I18n.t("todos.recurrence.pattern.every_n", :n => every_other1) + " " + I18n.t("common.weeks")
@ -392,7 +392,7 @@ class RecurringTodo < ActiveRecord::Base
return I18n.t('todos.recurrence.pattern.weekly')
end
end
def monthly_recurrence_pattern
return "invalid repeat pattern" if every_other2.nil?
if self.recurrence_selector == 0
@ -412,7 +412,7 @@ class RecurringTodo < ActiveRecord::Base
:x => self.xth, :day => self.day_of_week, :n_months => n_months)
end
end
def yearly_recurrence_pattern
if self.recurrence_selector == 0
return I18n.t("todos.recurrence.pattern.every_year_on",
@ -422,7 +422,7 @@ class RecurringTodo < ActiveRecord::Base
:date => I18n.t("todos.recurrence.pattern.the_xth_day_of_month", :x => self.xth, :day => self.day_of_week, :month => self.month_of_year))
end
end
def recurrence_pattern
return "invalid repeat pattern" if every_other1.nil?
case recurring_period
@ -658,10 +658,10 @@ class RecurringTodo < ActiveRecord::Base
return nil
end
def has_next_todo(previous)
def continues_recurring?(previous)
return self.occurences_count < self.number_of_occurences unless self.number_of_occurences.nil?
return true if self.end_date.nil? || self.ends_on == 'no_end_date'
case self.target
when 'due_date'
return get_due_date(previous) <= self.end_date
@ -673,7 +673,7 @@ class RecurringTodo < ActiveRecord::Base
end
def done?(end_date)
!has_next_todo(end_date)
!continues_recurring?(end_date)
end
def toggle_completion!

View file

@ -22,14 +22,14 @@ class RecurringTodoTest < ActiveSupport::TestCase
@wednesday = Time.zone.local(2008,6,11)
@thursday = Time.zone.local(2008,6,12)
end
def test_pattern_text
assert_equal "every day", @every_day.recurrence_pattern
assert_equal "on work days", @every_workday.recurrence_pattern
assert_equal "every last friday of every 2 months", @monthly_every_last_friday.recurrence_pattern
assert_equal "every year on June 08", @yearly.recurrence_pattern
end
def test_daily_every_day
# every_day should return todays date if there was no previous date
due_date = @every_day.get_due_date(nil)
@ -39,32 +39,32 @@ class RecurringTodoTest < ActiveSupport::TestCase
# when the last todo was completed today, the next todo is due tomorrow
due_date =@every_day.get_due_date(@today)
assert_equal @tomorrow, due_date
# do something every 14 days
@every_day.every_other1=14
due_date = @every_day.get_due_date(@today)
assert_equal @today+14.days, due_date
end
def test_daily_work_days
assert_equal @monday, @every_workday.get_due_date(@friday)
assert_equal @monday, @every_workday.get_due_date(@saturday)
assert_equal @monday, @every_workday.get_due_date(@sunday)
assert_equal @tuesday, @every_workday.get_due_date(@monday)
end
def test_show_from_date
# assume that target due_date works fine, i.e. don't do the same tests over
@every_day.target='show_from_date'
# when recurrence is targeted on show_from, due date shoult remain nil
assert_equal nil, @every_day.get_due_date(nil)
assert_equal nil, @every_day.get_due_date(@today-3.days)
# check show from get the next day
assert_equal_dmy @today, @every_day.get_show_from_date(@today-1.days)
assert_equal @today+1.day, @every_day.get_show_from_date(@today)
@every_day.target='due_date'
# when target on due_date, show_from is relative to due date unless show_always is true
@every_day.show_always = true
@ -73,30 +73,30 @@ class RecurringTodoTest < ActiveSupport::TestCase
@every_day.show_always = false
@every_day.show_from_delta=10
assert_equal_dmy @today, @every_day.get_show_from_date(@today+9.days) #today+1+9-10
# when show_from is 0, show_from is the same day it's due
@every_day.show_from_delta=0
assert_equal @every_day.get_due_date(@today+9.days), @every_day.get_show_from_date(@today+9.days)
# when show_from is nil, show always (happend in tests)
@every_day.show_from_delta=nil
assert_equal nil, @every_day.get_show_from_date(@today+9.days)
# TODO: show_from has no use case for daily pattern. Need to test on
# weekly/monthly/yearly
end
def test_end_date_on_recurring_todo
assert_equal true, @every_day.has_next_todo(@in_three_days)
assert_equal true, @every_day.has_next_todo(@in_four_days)
assert_equal true, @every_day.continues_recurring?(@in_three_days)
assert_equal true, @every_day.continues_recurring?(@in_four_days)
@every_day.end_date = @in_four_days
@every_day.ends_on = 'ends_on_end_date'
assert_equal false, @every_day.has_next_todo(@in_four_days)
assert_equal false, @every_day.continues_recurring?(@in_four_days)
end
def test_weekly_every_day_setters
@weekly_every_day.every_day = ' '
@weekly_every_day.weekly_return_sunday=('s')
assert_equal 's ', @weekly_every_day.every_day
@weekly_every_day.weekly_return_monday=('m')
@ -111,18 +111,18 @@ class RecurringTodoTest < ActiveSupport::TestCase
assert_equal 'smtwtf ', @weekly_every_day.every_day
@weekly_every_day.weekly_return_saturday=('s')
assert_equal 'smtwtfs', @weekly_every_day.every_day
# test remove
@weekly_every_day.weekly_return_wednesday=(' ')
assert_equal 'smt tfs', @weekly_every_day.every_day
end
def test_weekly_pattern
assert_equal true, @weekly_every_day.has_next_todo(nil)
assert_equal true, @weekly_every_day.continues_recurring?(nil)
due_date = @weekly_every_day.get_due_date(@sunday)
assert_equal @monday, due_date
# saturday is last day in week, so the next date should be sunday + n-1 weeks
# n-1 because sunday is already in the next week
@weekly_every_day.every_other1 = 3
@ -135,7 +135,7 @@ class RecurringTodoTest < ActiveSupport::TestCase
assert_equal 'sm tfs', @weekly_every_day.every_day
due_date = @weekly_every_day.get_due_date(@monday)
assert_equal @thursday, due_date
@weekly_every_day.every_other1 = 1
@weekly_every_day.every_day = ' tw '
due_date = @weekly_every_day.get_due_date(@tuesday)
@ -147,15 +147,15 @@ class RecurringTodoTest < ActiveSupport::TestCase
due_date = @weekly_every_day.get_due_date(@sunday)
assert_equal @saturday+1.week, due_date
end
def test_monthly_pattern
due_date = @monthly_every_last_friday.get_due_date(@sunday)
assert_equal Time.zone.local(2008,6,27), due_date
friday_is_last_day_of_month = Time.zone.local(2008,10,31)
due_date = @monthly_every_last_friday.get_due_date(friday_is_last_day_of_month-1.day )
assert_equal friday_is_last_day_of_month , due_date
@monthly_every_third_friday = @monthly_every_last_friday
@monthly_every_third_friday.every_other3=3 #third
due_date = @monthly_every_last_friday.get_due_date(@sunday) # june 8th 2008
@ -163,24 +163,24 @@ class RecurringTodoTest < ActiveSupport::TestCase
# set date past third friday of this month
due_date = @monthly_every_last_friday.get_due_date(Time.zone.local(2008,6,21)) # june 21th 2008
assert_equal Time.zone.local(2008, 8, 15), due_date # every 2 months, so aug
@monthly = @monthly_every_last_friday
@monthly.recurrence_selector=0
@monthly.every_other1 = 8 # every 8th day of the month
@monthly.every_other2 = 2 # every 2 months
due_date = @monthly.get_due_date(@saturday) # june 7th
assert_equal @sunday, due_date # june 8th
due_date = @monthly.get_due_date(@sunday) # june 8th
assert_equal Time.zone.local(2008,8,8), due_date # aug 8th
end
def test_yearly_pattern
# beginning of same year
due_date = @yearly.get_due_date(Time.zone.local(2008,2,10)) # feb 10th
assert_equal @sunday, due_date # june 8th
# same month, previous date
due_date = @yearly.get_due_date(@saturday) # june 7th
show_from_date = @yearly.get_show_from_date(@saturday) # june 7th
@ -193,7 +193,7 @@ class RecurringTodoTest < ActiveSupport::TestCase
# very overdue
due_date = @yearly.get_due_date(@monday+5.months-2.days) # november 7
assert_equal Time.zone.local(2009,6,8), due_date # june 8th next year
@yearly.recurrence_selector = 1
@yearly.every_other3 = 2 # second
@yearly.every_count = 3 # wednesday
@ -245,7 +245,7 @@ class RecurringTodoTest < ActiveSupport::TestCase
assert_equal @in_three_days, due_date
due_date = @every_day.get_due_date(@tomorrow)
assert_equal @in_three_days, due_date
# if we give a date in the future for the previous todo, the next to do
# should be based on that future date.
due_date = @every_day.get_due_date(@in_four_days)
@ -266,13 +266,13 @@ class RecurringTodoTest < ActiveSupport::TestCase
assert_equal Time.zone.local(2021,6,8), @yearly.get_due_date(nil) # jun 8th next year
assert_equal Time.zone.local(2021,6,8), @yearly.get_due_date(Time.zone.local(2019,6,1)) # also next year
assert_equal Time.zone.local(2021,6,8), @yearly.get_due_date(Time.zone.local(2020,6,15)) # also next year
this_year = Time.now.utc.year
@yearly.start_from = Time.zone.local(this_year+1,6,12)
due_date = @yearly.get_due_date(nil)
assert_equal due_date.year, this_year+2
end
def test_toggle_completion
assert @yearly.active?
assert @yearly.toggle_completion!
@ -283,40 +283,40 @@ class RecurringTodoTest < ActiveSupport::TestCase
assert @yearly.toggle_completion!
assert @yearly.active?
# re-entering active state should clear completed_at
assert @yearly.completed_at.nil?
end
def test_starred
@yearly.tag_with("1, 2, starred")
@yearly.tags.reload
assert @yearly.starred?
assert !@weekly_every_day.starred?
@yearly.toggle_star!
assert !@yearly.starred?
@yearly.toggle_star!
assert @yearly.starred?
end
def test_occurence_count
@every_day.number_of_occurences = 2
assert_equal true, @every_day.has_next_todo(@in_three_days)
assert_equal true, @every_day.continues_recurring?(@in_three_days)
@every_day.increment_occurrences
assert_equal true, @every_day.has_next_todo(@in_three_days)
assert_equal true, @every_day.continues_recurring?(@in_three_days)
@every_day.increment_occurrences
assert_equal false, @every_day.has_next_todo(@in_three_days)
assert_equal false, @every_day.continues_recurring?(@in_three_days)
# after completion, when you reactivate the recurring todo, the occurences
# count should be reset
assert_equal 2, @every_day.occurences_count
assert @every_day.toggle_completion!
assert @every_day.toggle_completion!
assert_equal true, @every_day.has_next_todo(@in_three_days)
assert_equal true, @every_day.continues_recurring?(@in_three_days)
assert_equal 0, @every_day.occurences_count
end
end