tracks/tracks/app/controllers/todo_controller.rb
bsag 80f3fdbc77 Applied Luke's patch (#164) to decrease the size of the homepage and load the edit forms dynamically when the edit button is clicked. Thanks, Luke!
As a result, I'm going to redo the empty message changes I made in [164], and will see if I can find a more sensible way to implement them. The empty messages still appear (now also in the notes area if that's empty), but you need a refresh after Ajax changes to view or remove them.



git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@165 a4c988fc-2ded-0310-b66e-134b36920a42
2005-12-04 11:43:09 +00:00

164 lines
4.3 KiB
Ruby

class TodoController < ApplicationController
model :user
model :project
model :context
helper :todo
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)]
@contexts_to_show = @contexts.clone
@contexts_to_show = @contexts_to_show.collect {|x| (!x.hidden? and !x.find_not_done_todos.empty?) ? x:nil }.compact
# 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
# 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
def edit_action
self.init
item = check_user_return_item
render :partial => 'action_edit_form', :object => item
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
# 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
# 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
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