2014-01-27 16:42:54 +01:00
|
|
|
module RecurringTodos
|
|
|
|
|
|
|
|
|
|
class AbstractRepeatPattern
|
|
|
|
|
|
2014-02-07 22:55:52 +01:00
|
|
|
attr_accessor :attributes
|
|
|
|
|
|
|
|
|
|
def initialize(user)
|
2014-01-27 16:42:54 +01:00
|
|
|
@user = user
|
|
|
|
|
end
|
|
|
|
|
|
2014-02-08 11:51:09 +01:00
|
|
|
def start_from
|
|
|
|
|
get :start_from
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def end_date
|
|
|
|
|
get :end_date
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def ends_on
|
|
|
|
|
get :ends_on
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def target
|
|
|
|
|
get :target
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def show_always
|
|
|
|
|
get :show_always
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def show_from_delta
|
|
|
|
|
get :show_from_delta
|
|
|
|
|
end
|
|
|
|
|
|
2014-02-23 15:54:02 +01:00
|
|
|
def recurring_target_as_text
|
|
|
|
|
target == 'due_date' ? I18n.t("todos.recurrence.pattern.due") : I18n.t("todos.recurrence.pattern.show")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def recurrence_pattern
|
|
|
|
|
raise "Should not call AbstractRepeatPattern.recurrence_pattern directly. Overwrite in subclass"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def xth(x)
|
|
|
|
|
xth_day = [
|
|
|
|
|
I18n.t('todos.recurrence.pattern.first'),I18n.t('todos.recurrence.pattern.second'),I18n.t('todos.recurrence.pattern.third'),
|
|
|
|
|
I18n.t('todos.recurrence.pattern.fourth'),I18n.t('todos.recurrence.pattern.last')]
|
|
|
|
|
x.nil? ? '??' : xth_day[x-1]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def day_of_week_as_text(day)
|
|
|
|
|
day.nil? ? '??' : I18n.t('todos.recurrence.pattern.day_names')[day]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def month_of_year_as_text(month)
|
|
|
|
|
month.nil? ? '??' : I18n.t('todos.recurrence.pattern.month_names')[month]
|
|
|
|
|
end
|
|
|
|
|
|
2014-02-09 21:48:52 +01:00
|
|
|
def build_recurring_todo(attribute_handler)
|
2014-02-10 11:45:25 +01:00
|
|
|
@recurring_todo = @user.recurring_todos.build(attribute_handler.safe_attributes)
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
2014-02-09 21:48:52 +01:00
|
|
|
def update_recurring_todo(recurring_todo, attribute_handler)
|
2014-02-10 11:45:25 +01:00
|
|
|
recurring_todo.assign_attributes(attribute_handler.safe_attributes)
|
2014-02-03 10:48:21 +01:00
|
|
|
recurring_todo
|
|
|
|
|
end
|
2014-02-08 11:51:09 +01:00
|
|
|
|
|
|
|
|
def build_from_recurring_todo(recurring_todo)
|
|
|
|
|
@recurring_todo = recurring_todo
|
2014-02-09 21:48:52 +01:00
|
|
|
@attributes = Tracks::AttributeHandler.new(@user, recurring_todo.attributes)
|
2014-02-08 11:51:09 +01:00
|
|
|
end
|
|
|
|
|
|
2014-02-10 19:39:39 +01:00
|
|
|
def valid?
|
|
|
|
|
@recurring_todo.valid?
|
|
|
|
|
end
|
|
|
|
|
|
2014-02-08 12:26:49 +01:00
|
|
|
def validate_not_blank(object, msg)
|
|
|
|
|
errors[:base] << msg if object.blank?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def validate_not_nil(object, msg)
|
|
|
|
|
errors[:base] << msg if object.nil?
|
|
|
|
|
end
|
|
|
|
|
|
2014-02-08 11:51:09 +01:00
|
|
|
def validate
|
|
|
|
|
starts_and_ends_on_validations
|
|
|
|
|
set_recurrence_on_validations
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def starts_and_ends_on_validations
|
2014-02-08 12:26:49 +01:00
|
|
|
validate_not_blank(start_from, "The start date needs to be filled in")
|
2014-02-08 11:51:09 +01:00
|
|
|
case ends_on
|
|
|
|
|
when 'ends_on_number_of_times'
|
2014-02-08 12:26:49 +01:00
|
|
|
validate_not_blank(number_of_occurences, "The number of recurrences needs to be filled in for 'Ends on'")
|
2014-02-08 11:51:09 +01:00
|
|
|
when "ends_on_end_date"
|
2014-02-08 12:26:49 +01:00
|
|
|
validate_not_blank(end_date, "The end date needs to be filled in for 'Ends on'")
|
2014-02-08 11:51:09 +01:00
|
|
|
else
|
|
|
|
|
errors[:base] << "The end of the recurrence is not selected" unless ends_on == "no_end_date"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def set_recurrence_on_validations
|
|
|
|
|
# show always or x days before due date. x not null
|
|
|
|
|
case target
|
|
|
|
|
when 'show_from_date'
|
|
|
|
|
# no validations
|
|
|
|
|
when 'due_date'
|
2014-02-08 12:26:49 +01:00
|
|
|
validate_not_nil(show_always, "Please select when to show the action")
|
|
|
|
|
validate_not_blank(show_from_delta, "Please fill in the number of days to show the todo before the due date") unless show_always
|
2014-02-08 11:51:09 +01:00
|
|
|
else
|
2014-02-10 19:39:39 +01:00
|
|
|
errors[:base] << "Unexpected value of recurrence target selector '#{target}'"
|
2014-02-08 11:51:09 +01:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def errors
|
|
|
|
|
@recurring_todo.errors
|
|
|
|
|
end
|
|
|
|
|
|
2014-02-09 21:48:52 +01:00
|
|
|
def get(attribute)
|
2014-02-10 19:39:39 +01:00
|
|
|
@attributes[attribute]
|
2014-02-08 11:51:09 +01:00
|
|
|
end
|
|
|
|
|
|
2014-01-27 16:42:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|