Class: TodoFromRecurringTodo

Inherits:
Object
  • Object
show all
Defined in:
lib/todo_from_recurring_todo.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, recurring_todo) ⇒ TodoFromRecurringTodo

Returns a new instance of TodoFromRecurringTodo



4
5
6
7
# File 'lib/todo_from_recurring_todo.rb', line 4

def initialize(user, recurring_todo)
  @user = user
  @recurring_todo = recurring_todo
end

Instance Attribute Details

#recurring_todoObject (readonly)

Returns the value of attribute recurring_todo



2
3
4
# File 'lib/todo_from_recurring_todo.rb', line 2

def recurring_todo
  @recurring_todo
end

#todoObject (readonly)

Returns the value of attribute todo



2
3
4
# File 'lib/todo_from_recurring_todo.rb', line 2

def todo
  @todo
end

#userObject (readonly)

Returns the value of attribute user



2
3
4
# File 'lib/todo_from_recurring_todo.rb', line 2

def user
  @user
end

Instance Method Details

#attributesObject



33
34
35
36
37
38
39
40
# File 'lib/todo_from_recurring_todo.rb', line 33

def attributes
  {
    :description => recurring_todo.description,
    :notes       => recurring_todo.notes,
    :project_id  => recurring_todo.project_id,
    :context_id  => recurring_todo.context_id
  }
end

#build_todo(time) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/todo_from_recurring_todo.rb', line 16

def build_todo(time)
  user.todos.build(attributes).tap do |todo|
    todo.recurring_todo_id = recurring_todo.id
    todo.due = recurring_todo.get_due_date(time)
    todo.show_from = show_from_date(time)
  end
end

#create(time = nil) ⇒ Object



9
10
11
12
13
14
# File 'lib/todo_from_recurring_todo.rb', line 9

def create(time = nil)
  @todo = build_todo(time)
  save_todo
  update_recurring_todo
  return todo.persisted? ? todo : nil
end

#end_dateObject



29
30
31
# File 'lib/todo_from_recurring_todo.rb', line 29

def end_date
  todo.due ? todo.due : todo.show_from
end

#save_todoObject



42
43
44
45
46
47
# File 'lib/todo_from_recurring_todo.rb', line 42

def save_todo
  if todo.save
    todo.tag_with(recurring_todo.tag_list)
    todo.tags.reload
  end
end

#show_from_date(time) ⇒ Object



49
50
51
52
53
54
# File 'lib/todo_from_recurring_todo.rb', line 49

def show_from_date(time)
  show_from_date = recurring_todo.get_show_from_date(time)
  if show_from_date && show_from_date >= Time.zone.now
    show_from_date
  end
end

#update_recurring_todoObject



24
25
26
27
# File 'lib/todo_from_recurring_todo.rb', line 24

def update_recurring_todo
  recurring_todo.increment_occurrences
  recurring_todo.toggle_completion! if recurring_todo.done?(end_date)
end