Extract creation of todos from recurrings todos

This commit is contained in:
Matt Rogers & Katrina Owen 2013-04-25 13:50:03 -05:00
parent 0607096e7c
commit a1b270699d
5 changed files with 61 additions and 35 deletions

View file

@ -148,38 +148,6 @@ class ApplicationController < ActionController::Base
end
end
def create_todo_from_recurring_todo(rt, date=nil)
# create todo and initialize with data from recurring_todo rt
todo = current_user.todos.build( { :description => rt.description, :notes => rt.notes, :project_id => rt.project_id, :context_id => rt.context_id})
todo.recurring_todo_id = rt.id
# set dates
todo.due = rt.get_due_date(date)
show_from_date = rt.get_show_from_date(date)
if show_from_date.nil?
todo.show_from=nil
else
# make sure that show_from is not in the past
todo.show_from = show_from_date < Time.zone.now ? nil : show_from_date
end
saved = todo.save
if saved
todo.tag_with(rt.tag_list)
todo.tags.reload
end
# increate number of occurences created from recurring todo
rt.inc_occurences
# mark recurring todo complete if there are no next actions left
checkdate = todo.due.nil? ? todo.show_from : todo.due
rt.toggle_completion! unless rt.has_next_todo(checkdate)
return saved ? todo : nil
end
def handle_unverified_request
unless request.format=="application/xml"
super # handle xml http auth via our own login code

View file

@ -124,7 +124,7 @@ class RecurringTodosController < ApplicationController
if @saved
@status_message = t('todos.recurring_action_saved')
@todo_saved = create_todo_from_recurring_todo(@recurring_todo).nil? == false
@todo_saved = TodoFromRecurringTodo.new(current_user, @recurring_todo).create.nil? == false
if @todo_saved
@status_message += " / " + t('todos.new_related_todo_created_short')
else
@ -189,7 +189,7 @@ class RecurringTodosController < ApplicationController
@active_todos = @recurring_todo.todos.active.count
# create todo if there is no active todo belonging to the activated
# recurring_todo
@new_recurring_todo = create_todo_from_recurring_todo(@recurring_todo) if @active_todos == 0
@new_recurring_todo = TodoFromRecurringTodo.new(current_user, @recurring_todo).create if @active_todos == 0
end
respond_to do |format|

View file

@ -1153,7 +1153,7 @@ class TodosController < ApplicationController
# for tomorrow.
date = date_to_check.at_midnight >= Time.zone.now.at_midnight ? date_to_check : Time.zone.now-1.day
new_recurring_todo = create_todo_from_recurring_todo(recurring_todo, date.at_midnight)
new_recurring_todo = TodoFromRecurringTodo.new(current_user, recurring_todo).create(date.at_midnight)
end
end
end

View file

@ -672,6 +672,10 @@ class RecurringTodo < ActiveRecord::Base
end
end
def done?(end_date)
!has_next_todo(end_date)
end
def toggle_completion!
return completed? ? activate! : complete!
end

View file

@ -0,0 +1,54 @@
class TodoFromRecurringTodo
attr_reader :user, :recurring_todo, :todo
def initialize(user, recurring_todo)
@user = user
@recurring_todo = recurring_todo
end
def create(time = nil)
@todo = build_todo(time)
save_todo
update_recurring_todo
return todo.persisted? ? todo : nil
end
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
def update_recurring_todo
recurring_todo.inc_occurences
recurring_todo.toggle_completion! if recurring_todo.done?(end_date)
end
def end_date
todo.due ? todo.due : todo.show_from
end
def attributes
{
:description => recurring_todo.description,
:notes => recurring_todo.notes,
:project_id => recurring_todo.project_id,
:context_id => recurring_todo.context_id
}
end
def save_todo
if todo.save
todo.tag_with(recurring_todo.tag_list)
todo.tags.reload
end
end
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
end