From f67e36871c4934af571f84945468f5fda058f591 Mon Sep 17 00:00:00 2001 From: lukemelia Date: Thu, 12 Oct 2006 05:00:46 +0000 Subject: [PATCH] Simplify some find methods in todo_controller using associations. Fix a bug in the RSS and TEXT feeds created by yesterday's change to the project status. git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@327 a4c988fc-2ded-0310-b66e-134b36920a42 --- tracks/app/controllers/context_controller.rb | 2 +- tracks/app/controllers/todo_controller.rb | 11 +++--- tracks/app/helpers/feed_helper.rb | 2 +- tracks/app/views/feed/projects_rss.rxml | 2 +- tracks/app/views/todo/update.rjs | 36 +++++++++++--------- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/tracks/app/controllers/context_controller.rb b/tracks/app/controllers/context_controller.rb index 8ab6f991..de167a9d 100644 --- a/tracks/app/controllers/context_controller.rb +++ b/tracks/app/controllers/context_controller.rb @@ -206,7 +206,7 @@ class ContextController < ApplicationController def init @source_view = params['_source_view'] || 'context' - @projects = @user.projects.collect { |x| x.done? ? nil:x }.compact + @projects = @user.projects.reject { |x| x.completed? } @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") diff --git a/tracks/app/controllers/todo_controller.rb b/tracks/app/controllers/todo_controller.rb index 40fd54d3..407942c3 100644 --- a/tracks/app/controllers/todo_controller.rb +++ b/tracks/app/controllers/todo_controller.rb @@ -111,7 +111,7 @@ class TodoController < ApplicationController @item.toggle!('done') @item.completed = Time.now() # For some reason, the before_save in todo.rb stopped working @saved = @item.save - @remaining_undone_in_context = Todo.count(:conditions => ['user_id = ? and context_id = ? and type = ? and done = ?', @user.id, @item.context_id, "Immediate", false]) + @remaining_undone_in_context = @user.contexts.find(@item.context_id).not_done_todos.length if @saved @down_count = @todos.reject { |x| x.done? || x.context.hide? }.size.to_s end @@ -136,8 +136,10 @@ class TodoController < ApplicationController params["item"]["due"] = "" end @saved = @item.update_attributes params["item"] - @remaining_undone_in_original_context = Todo.count(:conditions => ['user_id = ? and context_id = ? and type = ? and done = ?', @user.id, @original_item_context_id, "Immediate", false]) - @remaining_undone_in_original_project = Todo.count(:conditions => ['user_id = ? and project_id = ? and type = ? and done = ?', @user.id, @original_item_project_id, "Immediate", false]) + @context_changed = @original_item_context_id != @item.context_id + if @context_changed then @remaining_undone_in_context = @user.contexts.find(@original_item_context_id).not_done_todos.length; end + @project_changed = @original_item_project_id != @item.project_id + if @project_changed then @remaining_undone_in_project = @user.projects.find(@original_item_project_id).not_done_todos.length; end end def update_context @@ -195,8 +197,7 @@ class TodoController < ApplicationController @down_count = determine_down_count source_view do |from| from.todo do - @remaining_undone_in_context = Todo.count(:conditions => ['user_id = ? and context_id = ? and type = ? and done = ?', - @user.id, @context_id, "Immediate", false]) + @remaining_undone_in_context = @user.contexts.find(@context_id).not_done_todos.length end end end diff --git a/tracks/app/helpers/feed_helper.rb b/tracks/app/helpers/feed_helper.rb index 47c01b9b..cb73b6c4 100644 --- a/tracks/app/helpers/feed_helper.rb +++ b/tracks/app/helpers/feed_helper.rb @@ -37,7 +37,7 @@ module FeedHelper result_string << "\n" + p.name.upcase + "\n" result_string << p.description + "\n" if p.description_present? - result_string << "#{count_undone_todos(p)}. Project is #{p.done? ? 'Done' : 'Active'}.\n" + result_string << "#{count_undone_todos(p)}. Project is #{p.state}.\n" result_string << "#{p.linkurl}\n" if p.linkurl_present? result_string << "\n" end diff --git a/tracks/app/views/feed/projects_rss.rxml b/tracks/app/views/feed/projects_rss.rxml index c2ebab22..a19ce86d 100644 --- a/tracks/app/views/feed/projects_rss.rxml +++ b/tracks/app/views/feed/projects_rss.rxml @@ -11,7 +11,7 @@ xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do project_description = '' project_description += sanitize(markdown( p.description )) if p.description_present? project_description += "

#{count_undone_todos(p)}. " - project_description += "Project is #{p.done? ? 'Done' : 'Active'}. " + project_description += "Project is #{p.state}. " project_description += "#{p.linkurl}" if p.linkurl_present? project_description += "

" xml.description(project_description) diff --git a/tracks/app/views/todo/update.rjs b/tracks/app/views/todo/update.rjs index b0487076..15e38f3d 100644 --- a/tracks/app/views/todo/update.rjs +++ b/tracks/app/views/todo/update.rjs @@ -2,10 +2,7 @@ page.hide "info" if @saved item_container_id = "item-#{@item.id}-container" if source_view_is_one_of [:todo, :context] - if @item.context_id == @original_item_context_id - page.replace item_container_id, :partial => 'todo/item', :locals => { :parent_container_type => "context" } - page.visual_effect :highlight, item_container_id, :duration => 3 - else + if @context_changed page[item_container_id].remove if (@remaining_undone_in_original_context == 0) source_view do |from| @@ -13,24 +10,31 @@ if @saved from.context { page.show "c#{@original_item_context_id}empty-nd" } end end - page.call "todoItems.ensureVisibleWithEffectAppear", "c#{@item.context_id}" - page.call "todoItems.expandNextActionListingByContext", "c#{@item.context_id}items", true - page.insert_html :bottom, "c#{@item.context_id}items", :partial => 'todo/item', :locals => { :parent_container_type => "context" } + if source_view_is :todo + page.call "todoItems.ensureVisibleWithEffectAppear", "c#{@item.context_id}" + page.call "todoItems.expandNextActionListingByContext", "c#{@item.context_id}items", true + page.insert_html :bottom, "c#{@item.context_id}items", :partial => 'todo/item', :locals => { :parent_container_type => "context" } + end + page.replace_html("badge_count", @remaining_undone_in_context) if source_view_is :context page.delay(0.5) do page.call "todoItems.ensureContainerHeight", "c#{@original_item_context_id}items" - page.call "todoItems.ensureContainerHeight", "c#{@item.context_id}items" - page.visual_effect :highlight, item_container_id, :duration => 3 + if source_view_is :todo + page.call "todoItems.ensureContainerHeight", "c#{@item.context_id}items" + page.visual_effect :highlight, item_container_id, :duration => 3 + end end - end + else + page.replace item_container_id, :partial => 'todo/item', :locals => { :parent_container_type => "context" } + page.visual_effect :highlight, item_container_id, :duration => 3 + end elsif source_view_is :project - if @item.project_id == @original_item_project_id + if @project_changed + page[item_container_id].remove + page.show("p#{@original_item_project_id}empty-nd") if (@remaining_undone_in_project == 0) + page.replace_html "badge_count", @remaining_undone_in_project + else page.replace item_container_id, :partial => 'todo/item', :locals => { :parent_container_type => "project" } page.visual_effect :highlight, item_container_id, :duration => 3 - else - page[item_container_id].remove - if (@remaining_undone_in_original_project == 0) - page.show "p#{@original_item_project_id}empty-nd" - end end else logger.error "unexpected source_view '#{params[:_source_view]}'"