2019-12-18 09:49:57 -06:00
|
|
|
# typed: true
|
2014-01-27 16:42:54 +01:00
|
|
|
module RecurringTodos
|
|
|
|
|
2015-02-10 16:25:27 +01:00
|
|
|
class DailyRecurrencePattern < AbstractRecurrencePattern
|
2014-01-27 16:42:54 +01:00
|
|
|
|
2014-02-07 22:55:52 +01:00
|
|
|
def initialize(user)
|
|
|
|
super user
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def every_x_days
|
2014-02-08 11:51:09 +01:00
|
|
|
get :every_other1
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
2014-02-08 11:51:09 +01:00
|
|
|
def only_work_days?
|
|
|
|
get :only_work_days
|
|
|
|
end
|
|
|
|
|
2014-02-23 15:54:02 +01:00
|
|
|
def recurrence_pattern
|
|
|
|
if only_work_days?
|
|
|
|
I18n.t("todos.recurrence.pattern.on_work_days")
|
|
|
|
elsif every_x_days > 1
|
2014-03-03 21:48:25 +01:00
|
|
|
I18n.t("todos.recurrence.pattern.every_n_days", :n => every_x_days)
|
2014-02-23 15:54:02 +01:00
|
|
|
else
|
|
|
|
I18n.t("todos.recurrence.pattern.every_day")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-08 11:51:09 +01:00
|
|
|
def validate
|
|
|
|
super
|
|
|
|
errors[:base] << "Every other nth day may not be empty for this daily recurrence setting" if (!only_work_days?) && every_x_days.blank?
|
|
|
|
end
|
|
|
|
|
2014-02-27 16:01:01 +01:00
|
|
|
def get_next_date(previous)
|
|
|
|
# previous is the due date of the previous todo or it is the completed_at
|
|
|
|
# date when the completed_at date is after due_date (i.e. you did not make
|
|
|
|
# the due date in time)
|
|
|
|
|
|
|
|
start = determine_start(previous, 1.day)
|
|
|
|
|
|
|
|
if only_work_days?
|
2014-03-03 21:48:25 +01:00
|
|
|
# jump over weekend if necessary
|
2014-02-27 16:01:01 +01:00
|
|
|
return start + 2.day if start.wday() == 6 # saturday
|
|
|
|
return start + 1.day if start.wday() == 0 # sunday
|
|
|
|
return start
|
2014-08-14 21:05:05 -05:00
|
|
|
else
|
2014-02-27 16:01:01 +01:00
|
|
|
# if there was no previous todo, do not add n: the first todo starts on
|
|
|
|
# today or on start_from
|
|
|
|
return previous == nil ? start : start+every_x_days.day-1.day
|
|
|
|
end
|
|
|
|
end
|
2014-02-08 11:51:09 +01:00
|
|
|
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
2014-08-14 21:05:05 -05:00
|
|
|
end
|