From 4d2f25ab20c708b4466aa0ff01c21fdc90dfc90d Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Tue, 27 Nov 2012 23:04:07 -0600 Subject: [PATCH] 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. --- app/controllers/application_controller.rb | 20 --------------- app/controllers/contexts_controller.rb | 6 +---- app/controllers/projects_controller.rb | 6 +---- app/controllers/todos_controller.rb | 31 ++++++++++++++++++----- lib/tracks/done_todos.rb | 23 +++++++++++++++++ 5 files changed, 50 insertions(+), 36 deletions(-) create mode 100644 lib/tracks/done_todos.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6ef7d4d8..eb246331 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 ) diff --git a/app/controllers/contexts_controller.rb b/app/controllers/contexts_controller.rb index bfdf62ae..e8f87a86 100644 --- a/app/controllers/contexts_controller.rb +++ b/app/controllers/contexts_controller.rb @@ -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' diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 2df3bb5e..3d1a7b1e 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -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' diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index d6ac9f1d..04a07709 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -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 diff --git a/lib/tracks/done_todos.rb b/lib/tracks/done_todos.rb new file mode 100644 index 00000000..9649276d --- /dev/null +++ b/lib/tracks/done_todos.rb @@ -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