diff --git a/app/models/recurring_todos/abstract_repeat_pattern.rb b/app/models/recurring_todos/abstract_repeat_pattern.rb index 5c7ac546..c063ea45 100644 --- a/app/models/recurring_todos/abstract_repeat_pattern.rb +++ b/app/models/recurring_todos/abstract_repeat_pattern.rb @@ -46,18 +46,26 @@ module RecurringTodos @attributes = recurring_todo.attributes end + 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 + def validate starts_and_ends_on_validations set_recurrence_on_validations end def starts_and_ends_on_validations - errors[:base] << "The start date needs to be filled in" if start_from.blank? + validate_not_blank(start_from, "The start date needs to be filled in") case ends_on when 'ends_on_number_of_times' - errors[:base] << "The number of recurrences needs to be filled in for 'Ends on'" if number_of_occurences.blank? + validate_not_blank(number_of_occurences, "The number of recurrences needs to be filled in for 'Ends on'") when "ends_on_end_date" - errors[:base] << "The end date needs to be filled in for 'Ends on'" if end_date.blank? + validate_not_blank(end_date, "The end date needs to be filled in for 'Ends on'") else errors[:base] << "The end of the recurrence is not selected" unless ends_on == "no_end_date" end @@ -69,10 +77,8 @@ module RecurringTodos when 'show_from_date' # no validations when 'due_date' - errors[:base] << "Please select when to show the action" if show_always.nil? - unless show_always - errors[:base] << "Please fill in the number of days to show the todo before the due date" if show_from_delta.blank? - end + 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 else raise Exception.new, "unexpected value of recurrence target selector '#{target}'" end diff --git a/app/models/recurring_todos/monthly_repeat_pattern.rb b/app/models/recurring_todos/monthly_repeat_pattern.rb index 703849a8..3561ba60 100644 --- a/app/models/recurring_todos/monthly_repeat_pattern.rb +++ b/app/models/recurring_todos/monthly_repeat_pattern.rb @@ -40,12 +40,13 @@ module RecurringTodos def validate super + case recurrence_selector when 0 # 'monthly_every_x_day' - errors[:base] << "Every other nth month may not be empty for recurrence setting" if every_x_month.blank? + validate_not_blank(every_x_month, "Every other nth month may not be empty for recurrence setting") when 1 # 'every_xth_day' - errors[:base] <<"Every other nth month may not be empty for recurrence setting" if every_x_month2.blank? - errors[:base] <<"The day of the month may not be empty for recurrence setting" if day_of_week.blank? + validate_not_blank(every_x_month2, "Every other nth month may not be empty for recurrence setting") + validate_not_blank(day_of_week, "The day of the month may not be empty for recurrence setting") else raise Exception.new, "unexpected value of recurrence selector '#{recurrence_selector}'" end diff --git a/app/models/recurring_todos/weekly_repeat_pattern.rb b/app/models/recurring_todos/weekly_repeat_pattern.rb index dbe5e65d..721907a8 100644 --- a/app/models/recurring_todos/weekly_repeat_pattern.rb +++ b/app/models/recurring_todos/weekly_repeat_pattern.rb @@ -22,7 +22,7 @@ module RecurringTodos def validate super - errors[:base] << "Every other nth week may not be empty for weekly recurrence setting" if every_x_week.blank? + validate_not_blank(every_x_week, "Every other nth week may not be empty for weekly recurrence setting") something_set = %w{sunday monday tuesday wednesday thursday friday saturday}.inject(false) { |set, day| set || self.send("on_#{day}") } errors[:base] << "You must specify at least one day on which the todo recurs" unless something_set end diff --git a/app/models/recurring_todos/yearly_repeat_pattern.rb b/app/models/recurring_todos/yearly_repeat_pattern.rb index 27a34ce0..948b4dca 100644 --- a/app/models/recurring_todos/yearly_repeat_pattern.rb +++ b/app/models/recurring_todos/yearly_repeat_pattern.rb @@ -36,12 +36,12 @@ module RecurringTodos super case recurrence_selector when 0 # 'yearly_every_x_day' - errors[:base] << "The month of the year may not be empty for recurrence setting" if month_of_year.blank? - errors[:base] << "The day of the month may not be empty for recurrence setting" if every_x_day.blank? + validate_not_blank(month_of_year, "The month of the year may not be empty for recurrence setting") + validate_not_blank(every_x_day, "The day of the month may not be empty for recurrence setting") when 1 # 'yearly_every_xth_day' - errors[:base] << "The month of the year may not be empty for recurrence setting" if month_of_year2.blank? - errors[:base] << "The nth day of the month may not be empty for recurrence setting" if every_xth_day.blank? - errors[:base] << "The day of the week may not be empty for recurrence setting" if day_of_week.blank? + validate_not_blank(month_of_year2, "The month of the year may not be empty for recurrence setting") + validate_not_blank(every_xth_day, "The nth day of the month may not be empty for recurrence setting") + validate_not_blank(day_of_week, "The day of the week may not be empty for recurrence setting") else raise "unexpected value of recurrence selector '#{recurrence_selector}'" end