Class: RecurringTodos::WeeklyRecurrencePattern

Inherits:
AbstractRecurrencePattern show all
Defined in:
app/models/recurring_todos/weekly_recurrence_pattern.rb

Instance Attribute Summary

Attributes inherited from AbstractRecurrencePattern

#attributes

Instance Method Summary collapse

Methods inherited from AbstractRecurrencePattern

#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_occurrences, #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) ⇒ WeeklyRecurrencePattern

Returns a new instance of WeeklyRecurrencePattern.



3
4
5
# File 'app/models/recurring_todos/weekly_recurrence_pattern.rb', line 3

def initialize(user)
  super user
end

Instance Method Details

#determine_start_date(previous) ⇒ Object (private)



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

def determine_start_date(previous)
  if previous.nil?
    return 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 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 = start_from if start_from > previous
    end
    return start
  end
end

#every_x_weekObject



7
8
9
# File 'app/models/recurring_todos/weekly_recurrence_pattern.rb', line 7

def every_x_week
  get :every_other1
end

#find_first_day_in_this_week(start) ⇒ Object (private)



73
74
75
76
77
78
79
# File 'app/models/recurring_todos/weekly_recurrence_pattern.rb', line 73

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)


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

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 + 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 (#{every_day})"
end

#on_xday(n) ⇒ Object



17
18
19
# File 'app/models/recurring_todos/weekly_recurrence_pattern.rb', line 17

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

#recurrence_patternObject



21
22
23
24
25
26
27
# File 'app/models/recurring_todos/weekly_recurrence_pattern.rb', line 21

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



29
30
31
32
33
34
# File 'app/models/recurring_todos/weekly_recurrence_pattern.rb', line 29

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 || send("on_#{day}") }
  errors.add(:base, "You must specify at least one day on which the todo recurs") unless something_set
end