Some cool refactoring of the completed and completed_archive actions.

git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@303 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
lukemelia 2006-08-03 01:58:49 +00:00
parent f9a2a6460d
commit c290d7a36a
4 changed files with 28 additions and 23 deletions

View file

@ -230,33 +230,18 @@ class TodoController < ApplicationController
end
end
# List the completed tasks, sorted by completion date
# @done_today: in the last 24 hours
# @done_this_week: in the last week
# @done_this_month: in the last 4 weeks (<=28 days)
def completed
self.init
@page_title = "TRACKS::Completed tasks"
@done = Todo.find(:all,
:conditions => ['todos.user_id = ? and todos.done = ? and todos.completed is not null', @user.id, true],
:order => 'todos.completed DESC',
:include => [ :project, :context ])
@done_today = @done.collect { |x| x.completed >= 1.day.ago ? x:nil }.compact
@done_this_week = @done.collect { |x| 1.week.ago <= x.completed ? x:nil }.compact
@done_this_month = @done.collect { |x| 4.week.ago <= x.completed ? x:nil }.compact
@done = Todo.find_completed(@user.id)
@done_today = @done.completed_within 1.day.ago
@done_this_week = @done.completed_within 1.week.ago
@done_this_month = @done.completed_within 4.week.ago
end
# Archived completed items, older than 28 days
#
def completed_archive
self.init
@page_title = "TRACKS::Archived completed tasks"
@done = Todo.find(:all,
:conditions => ['todos.user_id = ? and todos.done = ? and todos.completed is not null', @user.id, true],
:order => 'todos.completed DESC',
:include => [ :project, :context ])
@done_archive = @done.collect { |x| 28.day.ago > x.completed ? x:nil }.compact
@done = Todo.find_completed(@user.id)
@done_archive = @done.completed_more_than 28.day.ago
end
protected

View file

@ -15,5 +15,25 @@ class Todo < ActiveRecord::Base
def self.not_done( id=id )
self.find(:all, :conditions =>[ "done = ? AND context_id = ?", false, id], :order =>"due IS NULL, due ASC, created_at ASC")
end
def self.find_completed(user_id)
done = self.find(:all,
:conditions => ['todos.user_id = ? and todos.done = ? and todos.completed is not null', user_id, true],
:order => 'todos.completed DESC',
:include => [ :project, :context ])
done.extend(CompletedToDosByDate)
end
end
module CompletedToDosByDate
def completed_within( date )
self.reject { |x| x.completed < date }
end
def completed_more_than( date )
self.reject { |x| x.completed > date }
end
end

View file

@ -1,5 +1,5 @@
<div id="display_box_projects">
<p>You have completed <%= @done_today.length %> actions so far today.</p>
<p>You have completed <%= pluralize @done_today.length, 'action' %> so far today.</p>
<div class="container">
<h2>Completed in the last 24 hours</h2>
<table class="next_actions" cellspacing="5" cellpadding="0" border="0">

View file

@ -1,6 +1,6 @@
<div id="display_box_projects">
<p>There are <%= @done_archive.length %> completed actions in the archive.</p>
<p>There <%= @done_archive.length == 1 ? 'is' : 'are' %> <%= pluralize @done_archive.length, 'completed action' %> in the archive.</p>
<div class="container">
<h2>Completed more than 31 days ago</h2>
<table class="next_actions" cellspacing="5" cellpadding="0" border="0">