Class: RecurringTodos::WeeklyRepeatPattern

Inherits:
AbstractRepeatPattern show all
Defined in:
app/models/recurring_todos/weekly_repeat_pattern.rb

Instance Attribute Summary

Attributes inherited from AbstractRepeatPattern

#attributes

Instance Method Summary collapse

Methods inherited from AbstractRepeatPattern

#build_from_recurring_todo, #build_recurring_todo, #continues_recurring?, #day_of_week_as_text, #determine_start, #end_date, #ends_on, #errors, #find_last_day_x_of_month, #find_xth_day_of_month, #get, #get_due_date, #get_show_from_date, #get_xth_day_of_month, #month_of_year_as_text, #number_of_occurences, #put_in_tickler?, #recurring_target_as_text, #set_recurrence_on_validations, #show_always?, #show_from_delta, #start_from, #starts_and_ends_on_validations, #target, #update_recurring_todo, #valid?, #validate_not_blank, #validate_not_nil, #xth

Constructor Details

#initialize(user) ⇒ WeeklyRepeatPattern


5
6
7
# File 'app/models/recurring_todos/weekly_repeat_pattern.rb', line 5

def initialize(user)
  super user
end

Instance Method Details

#determine_start_date(previous) ⇒ Object (private)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/recurring_todos/weekly_repeat_pattern.rb', line 56

def determine_start_date(previous)
  if previous.nil?
    return self.start_from || Time.zone.now
  else
    start = previous + 1.day
    if start.wday() == 0
      # we went to a new week, go to the nth next week and find first match
      # that week. Note that we already went into the next week, so -1
      start += (every_x_week-1).week
    end
    unless self.start_from.nil?
      # check if the start_from date is later than previous. If so, use
      # start_from as start to search for next date
      start = self.start_from if self.start_from > previous
    end
    return start
  end
end

#every_x_weekObject


9
10
11
# File 'app/models/recurring_todos/weekly_repeat_pattern.rb', line 9

def every_x_week
  get :every_other1
end

#find_first_day_in_this_week(start) ⇒ Object (private)


75
76
77
78
79
80
81
# File 'app/models/recurring_todos/weekly_repeat_pattern.rb', line 75

def find_first_day_in_this_week(start)
  # check if there are any days left this week for the next todo
  start.wday().upto 6 do |i|
    return start + (i-start.wday()).days if on_xday(i)
  end
  -1
end

#get_next_date(previous) ⇒ Object

Raises:

  • (Exception.new)

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/recurring_todos/weekly_repeat_pattern.rb', line 38

def get_next_date(previous)
  start = determine_start_date(previous)

  day = find_first_day_in_this_week(start)
  return day unless day == -1

  # we did not find anything this week, so check the nth next, starting from
  # sunday
  start = start + self.every_x_week.week - (start.wday()).days

  start = find_first_day_in_this_week(start)
  return start unless start == -1

  raise Exception.new, "unable to find next weekly date (#{self.every_day})"
end

#on_xday(n) ⇒ Object


19
20
21
# File 'app/models/recurring_todos/weekly_repeat_pattern.rb', line 19

def on_xday(n)
  get(:every_day) && get(:every_day)[n, 1] != ' '
end

#recurrence_patternObject


23
24
25
26
27
28
29
# File 'app/models/recurring_todos/weekly_repeat_pattern.rb', line 23

def recurrence_pattern
  if every_x_week > 1
    I18n.t("todos.recurrence.pattern.every_n", :n => every_x_week) + " " + I18n.t("common.weeks")
  else
    I18n.t('todos.recurrence.pattern.weekly')
  end
end

#validateObject


31
32
33
34
35
36
# File 'app/models/recurring_todos/weekly_repeat_pattern.rb', line 31

def validate
  super
  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