Unify controller action for destroying an action into the todo controller and lay some groundwork for similar requests coming from different views. Fixes #343.

git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@300 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
lukemelia 2006-08-01 07:03:31 +00:00
parent 1f6a99a27f
commit 9392d4d696
23 changed files with 152 additions and 149 deletions

View file

@ -2,6 +2,7 @@
# Likewise will all the methods added be available for all controllers.
require_dependency "login_system"
require_dependency "source_view"
require "redcloth"
require 'date'
@ -73,6 +74,7 @@ class ApplicationController < ActionController::Base
end
def parse_date_per_user_prefs( s )
return nil if s == ''
Date.strptime(s, @user.preferences["date_format"])
end

View file

@ -29,7 +29,6 @@ class ContextController < ApplicationController
init
check_user_set_context
init_todos
@on_page = "context"
@page_title = "TRACKS::Context: #{@context.name}"
end
@ -58,7 +57,6 @@ class ContextController < ApplicationController
end
@saved = @item.save
@on_page = "context"
if @saved
# This reports real count +1 for some reason that I don't understand
# Almost identical code for add_item in projects reports correct num
@ -85,36 +83,6 @@ class ContextController < ApplicationController
end
end
# Delete a next action
#
def destroy_action
self.init
@item = check_user_return_item
@saved = @item.destroy
if @saved
@down_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.context_id IN (?)", @user.id, false, @item.context_id]).size.to_s
end
return if request.xhr?
# fallback for standard requests
if @saved
flash["notice"] = 'Successfully deleted next action'
redirect_to :controller => 'todo', :action => 'list'
else
render :controller => 'todo', :action => 'list'
end
rescue
if request.xhr? # be sure to include an error.rjs
render :action => 'error'
else
flash["warning"] = 'An error occurred on the server.'
render :controller => 'todo', :action => 'list'
end
end
# Toggles the 'done' status of the action
#
def toggle_check
@ -217,6 +185,7 @@ class ContextController < ApplicationController
end
def init
@source_view = params['_source_view'] || 'context'
@projects = @user.projects.collect { |x| x.done? ? nil:x }.compact
@contexts = @user.contexts
@todos = @user.todos

View file

@ -11,6 +11,7 @@ class DeferredController < ApplicationController
def index
@source_view = 'deferred'
init_projects_and_contexts
init_not_done_counts
@page_title = "TRACKS::Tickler"
@ -19,6 +20,7 @@ class DeferredController < ApplicationController
end
def create
@source_view = 'deferred'
@item = Deferred.new
@item.attributes = params["todo"]
if params["todo"]["show_from"]
@ -46,12 +48,14 @@ class DeferredController < ApplicationController
end
def edit
@source_view = 'deferred'
init_projects_and_contexts
@item = check_user_return_item
render :layout => false
end
def update
@source_view = 'deferred'
@item = check_user_return_item
@original_item_context_id = @item.context_id
@item.attributes = params["item"]

View file

@ -31,7 +31,6 @@ class ProjectController < ApplicationController
init
init_todos
@notes = @project.notes
@on_page = "project"
@page_title = "TRACKS::Project: #{@project.name}"
if @contexts.empty?
@ -80,7 +79,6 @@ class ProjectController < ApplicationController
end
@saved = @item.save
@on_page = "project"
if @saved
@up_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.project_id IN (?)", @user.id, false, @item.project_id]).size.to_s
end
@ -104,37 +102,6 @@ class ProjectController < ApplicationController
redirect_to :controller => 'todo', :action => 'index'
end
end
# Delete a next action
#
def destroy_action
self.init
@item = check_user_return_item
@saved = @item.destroy
@on_page = "project"
if @saved
@down_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.project_id IN (?)", @user.id, false, @item.project_id]).size.to_s
end
return if request.xhr?
# fallback for standard requests
if @saved
flash["notice"] = 'Successfully deleted next action'
redirect_to :controller => 'todo', :action => 'index'
else
render :controller => 'todo', :action => 'index'
end
rescue
if request.xhr? # be sure to include an error.rjs
render :action => 'error'
else
flash["warning"] = 'An error occurred on the server.'
render :controller => 'todo', :action => 'index'
end
end
# Toggles the 'done' status of the action
#
@ -145,7 +112,6 @@ class ProjectController < ApplicationController
@item.toggle!('done')
@item.completed = Time.now() # For some reason, the before_save in todo.rb stopped working
@saved = @item.save
@on_page = "project"
if @saved
@down_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.project_id IN (?)", @user.id, false, @item.project_id]).size.to_s
@done_count = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ? and todos.project_id IN (?)", @user.id, true, @item.project_id]).size.to_s
@ -249,6 +215,7 @@ class ProjectController < ApplicationController
end
def init
@source_view = params['_source_view'] || 'project'
@projects = @user.projects
@contexts = @user.contexts
@todos = @user.todos

View file

@ -17,7 +17,6 @@ class TodoController < ApplicationController
@projects = @user.projects.find(:all, :include => [ :todos ])
@contexts = @user.contexts.find(:all, :include => [ :todos ])
@on_page = "home"
@page_title = "TRACKS::List tasks"
# If you've set no_completed to zero, the completed items box
@ -54,7 +53,6 @@ class TodoController < ApplicationController
self.init
@item = @user.todos.build
@item.attributes = params["todo"]
@on_page = "home"
if @item.due?
@item.due = parse_date_per_user_prefs(params["todo"]["due"])
@ -82,13 +80,13 @@ class TodoController < ApplicationController
wants.html do
flash["warning"] = 'An error occurred on the server.'
render :action => "index"
end
end
wants.js { render :action => 'error' }
wants.xml { render :text => 'An error occurred on the server.' + $! }
end
end
def edit_action
def edit
self.init
@item = check_user_return_item
render :layout => false
@ -111,7 +109,6 @@ class TodoController < ApplicationController
@item.toggle!('done')
@item.completed = Time.now() # For some reason, the before_save in todo.rb stopped working
@saved = @item.save
@on_page = "home"
@remaining_undone_in_context = Todo.count(:conditions => ['user_id = ? and context_id = ? and type = ? and done = ?', @user.id, @item.context_id, "Immediate", false])
if @saved
@down_count = @todos.collect { |x| ( !x.done? and !x.context.hide? ) ? x:nil }.compact.size.to_s
@ -130,9 +127,6 @@ class TodoController < ApplicationController
#
def update_action
self.init
if params["on_project_page"] == true
@on_page = "project"
end
@item = check_user_return_item
@original_item_context_id = @item.context_id
@item.attributes = params["item"]
@ -180,38 +174,61 @@ class TodoController < ApplicationController
end
end
# Delete a next action
#
def destroy_action
self.init
def destroy
@item = check_user_return_item
context_id = @item.context_id
project_id = @item.project_id
@saved = @item.destroy
@on_page = "home"
@remaining_undone_in_context = Todo.count(:conditions => ['user_id = ? and context_id = ? and type = ? and done = ?', @user.id, context_id, "Immediate", false])
if @saved
self.init
@down_count = @todos.reject { |x| x.done? or x.context.hide? }.size.to_s
end
return if request.xhr?
respond_to do |wants|
wants.html do
if @saved
flash["notice"] = 'Successfully deleted next action'
redirect_to :action => 'index'
else
flash["warning"] = 'Failed to delete the action.'
redirect_to :action => 'index'
end
end
wants.js do
if @saved
@down_count = 0
source_view do |from|
from.todo do
@down_count = Todo.count(:conditions => ['todos.user_id = ? and todos.type = ? and todos.done = ? and contexts.hide = ?',
@user.id, "Immediate", false, false],
:include => [ :context ])
@remaining_undone_in_context = Todo.count(:conditions => ['user_id = ? and context_id = ? and type = ? and done = ?',
@user.id, context_id, "Immediate", false])
end
from.context do
@down_count = Todo.count(:conditions => ['todos.user_id = ? and todos.type = ? and todos.done = ? and todos.context_id = ?',
@user.id, "Immediate", false, context_id])
end
from.project do
@down_count = Todo.count(:conditions => ['todos.user_id = ? and todos.type = ? and todos.done = ? and todos.project_id = ?',
@user.id, "Immediate", false, project_id]) unless project_id == nil
end
end
end
render
end
wants.xml { render :text => '200 OK. Action deleted.', :status => 200 }
# fallback for standard requests
if @saved
flash["notice"] = 'Successfully deleted next action'
redirect_to :action => 'index'
else
render :action => 'index'
end
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 => 'index'
end
respond_to do |wants|
wants.html do
flash["warning"] = 'An error occurred on the server.'
redirect_to :action => 'index'
end
wants.js { render :action => 'error' }
wants.xml { render :text => 'An error occurred on the server.' + $! }
end
end
# List the completed tasks, sorted by completion date
@ -250,12 +267,20 @@ class TodoController < ApplicationController
if @user == item.user
return item
else
flash["warning"] = "Item and session user mis-match: #{item.user.name} and #{@user.name}!"
render_text ""
@error_message = 'Item and session user mis-match: #{item.user.name} and #{@user.name}!'
respond_to do |wants|
wants.html do
flash["warning"] = @error_message
render :action => "index"
end
wants.js { render :action => 'error' }
wants.xml { render :text => @error_message, :status => 403 }
end
end
end
def init
@source_view = params['_source_view'] || 'todo'
@projects = @user.projects
@contexts = @user.contexts
init_todos