tracks/tracks/app/controllers/todo_controller.rb

144 lines
3.6 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')
render :partial => 'item', :object => item
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 = @todos.collect { |x| x.done? ? x:nil }.compact.sort! {|x,y| y.completed <=> x.completed }
end
end