tracks/tracks/app/controllers/todo_controller.rb
bsag 8349e760b3 First stage of cleaning up the messages in project/show which remind you if there are no uncompleted or completed actions, or notes in the current project. The show method checks whether @not_done, @done or @notes is empty, and if it is sets a message which is displayed in the partial.
I've added Scriptaculous calls to the Ajax for adding a new action, or completing a previously not done action removes the appropriate message (as these activities must logically make @not_done and @done non-empty. Similar thing added for project notes.

The next step will be to handle those cases where the last action is completed or deleted, or an unchecked done item refills the not_done actions, which is trickier to pick up. Then I'll repeat for context and todo views.



git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@164 a4c988fc-2ded-0310-b66e-134b36920a42
2005-11-27 17:56:34 +00:00

150 lines
4 KiB
Ruby

class TodoController < ApplicationController
helper :todo
model :context, :project, :user
before_filter :login_required
layout "standard"
def index
list
render_action "list"
end
# 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
#
def list
self.init
@page_title = "TRACKS::List tasks"
@done = @done[0..(NO_OF_ACTIONS-1)]
# Set count badge to number of not-done, not hidden context items
@count = @todos.collect { |x| ( !x.done? and !x.context.hidden? ) ? x:nil }.compact.size
end
# List the completed tasks, sorted by completion date
#
# Use days declaration? 1.day.ago?
def completed
self.init
@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
end
# 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
# Called by a form button
# Parameters from form fields are passed to create new action
# in the selected context.
def add_item
self.init
item = @user.todos.build
item.attributes = @params["new_item"]
if item.due?
item.due = Date.strptime(@params["new_item"]["due"], DATE_FORMAT)
else
item.due = ""
end
if item.save
render :partial => 'item', :object => item, :project => @params["project"]
else
flash["warning"] = "Couldn't add next action \"#{item.description}\""
render_text ""
end
end
# Edit the details of an action
#
def update_action
self.init
item = check_user_return_item
item.attributes = @params["item"]
if item.due?
item.due = Date.strptime(@params["item"]["due"], DATE_FORMAT)
else
item.due = ""
end
if item.save
render :partial => 'item', :object => item, :project => @params["project"]
else
flash["warning"] = "Couldn't update the action"
render_text ""
end
end
# Delete a next action in a context
#
def destroy_action
item = check_user_return_item
if item.destroy
render_text ""
else
flash["warning"] = "Couldn't delete next action \"#{item.description}\""
render_text ""
end
end
# Toggles the 'done' status of the action
#
def toggle_check
self.init
item = check_user_return_item
item.toggle!('done')
item.completed = Time.now() # For some reason, the before_save in todo.rb stopped working
if item.save
render :partial => 'item', :object => item
end
end
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
@done = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = 1", @user.id], :include => [:project], :order => "completed DESC")
# 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 }
end
end