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
2006-08-08 23:38:42 +00:00
append_before_filter :init , :except = > [ :destroy , :completed , :completed_archive ]
2006-11-12 19:12:36 +00:00
layout " standard " , :except = > :date_preview
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
2006-08-08 23:38:42 +00:00
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
2006-09-17 06:53:33 +00:00
max_completed = @user . preference . show_number_completed - 1
2006-07-28 06:55:35 +00:00
@done = nil
if max_completed > 0
@done = Todo . find ( :all ,
2006-11-15 09:05:07 +00:00
:conditions = > [ 'todos.user_id = ? and todos.state = ?' , @user . id , 'completed' ] ,
:order = > 'todos.completed_at DESC' ,
2006-07-28 06:55:35 +00:00
: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?
2006-11-05 10:47:51 +00:00
flash [ :warning ] = 'You must add at least one context before adding next actions.'
2006-02-26 12:02:11 +00:00
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-11-15 09:05:07 +00:00
@count = @todos . reject { | x | ! x . active? || x . context . hide? } . size
2006-06-10 05:52:20 +00:00
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
Started adding support for the Chronic library, to provide natural language date selections. You can now type phrases such as "tomorrow", "nov 10", "1 week hence", "10 days hence" and so on into the date box and these will be parsed into a valid date. I haven't managed to get proper validation working yet, but you'll get a live preview of the parsed date just below the input box.
What doesn't work yet:
* If you delete all characters in the date box, you'll get an error message. This will go away if you type more characters
* You'll get an error as above when the form is cleared and redisplayed after submission. Again, it will go away if you type anything in the box.
* Validation doesn't work, but the preview will display "Invalid date" if Chronic can't parse your phrase
* This isn't added to the edit form for actions yet.
Also partially fixed #394: the mobile interface works again, but you might get an error visiting the subsequent pages of a filtered view (i.e. viewing a single context or project). I'm not sure what's causing this, but it's on my list to fix.
git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@332 a4c988fc-2ded-0310-b66e-134b36920a42
2006-10-25 15:35:08 +00:00
def date_preview
return if params [ " todo_due " ] . blank?
@date = parse_date_per_user_prefs ( params [ " todo_due " ] )
if @date . nil?
@form_date = " Invalid date "
else
@form_date = @date . strftime ( " %a %b %d %Y " )
end
render :partial = > " shared/date_preview " , :layout = > false
end
2006-09-16 06:50:22 +00:00
def create
2006-08-08 23:38:42 +00:00
init
2006-01-04 19:49:15 +00:00
@item = @user . todos . build
2006-09-16 06:50:22 +00:00
p = params [ 'todo' ] || params [ 'request' ] [ 'todo' ]
@item . attributes = p
2006-07-17 03:40:35 +00:00
2006-07-28 04:09:02 +00:00
if @item . due?
Started adding support for the Chronic library, to provide natural language date selections. You can now type phrases such as "tomorrow", "nov 10", "1 week hence", "10 days hence" and so on into the date box and these will be parsed into a valid date. I haven't managed to get proper validation working yet, but you'll get a live preview of the parsed date just below the input box.
What doesn't work yet:
* If you delete all characters in the date box, you'll get an error message. This will go away if you type more characters
* You'll get an error as above when the form is cleared and redisplayed after submission. Again, it will go away if you type anything in the box.
* Validation doesn't work, but the preview will display "Invalid date" if Chronic can't parse your phrase
* This isn't added to the edit form for actions yet.
Also partially fixed #394: the mobile interface works again, but you might get an error visiting the subsequent pages of a filtered view (i.e. viewing a single context or project). I'm not sure what's causing this, but it's on my list to fix.
git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@332 a4c988fc-2ded-0310-b66e-134b36920a42
2006-10-25 15:35:08 +00:00
@date = parse_date_per_user_prefs ( p [ " due " ] )
@item . due = @date . to_s ( :db )
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
2006-11-15 09:05:07 +00:00
@up_count = @todos . reject { | x | ! x . active? or x . context . hide? } . size . to_s
2006-07-28 06:55:35 +00:00
end
2006-09-16 06:50:22 +00:00
render :action = > 'create'
2006-07-28 06:55:35 +00:00
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
2006-11-05 10:47:51 +00:00
flash [ :warning ] = 'An error occurred on the server.'
2006-07-28 04:09:02 +00:00
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-09-16 06:50:22 +00:00
def add_item
create
end
2006-07-17 03:40:35 +00:00
2006-08-01 07:03:31 +00:00
def edit
2006-08-08 23:38:42 +00:00
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
2006-08-08 23:38:42 +00:00
init
2006-06-10 06:00:32 +00:00
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
2006-08-08 23:38:42 +00:00
init
2006-11-15 09:05:07 +00:00
logger . info " source view is " + @source_view
2006-01-10 06:15:48 +00:00
@item = check_user_return_item
2006-11-15 09:05:07 +00:00
@item . toggle_completion ( )
2006-01-10 06:15:48 +00:00
@saved = @item . save
if @saved
2006-11-15 09:05:07 +00:00
@remaining_undone_in_context = @user . contexts . find ( @item . context_id ) . not_done_todo_count
determine_down_count
2006-01-10 06:15:48 +00:00
end
return if request . xhr?
if @saved
2006-11-15 09:05:07 +00:00
redirect_with_notice " The action <strong>' #{ @item . description } '</strong> was marked as <strong> #{ @item . completed? ? 'complete' : 'incomplete' } </strong> " , :action = > " index "
2006-01-10 06:15:48 +00:00
else
2006-11-15 09:05:07 +00:00
redirect_with_notice " The action <strong>' #{ @item . description } '</strong> was NOT marked as <strong> #{ @item . completed? ? 'complete' : 'incomplete' } due to an error on the server.</strong> " , :action = > " index "
2005-12-04 11:43:09 +00:00
end
end
2006-08-02 03:40:22 +00:00
def update
2006-08-08 23:38:42 +00:00
init
2006-04-11 12:40:10 +00:00
@item = check_user_return_item
@original_item_context_id = @item . context_id
2006-10-12 03:48:48 +00:00
@original_item_project_id = @item . project_id
2006-05-13 13:32:39 +00:00
@item . attributes = params [ " item " ]
2006-08-08 23:38:42 +00:00
if params [ " item " ] . has_key? ( " due " )
params [ " item " ] [ " due " ] = parse_date_per_user_prefs ( params [ " item " ] [ " due " ] )
2005-05-28 14:08:44 +00:00
else
2006-08-08 23:38:42 +00:00
params [ " item " ] [ " due " ] = " "
2005-05-28 14:08:44 +00:00
end
2006-08-08 23:38:42 +00:00
@saved = @item . update_attributes params [ " item " ]
2006-10-12 05:00:46 +00:00
@context_changed = @original_item_context_id != @item . context_id
if @context_changed then @remaining_undone_in_context = @user . contexts . find ( @original_item_context_id ) . not_done_todos . length ; end
@project_changed = @original_item_project_id != @item . project_id
2006-10-16 06:04:37 +00:00
if ( @project_changed && ! @original_item_project_id . nil? ) then @remaining_undone_in_project = @user . projects . find ( @original_item_project_id ) . not_done_todos . length ; end
2005-01-09 11:59:57 +00:00
end
2006-07-03 00:22:28 +00:00
def update_context
2006-08-08 23:38:42 +00:00
init
2006-07-03 00:22:28 +00:00
@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
2006-08-08 23:38:42 +00:00
init
2006-07-03 00:59:31 +00:00
@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-08-25 04:24:58 +00:00
@context_id = @item . context_id
@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
2006-08-08 23:38:42 +00:00
redirect_with_notice 'Successfully deleted next action' , :action = > 'index'
2006-08-01 07:03:31 +00:00
else
2006-08-08 23:38:42 +00:00
redirect_with_warning 'Failed to delete the action.' , :action = > 'index'
2006-08-01 07:03:31 +00:00
end
end
wants . js do
if @saved
2006-11-15 09:05:07 +00:00
determine_down_count
2006-08-01 07:03:31 +00:00
source_view do | from |
from . todo do
2006-10-12 05:00:46 +00:00
@remaining_undone_in_context = @user . contexts . find ( @context_id ) . not_done_todos . length
2006-08-01 07:03:31 +00:00
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
2006-11-05 10:47:51 +00:00
flash [ :warning ] = 'An error occurred on the server.'
2006-08-01 07:03:31 +00:00
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
2006-08-08 23:38:42 +00:00
private
2005-08-08 01:54:05 +00:00
def check_user_return_item
2006-11-15 09:05:07 +00:00
item = Todo . find ( params [ 'id' ] . to_i )
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
2006-11-05 10:47:51 +00:00
flash [ :warning ] = @error_message
2006-08-01 07:03:31 +00:00
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'
2006-11-15 09:05:07 +00:00
init_data_for_sidebar
init_todos
2006-07-17 03:40:35 +00:00
end
def init_todos
2006-11-12 19:12:36 +00:00
# Exclude hidden projects from count on home page
2006-11-15 09:05:07 +00:00
@todos = @user . todos . find ( :all , :conditions = > [ 'todos.state = ? or todos.state = ?' , 'active' , 'complete' ] , :include = > [ :project , :context ] )
2006-07-28 06:55:35 +00:00
2006-11-12 19:12:36 +00:00
# Exclude hidden projects from the home page
2006-11-15 09:05:07 +00:00
@not_done_todos = @user . todos . find ( :all , :conditions = > [ 'todos.state = ?' , 'active' ] , :order = > " todos.due IS NULL, todos.due ASC, todos.created_at ASC " , :include = > [ :project , :context ] )
2006-08-08 23:38:42 +00:00
end
def determine_down_count
source_view do | from |
from . todo do
2006-11-15 09:05:07 +00:00
@down_count = Todo . count_by_sql ( [ 'SELECT COUNT(*) FROM todos, contexts WHERE todos.context_id = contexts.id and todos.user_id = ? and todos.state = ? and contexts.hide = ?' , @user . id , 'active' , false ] )
2006-08-08 23:38:42 +00:00
end
from . context do
2006-11-15 09:05:07 +00:00
@down_count = @user . contexts . find ( @item . context_id ) . todos . count_in_state ( :active )
2006-08-08 23:38:42 +00:00
end
from . project do
2006-11-15 09:05:07 +00:00
unless @item . project_id == nil
@down_count = @user . projects . find ( @item . project_id ) . todos . count_in_state ( :active )
end
2006-08-08 23:38:42 +00:00
end
end
end
2006-07-28 06:55:35 +00:00
2005-01-09 11:59:57 +00:00
end