2005-01-09 11:59:57 +00:00
class TodoController < ApplicationController
2005-08-08 01:54:05 +00:00
2005-12-04 11:43:09 +00:00
model :user
model :project
model :context
2005-01-09 11:59:57 +00:00
helper :todo
2005-08-08 01:54:05 +00:00
2006-06-10 05:52:20 +00:00
prepend_before_filter :login_required
2005-01-09 11:59:57 +00:00
layout " standard "
2005-08-08 01:54:05 +00:00
2005-05-28 14:08:44 +00:00
# Main method for listing tasks
# Set page title, and fill variables with contexts and done and not-done tasks
# Number of completed actions to show is determined by a setting in settings.yml
2006-07-28 04:09:02 +00:00
def index
2005-08-08 01:54:05 +00:00
self . init
2006-07-28 06:55:35 +00:00
@projects = @user . projects . find ( :all , :include = > [ :todos ] )
@contexts = @user . contexts . find ( :all , :include = > [ :todos ] )
2005-06-11 12:24:19 +00:00
@page_title = " TRACKS::List tasks "
2006-04-11 12:40:10 +00:00
2006-04-11 17:19:30 +00:00
# If you've set no_completed to zero, the completed items box
# isn't shown on the home page
max_completed = @user . preferences [ " no_completed " ] . to_i - 1
2006-07-28 06:55:35 +00:00
@done = nil
if max_completed > 0
@done = Todo . find ( :all ,
:conditions = > [ 'todos.user_id = ? and todos.done = ?' , @user . id , true ] ,
:order = > 'todos.completed DESC' ,
:limit = > max_completed ,
:include = > [ :project , :context ] )
end
2006-07-17 03:40:35 +00:00
@contexts_to_show = @contexts . reject { | x | x . hide? }
2006-02-26 12:02:11 +00:00
if @contexts . empty?
flash [ 'warning' ] = 'You must add at least one context before adding next actions.'
end
2005-12-04 11:43:09 +00:00
2005-06-11 12:24:19 +00:00
# Set count badge to number of not-done, not hidden context items
2006-06-10 05:52:20 +00:00
@count = @todos . reject { | x | x . done? || x . context . hide? } . size
respond_to do | wants |
wants . html
wants . xml { render :action = > 'list.rxml' , :layout = > false }
end
2005-06-11 12:24:19 +00:00
end
2005-01-09 11:59:57 +00:00
2005-05-28 14:08:44 +00:00
# Called by a form button
# Parameters from form fields are passed to create new action
# in the selected context.
def add_item
2005-08-08 01:54:05 +00:00
self . init
2006-01-04 19:49:15 +00:00
@item = @user . todos . build
2006-05-13 13:32:39 +00:00
@item . attributes = params [ " todo " ]
2006-07-17 03:40:35 +00:00
2006-07-28 04:09:02 +00:00
if @item . due?
2006-07-28 19:03:34 +00:00
@item . due = parse_date_per_user_prefs ( params [ " todo " ] [ " due " ] )
2006-07-28 04:09:02 +00:00
else
@item . due = " "
end
Added the beginnings of a tickler to Tracks. It's fairly rudimentary at the moment, but it's designed to set the foundations for more kinds of deferred tasks.The current system works, but isn't very DRY: it will need refactoring for speed.
It has these features:
* The todos table and model has been altered (run rake migrate to update) to create two sub-classes of the todo model: Immediate and Deferred. Fairly obviously, Immediate actions are those shown immediately, and Deferred are those shown when certain conditions are fulfilled. At the moment, this is when the 'show_from' date arrives.
* Deferred actions are created on a separate page: /todo/tickler. You can view the show_from date here and delete or edit the actions. Deferred actions don't show on the home page (their handling on project and context pages is still to be fixed).
* A periodically called method (every 10 minutes) checks whether any of the deferred actions is due to be show, and if so, a warning message is shown on the home page to tell you how many deferred actions are to be shown. You need to refresh the page to see them (again, this is to be fixed).
* When deferred actions become due, their type is changed from "Deferred" to "Immediate". The handling of their staleness is still to be fixed.
There's a way to go before it's really smooth, but it's a start.
At least partially fixes #270 and #78, but will be improved with time too.
git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@232 a4c988fc-2ded-0310-b66e-134b36920a42
2006-05-02 17:11:46 +00:00
2006-07-28 04:09:02 +00:00
@saved = @item . save
respond_to do | wants |
wants . html { redirect_to :action = > " index " }
2006-07-28 06:55:35 +00:00
wants . js do
if @saved
init_todos
@up_count = @todos . reject { | x | x . done? or x . context . hide? } . size . to_s
end
render
end
2006-07-28 04:09:02 +00:00
wants . xml { render :xml = > @item . to_xml ( :root = > 'todo' , :except = > :user_id ) }
end
# if you're seeing the message 'An error occurred on the server.' and you want to debug, comment out the rescue section and check the Ajax response for an exception message
rescue
respond_to do | wants |
wants . html do
flash [ " warning " ] = 'An error occurred on the server.'
render :action = > " index "
2006-08-01 07:03:31 +00:00
end
2006-07-28 04:09:02 +00:00
wants . js { render :action = > 'error' }
wants . xml { render :text = > 'An error occurred on the server.' + $! }
end
Added the beginnings of a tickler to Tracks. It's fairly rudimentary at the moment, but it's designed to set the foundations for more kinds of deferred tasks.The current system works, but isn't very DRY: it will need refactoring for speed.
It has these features:
* The todos table and model has been altered (run rake migrate to update) to create two sub-classes of the todo model: Immediate and Deferred. Fairly obviously, Immediate actions are those shown immediately, and Deferred are those shown when certain conditions are fulfilled. At the moment, this is when the 'show_from' date arrives.
* Deferred actions are created on a separate page: /todo/tickler. You can view the show_from date here and delete or edit the actions. Deferred actions don't show on the home page (their handling on project and context pages is still to be fixed).
* A periodically called method (every 10 minutes) checks whether any of the deferred actions is due to be show, and if so, a warning message is shown on the home page to tell you how many deferred actions are to be shown. You need to refresh the page to see them (again, this is to be fixed).
* When deferred actions become due, their type is changed from "Deferred" to "Immediate". The handling of their staleness is still to be fixed.
There's a way to go before it's really smooth, but it's a start.
At least partially fixes #270 and #78, but will be improved with time too.
git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@232 a4c988fc-2ded-0310-b66e-134b36920a42
2006-05-02 17:11:46 +00:00
end
2006-07-17 03:40:35 +00:00
2006-08-01 07:03:31 +00:00
def edit
2005-12-04 11:43:09 +00:00
self . init
2006-07-17 03:40:35 +00:00
@item = check_user_return_item
2005-12-04 11:43:09 +00:00
end
2006-06-10 06:00:32 +00:00
def show
self . init
item = check_user_return_item
respond_to do | wants |
wants . xml { render :xml = > item . to_xml ( :root = > 'todo' , :except = > :user_id ) }
end
end
2005-12-04 11:43:09 +00:00
# Toggles the 'done' status of the action
#
def toggle_check
self . init
2006-01-10 06:15:48 +00:00
@item = check_user_return_item
@item . toggle! ( 'done' )
@item . completed = Time . now ( ) # For some reason, the before_save in todo.rb stopped working
@saved = @item . save
2006-07-18 05:09:54 +00:00
@remaining_undone_in_context = Todo . count ( :conditions = > [ 'user_id = ? and context_id = ? and type = ? and done = ?' , @user . id , @item . context_id , " Immediate " , false ] )
2006-01-10 06:15:48 +00:00
if @saved
@down_count = @todos . collect { | x | ( ! x . done? and ! x . context . hide? ) ? x : nil } . compact . size . to_s
end
return if request . xhr?
if @saved
flash [ 'notice' ] = " The action <strong>' #{ @item . description } '</strong> was marked as <strong> #{ @item . done? ? 'complete' : 'incomplete' } </strong> "
else
flash [ 'notice' ] = " The action <strong>' #{ @item . description } '</strong> was NOT marked as <strong> #{ @item . done? ? 'complete' : 'incomplete' } due to an error on the server.</strong> "
2005-12-04 11:43:09 +00:00
end
2006-07-28 04:09:02 +00:00
redirect_to :action = > " index "
2005-12-04 11:43:09 +00:00
end
2005-05-28 14:08:44 +00:00
# Edit the details of an action
2005-08-08 01:54:05 +00:00
#
2006-08-02 03:40:22 +00:00
def update
2005-08-08 01:54:05 +00:00
self . init
2006-04-11 12:40:10 +00:00
@item = check_user_return_item
@original_item_context_id = @item . context_id
2006-05-13 13:32:39 +00:00
@item . attributes = params [ " item " ]
2005-08-08 01:54:05 +00:00
2006-04-11 12:40:10 +00:00
if @item . due?
2006-07-28 19:03:34 +00:00
@item . due = parse_date_per_user_prefs ( params [ " item " ] [ " due " ] )
2005-05-28 14:08:44 +00:00
else
2006-04-11 12:40:10 +00:00
@item . due = " "
2005-05-28 14:08:44 +00:00
end
2005-08-08 01:54:05 +00:00
2006-04-11 12:40:10 +00:00
@saved = @item . save
2005-01-09 11:59:57 +00:00
end
2006-07-03 00:22:28 +00:00
def update_context
self . init
@item = check_user_return_item
context = Context . find ( params [ 'context_id' ] ) ;
if @user == context . user
@original_item_context_id = @item . context_id
@item . context_id = context . id
@item . context = context
@saved = @item . save
2006-08-02 03:40:22 +00:00
render :action = > 'update'
2006-07-03 00:22:28 +00:00
else
render :update do | page |
page . replace_html " info " , content_tag ( " div " , " Error updating the context of the dragged item. Item and context user mis-match: #{ @item . user . name } and #{ @context . user . name } ! - refresh the page to see them. " , " class " = > " warning " )
end
end
end
2006-07-03 00:59:31 +00:00
def update_project
self . init
@item = check_user_return_item
project = Project . find ( params [ 'project_id' ] ) ;
if @user == project . user
@original_item_context_id = @item . context_id
@item . project_id = project . id
@item . project = project
@saved = @item . save
2006-08-02 03:40:22 +00:00
render :action = > 'update'
2006-07-03 00:59:31 +00:00
else
render :update do | page |
page . replace_html " info " , content_tag ( " div " , " Error updating the project of the dragged item. Item and project user mis-match: #{ @item . user . name } and #{ @project . user . name } ! - refresh the page to see them. " , " class " = > " warning " )
end
end
end
Added the beginnings of a tickler to Tracks. It's fairly rudimentary at the moment, but it's designed to set the foundations for more kinds of deferred tasks.The current system works, but isn't very DRY: it will need refactoring for speed.
It has these features:
* The todos table and model has been altered (run rake migrate to update) to create two sub-classes of the todo model: Immediate and Deferred. Fairly obviously, Immediate actions are those shown immediately, and Deferred are those shown when certain conditions are fulfilled. At the moment, this is when the 'show_from' date arrives.
* Deferred actions are created on a separate page: /todo/tickler. You can view the show_from date here and delete or edit the actions. Deferred actions don't show on the home page (their handling on project and context pages is still to be fixed).
* A periodically called method (every 10 minutes) checks whether any of the deferred actions is due to be show, and if so, a warning message is shown on the home page to tell you how many deferred actions are to be shown. You need to refresh the page to see them (again, this is to be fixed).
* When deferred actions become due, their type is changed from "Deferred" to "Immediate". The handling of their staleness is still to be fixed.
There's a way to go before it's really smooth, but it's a start.
At least partially fixes #270 and #78, but will be improved with time too.
git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@232 a4c988fc-2ded-0310-b66e-134b36920a42
2006-05-02 17:11:46 +00:00
2006-08-01 07:03:31 +00:00
def destroy
2006-01-04 19:49:15 +00:00
@item = check_user_return_item
2006-07-18 05:09:54 +00:00
context_id = @item . context_id
2006-08-01 07:03:31 +00:00
project_id = @item . project_id
2006-01-04 19:49:15 +00:00
@saved = @item . destroy
2006-08-01 07:03:31 +00:00
respond_to do | wants |
wants . html do
if @saved
flash [ " notice " ] = 'Successfully deleted next action'
redirect_to :action = > 'index'
else
flash [ " warning " ] = 'Failed to delete the action.'
redirect_to :action = > 'index'
end
end
wants . js do
if @saved
@down_count = 0
source_view do | from |
from . todo do
@down_count = Todo . count ( :conditions = > [ 'todos.user_id = ? and todos.type = ? and todos.done = ? and contexts.hide = ?' ,
@user . id , " Immediate " , false , false ] ,
:include = > [ :context ] )
@remaining_undone_in_context = Todo . count ( :conditions = > [ 'user_id = ? and context_id = ? and type = ? and done = ?' ,
@user . id , context_id , " Immediate " , false ] )
end
from . context do
@down_count = Todo . count ( :conditions = > [ 'todos.user_id = ? and todos.type = ? and todos.done = ? and todos.context_id = ?' ,
@user . id , " Immediate " , false , context_id ] )
end
from . project do
@down_count = Todo . count ( :conditions = > [ 'todos.user_id = ? and todos.type = ? and todos.done = ? and todos.project_id = ?' ,
@user . id , " Immediate " , false , project_id ] ) unless project_id == nil
end
end
end
render
end
wants . xml { render :text = > '200 OK. Action deleted.' , :status = > 200 }
2006-01-04 19:49:15 +00:00
2005-06-11 12:24:19 +00:00
end
2006-01-04 19:49:15 +00:00
rescue
2006-08-01 07:03:31 +00:00
respond_to do | wants |
wants . html do
flash [ " warning " ] = 'An error occurred on the server.'
redirect_to :action = > 'index'
end
wants . js { render :action = > 'error' }
wants . xml { render :text = > 'An error occurred on the server.' + $! }
end
2005-05-28 14:08:44 +00:00
end
2005-08-08 01:54:05 +00:00
2005-12-04 11:43:09 +00:00
def completed
@page_title = " TRACKS::Completed tasks "
2006-08-03 01:58:49 +00:00
@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
2005-08-08 01:54:05 +00:00
end
2005-12-04 11:43:09 +00:00
def completed_archive
@page_title = " TRACKS::Archived completed tasks "
2006-08-03 01:58:49 +00:00
@done = Todo . find_completed ( @user . id )
@done_archive = @done . completed_more_than 28 . day . ago
2005-12-04 11:43:09 +00:00
end
2006-02-26 11:36:25 +00:00
2005-08-08 01:54:05 +00:00
protected
def check_user_return_item
2006-05-13 13:32:39 +00:00
item = Todo . find ( params [ 'id' ] )
2006-04-08 17:46:41 +00:00
if @user == item . user
2005-08-08 01:54:05 +00:00
return item
else
2006-08-01 07:03:31 +00:00
@error_message = 'Item and session user mis-match: #{item.user.name} and #{@user.name}!'
respond_to do | wants |
wants . html do
flash [ " warning " ] = @error_message
render :action = > " index "
end
wants . js { render :action = > 'error' }
wants . xml { render :text = > @error_message , :status = > 403 }
end
2005-08-08 01:54:05 +00:00
end
end
def init
2006-08-01 07:03:31 +00:00
@source_view = params [ '_source_view' ] || 'todo'
2005-08-08 01:54:05 +00:00
@projects = @user . projects
@contexts = @user . contexts
2006-07-17 03:40:35 +00:00
init_todos
2006-07-28 06:55:35 +00:00
init_not_done_counts
2006-07-17 03:40:35 +00:00
end
def init_todos
2006-07-28 06:55:35 +00:00
@todos = Todo . find ( :all ,
:conditions = > [ 'todos.user_id = ? and todos.type = ?' , @user . id , " Immediate " ] ,
:include = > [ :project , :context ] )
@not_done_todos = Todo . find ( :all ,
:conditions = > [ 'todos.user_id = ? and todos.type = ? and todos.done = ?' , @user . id , " Immediate " , false ] ,
:order = > " todos.due IS NULL, todos.due ASC, todos.created_at ASC " ,
:include = > [ :project , :context ] )
2006-07-28 04:09:02 +00:00
end
2006-07-28 06:55:35 +00:00
2005-01-09 11:59:57 +00:00
end