small refactorings and add some tests

This commit is contained in:
Reinier Balt 2014-03-31 11:09:00 +02:00
parent 3cb18cd875
commit ed039d4c4a
5 changed files with 133 additions and 82 deletions

View file

@ -34,8 +34,7 @@ module RecurringTodos
def recurrence_pattern
if self.recurrence_selector == 0
I18n.t("todos.recurrence.pattern.every_year_on",
:date => I18n.l(DateTime.new(Time.zone.now.year, month_of_year, every_x_day), :format => :month_day))
I18n.t("todos.recurrence.pattern.every_year_on", :date => date_as_month_day)
else
I18n.t("todos.recurrence.pattern.every_year_on",
:date => I18n.t("todos.recurrence.pattern.the_xth_day_of_month",
@ -63,34 +62,47 @@ module RecurringTodos
def get_next_date(previous)
start = determine_start(previous)
day = every_x_day
month = get(:every_other2)
case recurrence_selector
when 0 # specific day of a specific month
if start.month > month || (start.month == month && start.day >= day)
# if there is no next month n and day m in this year, search in next
# year
start = Time.zone.local(start.year+1, month, 1)
else
# if there is a next month n, stay in this year
start = Time.zone.local(start.year, month, 1)
end
Time.zone.local(start.year, month, day)
return get_specific_day_of_month(start, month)
when 1 # relative weekday of a specific month
# if there is no next month n in this year, search in next year
the_next = start.month > month ? Time.zone.local(start.year+1, month, 1) : start
# get the xth day of the month
the_next = get_xth_day_of_month(self.every_xth_day, day_of_week, month, the_next.year)
# if the_next is before previous, we went back into the past, so try next
# year
the_next = get_xth_day_of_month(self.every_xth_day, day_of_week, month, start.year+1) if the_next <= start
the_next
return get_relative_weekday_of_month(start, month)
end
nil
end
private
def date_as_month_day
I18n.l(DateTime.new(Time.zone.now.year, month_of_year, every_x_day), :format => :month_day)
end
def get_specific_day_of_month(start, month)
if start.month > month || (start.month == month && start.day >= every_x_day)
# if there is no next month n and day m in this year, search in next
# year
start = Time.zone.local(start.year+1, month, 1)
else
# if there is a next month n, stay in this year
start = Time.zone.local(start.year, month, 1)
end
Time.zone.local(start.year, month, every_x_day)
end
def get_relative_weekday_of_month(start, month)
# if there is no next month n in this year, search in next year
the_next = start.month > month ? Time.zone.local(start.year+1, month, 1) : start
# get the xth day of the month
the_next = get_xth_day_of_month(self.every_xth_day, day_of_week, month, the_next.year)
# if the_next is before previous, we went back into the past, so try next
# year
the_next = get_xth_day_of_month(self.every_xth_day, day_of_week, month, start.year+1) if the_next <= start
the_next
end
end