DRY up code around Immediate and Deferred actions, including controller methods and views

Relocated todo_controller#deferred_update_action to be next to todo_controller#update_action 
Include all non-hidden contexts on the home page, but hide the empty ones. Ground work for supporting the automatic showing of a context with zero items when an item is moved to it.
Added a comment about todo_controller#update_element -- is it used?
Remove unused #list_of method from context, note & project models
Remove unused #list_all method from note model
Methods to lazy load done_todos and not_done_todos for project and context models

Fixes #322



git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@282 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
lukemelia 2006-07-17 03:40:35 +00:00
parent 13f7f443af
commit 1ee651f8bb
19 changed files with 132 additions and 255 deletions

View file

@ -223,8 +223,8 @@ class ContextController < ApplicationController
def init_todos
check_user_set_context
@done = @context.find_done_todos
@not_done = @context.find_not_done_todos
@done = @context.done_todos
@not_done = @context.not_done_todos
@count = @not_done.size
end

View file

@ -256,8 +256,8 @@ class ProjectController < ApplicationController
def init_todos
check_user_set_project
@done = @project.find_done_todos
@not_done = @project.find_not_done_todos
@done = @project.done_todos
@not_done = @project.not_done_todos
@count = @not_done.size
end

View file

@ -28,7 +28,7 @@ class TodoController < ApplicationController
max_completed = @user.preferences["no_completed"].to_i-1
@done = (max_completed > 0) ? @done[0..max_completed] : nil
@contexts_to_show = @contexts.reject {|x| x.hide? || x.find_not_done_todos.empty? }
@contexts_to_show = @contexts.reject {|x| x.hide? }
if @contexts.empty?
flash['warning'] = 'You must add at least one context before adding next actions.'
@ -43,6 +43,7 @@ class TodoController < ApplicationController
end
end
# Is this used? Seems like it should be deleted. -lukemelia, 2006-07-16
def update_element
end
@ -53,83 +54,26 @@ class TodoController < ApplicationController
self.init
@item = @user.todos.build
@item.attributes = params["todo"]
if @item.due?
@item.due = Date.strptime(params["todo"]["due"], @user.preferences["date_format"])
else
@item.due = ""
end
@saved = @item.save
@on_page = "home"
if @saved
self.init # we have to do this again to update @todos
@up_count = @todos.reject { |x| x.done? or x.context.hide? }.size.to_s
end
respond_to do |wants|
# if @saved
# flash["notice"] = 'Added new next action.'
# else
# flash["warning"] = 'The next action was not added. Please try again.'
# end
wants.html { redirect_to :action => 'list' }
wants.js
wants.xml { render :xml => @item.to_xml( :root => 'todo', :except => :user_id ) }
end
rescue
respond_to do |wants|
wants.html { render :action => 'list' } # flash["warning"] = 'An error occurred on the server.' render :action => 'list' }# flash["warning"] = 'An error occurred on the server.' # render :action => 'list'
wants.js { render :action => 'error' }
wants.xml { render :text => 'An error occurred on the server.' + $! }
end
perform_add_item('list')
end
# Adding deferred actions from form on todo/tickler
#
def add_deferred_item
self.init
@tickle = Deferred.create(params["todo"])
if @tickle.due?
@tickle.due = Date.strptime(params["todo"]["due"], @user.preferences["date_format"])
else
@tickle.due = ""
end
@saved = @tickle.save
@item = Deferred.create(params["todo"])
@item.user_id = @user.id
@on_page = "tickler"
@on_page = "home"
if @saved
@up_count = @todos.collect { |x| ( !x.done? and !x.context.hide? ) ? x:nil }.compact.size.to_s
end
return if request.xhr?
# fallback for standard requests
if @saved
flash["notice"] = 'Added new next action.'
redirect_to :action => 'tickler'
else
flash["warning"] = 'The next action was not added. Please try again.'
redirect_to :action => 'tickler'
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 => 'tickler'
end
perform_add_item('tickler')
end
def edit_action
self.init
item = check_user_return_item
render :partial => 'action_edit_form', :object => item
@item = check_user_return_item
render :layout => false
end
def show
@ -140,13 +84,6 @@ class TodoController < ApplicationController
end
end
def edit_deferred_action
self.init
item = check_user_return_item
render :partial => 'action_edit_deferred_form', :object => item
end
# Toggles the 'done' status of the action
#
def toggle_check
@ -190,6 +127,21 @@ class TodoController < ApplicationController
@saved = @item.save
end
def deferred_update_action
#self.init
@tickle = check_user_return_item
@original_item_context_id = @tickle.context_id
@tickle.attributes = params["item"]
if @tickle.due?
@tickle.due = Date.strptime(params["item"]["due"], @user.preferences["date_format"])
else
@tickle.due = ""
end
@saved = @tickle.save
end
def update_context
self.init
@item = check_user_return_item
@ -223,21 +175,6 @@ class TodoController < ApplicationController
end
end
end
def deferred_update_action
#self.init
@tickle = check_user_return_item
@original_item_context_id = @tickle.context_id
@tickle.attributes = params["item"]
if @tickle.due?
@tickle.due = Date.strptime(params["item"]["due"], @user.preferences["date_format"])
else
@tickle.due = ""
end
@saved = @tickle.save
end
# Delete a next action
#
@ -304,6 +241,7 @@ class TodoController < ApplicationController
@page_title = "TRACKS::Tickler"
@tickles = @user.todos.find(:all, :conditions => ['type = ?', "Deferred"], :order => "show_from ASC")
@count = @tickles.size
@on_page = "tickler"
end
# Called by periodically_call_remote
@ -339,9 +277,43 @@ class TodoController < ApplicationController
def init
@projects = @user.projects
@contexts = @user.contexts
@todos = Todo.find(:all, :conditions => ['user_id = ? and type = ?', @user.id, "Immediate"])
@done = Todo.find(:all, :conditions => ['user_id = ? and done = ?', @user.id, true], :order => 'completed DESC')
# @todos = @todos.collect { |x| (x.class == Immediate) ? x : nil }.compact
init_todos
end
def init_todos
@todos = Todo.find(:all, :conditions => ['user_id = ? and type = ?', @user.id, "Immediate"])
@done = Todo.find(:all, :conditions => ['user_id = ? and done = ?', @user.id, true], :order => 'completed DESC')
end
def perform_add_item(redirect_action)
if @item.due?
@item.due = Date.strptime(params["todo"]["due"], @user.preferences["date_format"])
else
@item.due = ""
end
@saved = @item.save
if @saved
init_todos
@up_count = @todos.reject { |x| x.done? or x.context.hide? }.size.to_s
end
respond_to do |wants|
wants.html { redirect_to :action => redirect_action }
wants.js
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 { render :action => redirect_action } # TODO: would prefer something like: flash["warning"] = 'An error occurred on the server.' render :action => 'list'
wants.js { render :action => 'error' }
wants.xml { render :text => 'An error occurred on the server.' + $! }
end
end
end