Optimized ActiveRecord calls to minimize SQL queries, especially on the home page. Also, moved feed page to feed controller.

git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@294 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
lukemelia 2006-07-28 06:55:35 +00:00
parent afaf2235fa
commit e0e407d99a
30 changed files with 151 additions and 115 deletions

View file

@ -72,4 +72,19 @@ class ApplicationController < ActionController::Base
@admin = User.find(:first, :conditions => [ "is_admin = ?", true ])
end
def init_data_for_sidebar
@projects = @user.projects
@contexts = @user.contexts
init_not_done_counts
end
def init_not_done_counts
@project_not_done_counts = Todo.count(:todo,
:conditions => ['todos.user_id = ? and todos.type = ? and todos.done = ?', @user.id, "Immediate", false],
:group => :project_id)
@context_not_done_counts = Todo.count(:todo,
:conditions => ['todos.user_id = ? and todos.type = ? and todos.done = ?', @user.id, "Immediate", false],
:group => :context_id)
end
end

View file

@ -27,6 +27,7 @@ class ContextController < ApplicationController
#
def show
init
check_user_set_context
init_todos
@on_page = "context"
@page_title = "TRACKS::Context: #{@context.name}"
@ -219,13 +220,14 @@ class ContextController < ApplicationController
@contexts = @user.contexts
@todos = @user.todos
@done = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ?", @user.id, true], :include => [:project], :order => "completed DESC")
init_not_done_counts
end
def init_todos
check_user_set_context
@done = @context.done_todos
@not_done = @context.not_done_todos
@count = @not_done.size
@not_done_todos = @context.not_done_todos
@count = @not_done_todos.size
end
end

View file

@ -12,6 +12,7 @@ class DeferredController < ApplicationController
def index
init_projects_and_contexts
init_not_done_counts
@page_title = "TRACKS::Tickler"
@tickles = @user.todos.find(:all, :conditions => ['type = ?', "Deferred"], :order => "show_from ASC")
@count = @tickles.size

View file

@ -4,11 +4,16 @@ class FeedController < ApplicationController
helper :feed
model :todo, :context, :project
session :disabled => true # Prevents session control from interfering with feed
session :disabled => true, :except => 'index' # Prevents session control from interfering with feed
before_filter :check_token_against_user_word
before_filter :check_token_against_user_word, :except => 'index'
prepend_before_filter :login_required, :only => 'index'
def index
@page_title = 'TRACKS::Feeds'
init_data_for_sidebar
render :layout => 'standard'
end
# Build an RSS feed

View file

@ -252,6 +252,7 @@ class ProjectController < ApplicationController
@contexts = @user.contexts
@todos = @user.todos
@done = Todo.find(:all, :conditions => ["todos.user_id = ? and todos.done = ?", @user.id, true], :include => [:project], :order => "completed DESC")
init_not_done_counts
end
def init_todos

View file

@ -14,14 +14,24 @@ class TodoController < ApplicationController
# Number of completed actions to show is determined by a setting in settings.yml
def index
self.init
@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
# isn't shown on the home page
max_completed = @user.preferences["no_completed"].to_i-1
@done = (max_completed > 0) ? @done[0..max_completed] : nil
@done = nil
if max_completed > 0
@done = Todo.find(:all,
:conditions => ['todos.user_id = ? and todos.done = ?', @user.id, true],
:order => 'todos.completed DESC',
:limit => max_completed,
:include => [ :project, :context ])
end
@contexts_to_show = @contexts.reject {|x| x.hide? }
if @contexts.empty?
@ -37,10 +47,6 @@ class TodoController < ApplicationController
end
end
# Is this used? Seems like it should be deleted. -lukemelia, 2006-07-16
def update_element
end
# Called by a form button
# Parameters from form fields are passed to create new action
# in the selected context.
@ -58,14 +64,15 @@ class TodoController < ApplicationController
@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 => "index" }
wants.js
wants.js do
if @saved
init_todos
@up_count = @todos.reject { |x| x.done? or x.context.hide? }.size.to_s
end
render
end
wants.xml { render :xml => @item.to_xml( :root => 'todo', :except => :user_id ) }
end
@ -215,11 +222,13 @@ class TodoController < ApplicationController
self.init
@page_title = "TRACKS::Completed tasks"
unless @done.nil?
@done_today = @done.collect { |x| x.completed >= 1.day.ago ? x:nil }.compact
@done_this_week = @done.collect { |x| 1.week.ago <= x.completed ? x:nil }.compact
@done_this_month = @done.collect { |x| 4.week.ago <= x.completed ? x:nil }.compact
end
@done = Todo.find(:all,
:conditions => ['todos.user_id = ? and todos.done = ? and todos.completed is not null', @user.id, true],
:order => 'todos.completed DESC',
:include => [ :project, :context ])
@done_today = @done.collect { |x| x.completed >= 1.day.ago ? x:nil }.compact
@done_this_week = @done.collect { |x| 1.week.ago <= x.completed ? x:nil }.compact
@done_this_month = @done.collect { |x| 4.week.ago <= x.completed ? x:nil }.compact
end
# Archived completed items, older than 28 days
@ -227,14 +236,13 @@ class TodoController < ApplicationController
def completed_archive
self.init
@page_title = "TRACKS::Archived completed tasks"
@done = Todo.find(:all,
:conditions => ['todos.user_id = ? and todos.done = ? and todos.completed is not null', @user.id, true],
:order => 'todos.completed DESC',
:include => [ :project, :context ])
@done_archive = @done.collect { |x| 28.day.ago > x.completed ? x:nil }.compact
end
def feeds
self.init
@page_title = "TRACKS::Feeds"
end
protected
def check_user_return_item
@ -251,11 +259,18 @@ class TodoController < ApplicationController
@projects = @user.projects
@contexts = @user.contexts
init_todos
init_not_done_counts
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')
@todos = Todo.find(:all,
:conditions => ['todos.user_id = ? and todos.type = ?', @user.id, "Immediate"],
:include => [ :project, :context ])
@not_done_todos = Todo.find(:all,
:conditions => ['todos.user_id = ? and todos.type = ? and todos.done = ?', @user.id, "Immediate", false],
:order => "todos.due IS NULL, todos.due ASC, todos.created_at ASC",
:include => [ :project, :context ])
end
end