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
2005-06-11 12:24:19 +00:00
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
def index
list
render_action " list "
end
2005-03-27 17:45:49 +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
#
2005-06-11 12:24:19 +00:00
def list
2005-08-08 01:54:05 +00:00
self . init
2005-12-07 19:06:24 +00:00
@on_page = " home "
2005-06-11 12:24:19 +00:00
@page_title = " TRACKS::List tasks "
2006-02-12 15:18:21 +00:00
@done = @done [ 0 .. ( @user . preferences [ " no_completed " ] . to_i - 1 ) ]
2005-08-08 01:54:05 +00:00
2005-12-04 11:43:09 +00:00
@contexts_to_show = @contexts . clone
2006-01-10 06:15:48 +00:00
@contexts_to_show = @contexts_to_show . collect { | x | ( ! x . hide? and ! x . find_not_done_todos . empty? ) ? x : nil } . compact
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-01-10 06:15:48 +00:00
@count = @todos . collect { | x | ( ! x . done? and ! x . context . hide? ) ? x : nil } . compact . size
2005-06-11 12:24:19 +00:00
end
2005-01-09 11:59:57 +00:00
2006-01-04 19:49:15 +00:00
def update_element
end
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
@item . attributes = @params [ " todo " ]
2005-08-08 01:54:05 +00:00
2006-01-04 19:49:15 +00:00
if @item . due?
2006-02-12 15:18:21 +00:00
@item . due = Date . strptime ( @params [ " todo " ] [ " due " ] , @user . preferences [ " date_format " ] )
2005-06-11 12:24:19 +00:00
else
2006-01-04 19:49:15 +00:00
@item . due = " "
2005-06-11 12:24:19 +00:00
end
2006-01-04 19:49:15 +00:00
@saved = @item . save
@on_page = " home "
2006-01-10 06:15:48 +00:00
if @saved
@up_count = @todos . collect { | x | ( ! x . done? and ! x . context . hide? ) ? x : nil } . compact . size . to_s
end
2006-01-04 19:49:15 +00:00
return if request . xhr?
# fallback for standard requests
if @saved
2006-01-08 13:21:24 +00:00
flash [ " notice " ] = 'Added new next action.'
2006-01-04 19:49:15 +00:00
redirect_to :action = > 'list'
2005-08-08 01:54:05 +00:00
else
2006-01-08 13:21:24 +00:00
flash [ " warning " ] = 'The next action was not added. Please try again.'
redirect_to :action = > 'list'
2005-08-08 01:54:05 +00:00
end
2006-01-04 19:49:15 +00:00
rescue
if request . xhr? # be sure to include an error.rjs
render :action = > 'error'
else
flash [ " warning " ] = 'An error occurred on the server.'
render :action = > 'list'
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 edit_action
self . init
item = check_user_return_item
2006-01-10 06:15:48 +00:00
2005-12-04 11:43:09 +00:00
render :partial = > 'action_edit_form' , :object = > item
end
# 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
@on_page = " home "
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-01-10 06:15:48 +00:00
redirect_to :action = > " list "
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
#
2005-05-28 14:08:44 +00:00
def update_action
2005-08-08 01:54:05 +00:00
self . init
2005-12-07 19:06:24 +00:00
if @params [ " on_project_page " ] == true
@on_page = " project "
end
2005-08-08 01:54:05 +00:00
item = check_user_return_item
item . attributes = @params [ " item " ]
if item . due?
2006-02-12 15:18:21 +00:00
item . due = Date . strptime ( @params [ " item " ] [ " due " ] , @user . preferences [ " date_format " ] )
2005-05-28 14:08:44 +00:00
else
2005-08-08 01:54:05 +00:00
item . due = " "
2005-05-28 14:08:44 +00:00
end
2005-08-08 01:54:05 +00:00
if item . save
2005-12-07 19:06:24 +00:00
render :partial = > 'item' , :object = > item
2005-01-09 11:59:57 +00:00
else
2005-05-28 14:08:44 +00:00
flash [ " warning " ] = " Couldn't update the action "
render_text " "
2005-01-09 11:59:57 +00:00
end
end
2005-08-08 01:54:05 +00:00
2006-01-04 19:49:15 +00:00
# Delete a next action
2005-05-28 14:08:44 +00:00
#
def destroy_action
2006-01-04 19:49:15 +00:00
self . init
@item = check_user_return_item
@saved = @item . destroy
2006-01-10 06:15:48 +00:00
@on_page = " home "
if @saved
@down_count = @todos . collect { | x | ( ! x . done? and ! x . context . hide? ) ? x : nil } . compact . size . to_s
end
2006-01-04 19:49:15 +00:00
return if request . xhr?
# fallback for standard requests
if @saved
2006-01-08 13:21:24 +00:00
flash [ " notice " ] = 'Successfully deleted next action'
2006-01-04 19:49:15 +00:00
redirect_to :action = > 'list'
2005-06-11 12:24:19 +00:00
else
2006-01-04 19:49:15 +00:00
render :action = > 'list'
2005-06-11 12:24:19 +00:00
end
2006-01-04 19:49:15 +00:00
rescue
if request . xhr? # be sure to include an error.rjs
render :action = > 'error'
else
flash [ " warning " ] = 'An error occurred on the server.'
render :action = > 'list'
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
# List the completed tasks, sorted by completion date
2005-06-11 12:24:19 +00:00
#
2005-12-04 11:43:09 +00:00
# Use days declaration? 1.day.ago?
def completed
2005-08-08 01:54:05 +00:00
self . init
2005-12-04 11:43:09 +00:00
@page_title = " TRACKS::Completed tasks "
day = ( 60 * 60 * 24 )
today = Time . now
today_date = today - ( 1 * day )
week_begin = today - ( 1 * day )
week_end = today - ( 7 * day )
month_begin = today - ( 8 * day )
month_end = today - ( 31 * day )
@done_today = @done . collect { | x | today_date < = x . completed ? x : nil } . compact
@done_this_week = @done . collect { | x | week_begin > = x . completed && week_end < = x . completed ? x : nil } . compact
@done_this_month = @done . collect { | x | month_begin > = x . completed && month_end < = x . completed ? x : nil } . compact
2005-05-28 14:08:44 +00:00
2005-08-08 01:54:05 +00:00
end
2005-12-04 11:43:09 +00:00
# Archived completed items, older than 31 days
#
def completed_archive
self . init
@page_title = " TRACKS::Archived completed tasks "
archive_date = Time . now - 32 * ( 60 * 60 * 24 )
@done_archive = @done . collect { | x | archive_date > = x . completed ? x : nil } . compact
end
2005-08-08 01:54:05 +00:00
protected
def check_user_return_item
item = Todo . find ( @params [ 'id' ] )
if @session [ 'user' ] == item . user
return item
else
flash [ " warning " ] = " Item and session user mis-match: #{ item . user . name } and #{ @session [ 'user' ] . name } ! "
render_text " "
end
end
def init
@user = @session [ 'user' ]
@projects = @user . projects
@contexts = @user . contexts
@todos = @user . todos
2006-01-15 14:38:57 +00:00
@done = Todo . find ( :all , :conditions = > [ " todos.user_id = ? and todos.done = ? " , @user . id , true ] , :include = > [ :project ] , :order = > " completed DESC " )
2005-11-06 18:28:00 +00:00
# for some reason, this generates an error about anil object under 0.14.2
#@done = @todos.collect { |x| x.done? ? x:nil }.compact.sort! {|x,y| y.completed <=> x.completed }
2005-08-08 01:54:05 +00:00
end
2005-12-04 11:43:09 +00:00
2005-01-09 11:59:57 +00:00
end