Refactor the fetching of done todos

Move the get_done_today, get_done_this_week, and get_done_this month
methods into their own class in lib/tracks and use the new class in the
context, project, and todo controllers.

This removes the complexity from the application controller, silos it
off, and slightly reduces the complexity of the other controllers so
that they don't have to duplicate as much code.

The tradeoff here is that the code that was moved out into its own class
was also duplicated in the todos controller due to a different use case
that I didn't see before. This is still an improvement however and so
I'm ok with going back and tackling the extra complexity added to
TodoController at a later date.
This commit is contained in:
Matt Rogers 2012-11-27 23:04:07 -06:00
parent 2393d6d371
commit 4d2f25ab20
5 changed files with 50 additions and 36 deletions

View file

@ -238,26 +238,6 @@ class ApplicationController < ActionController::Base
self.class.prefered_auth?
end
# all completed todos [today@00:00, today@now]
def get_done_today(completed_todos, includes = {:include => Todo::DEFAULT_INCLUDES})
start_of_this_day = Time.zone.now.beginning_of_day
completed_todos.completed_after(start_of_this_day).all(includes)
end
# all completed todos [begin_of_week, start_of_today]
def get_done_this_week(completed_todos, includes = {:include => Todo::DEFAULT_INCLUDES})
start_of_this_week = Time.zone.now.beginning_of_week
start_of_this_day = Time.zone.now.beginning_of_day
completed_todos.completed_before(start_of_this_day).completed_after(start_of_this_week).all(includes)
end
# all completed todos [begin_of_month, begin_of_week]
def get_done_this_month(completed_todos, includes = {:include => Todo::DEFAULT_INCLUDES})
start_of_this_month = Time.zone.now.beginning_of_month
start_of_this_week = Time.zone.now.beginning_of_week
completed_todos.completed_before(start_of_this_week).completed_after(start_of_this_month).all(includes)
end
private
def parse_date_per_user_prefs( s )

View file

@ -175,11 +175,7 @@ class ContextsController < ApplicationController
@context = current_user.contexts.find(params[:id])
@page_title = t('contexts.completed_tasks_title', :context_name => @context.name)
completed_todos = @context.todos.completed
@done_today = get_done_today(completed_todos)
@done_this_week = get_done_this_week(completed_todos)
@done_this_month = get_done_this_month(completed_todos)
@done_today, @done_this_week, @done_this_month = DoneTodos.done_todos_for_container(@context)
@count = @done_today.size + @done_this_week.size + @done_this_month.size
render :template => 'todos/done'

View file

@ -306,11 +306,7 @@ class ProjectsController < ApplicationController
@project = current_user.projects.find(params[:id])
@page_title = t('projects.completed_tasks_title', :project_name => @project.name)
completed_todos = @project.todos.completed
@done_today = get_done_today(completed_todos)
@done_this_week = get_done_this_week(completed_todos)
@done_this_month = get_done_this_month(completed_todos)
@done_today, @done_this_week, @done_this_month = DoneTodos.done_todos_for_container(@project)
@count = @done_today.size + @done_this_week.size + @done_this_month.size
render :template => 'todos/done'

View file

@ -588,16 +588,15 @@ class TodosController < ApplicationController
@source_view = 'done'
@page_title = t('todos.completed_tasks_title')
completed_todos = current_user.todos.completed
@done_today = get_done_today(completed_todos)
@done_this_week = get_done_this_week(completed_todos)
@done_this_month = get_done_this_month(completed_todos)
@done_today, @done_this_week, @done_this_month = DoneTodos.done_todos_for_container(current_user)
@count = @done_today.size + @done_this_week.size + @done_this_month.size
respond_to do |format|
format.html
format.xml { render :xml => completed_todos.to_xml( *to_xml_params ) }
format.xml do
completed_todos = current_user.todos.completed
render :xml => completed_todos.to_xml( *to_xml_params )
end
end
end
@ -1355,6 +1354,26 @@ class TodosController < ApplicationController
return !( all_list_uniq_ids.length == all_list_count )
end
# all completed todos [today@00:00, today@now]
def get_done_today(completed_todos, includes = {:include => Todo::DEFAULT_INCLUDES})
start_of_this_day = Time.zone.now.beginning_of_day
completed_todos.completed_after(start_of_this_day).all(includes)
end
# all completed todos [begin_of_week, start_of_today]
def get_done_this_week(completed_todos, includes = {:include => Todo::DEFAULT_INCLUDES})
start_of_this_week = Time.zone.now.beginning_of_week
start_of_this_day = Time.zone.now.beginning_of_day
completed_todos.completed_before(start_of_this_day).completed_after(start_of_this_week).all(includes)
end
# all completed todos [begin_of_month, begin_of_week]
def get_done_this_month(completed_todos, includes = {:include => Todo::DEFAULT_INCLUDES})
start_of_this_month = Time.zone.now.beginning_of_month
start_of_this_week = Time.zone.now.beginning_of_week
completed_todos.completed_before(start_of_this_week).completed_after(start_of_this_month).all(includes)
end
class TodoCreateParamsHelper

23
lib/tracks/done_todos.rb Normal file
View file

@ -0,0 +1,23 @@
class DoneTodos
def self.done_todos_for_container(container)
completed_todos = container.todos.completed
return done_today(completed_todos), done_this_week(completed_todos), done_this_month(completed_todos)
end
def self.done_today(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
start_of_this_day = Time.zone.now.beginning_of_day
todos.completed_after(start_of_this_day).all(includes)
end
def self.done_this_week(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
start_of_this_week = Time.zone.now.beginning_of_week
start_of_this_day = Time.zone.now.beginning_of_day
todos.completed_before(start_of_this_day).completed_after(start_of_this_week).all(includes)
end
def self.done_this_month(todos, includes = {:include => Todo::DEFAULT_INCLUDES})
start_of_this_month = Time.zone.now.beginning_of_month
start_of_this_week = Time.zone.now.beginning_of_week
todos.completed_before(start_of_this_week).completed_after(start_of_this_month).all(includes)
end
end