make validations a bit more dry

This commit is contained in:
Reinier Balt 2014-02-08 12:26:49 +01:00
parent 29b815e998
commit bad91e8d10
4 changed files with 23 additions and 16 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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