fix test failures after the changes and refactorings

This commit is contained in:
Reinier Balt 2011-06-12 04:29:35 +02:00
parent 559a02d6f1
commit 8546ae5dfa
8 changed files with 30 additions and 81 deletions

View file

@ -80,22 +80,12 @@ class ApplicationController < ActionController::Base
# actions or multiple actions
#
def count_undone_todos_phrase(todos_parent, string="actions")
count = count_undone_todos(todos_parent)
count = todos_parent.nil? ? 0 : todos_parent.todos.not_completed.count
# count_undone_todos(todos_parent)
word = count == 1 ? string.singularize : string.pluralize
return count.to_s + "&nbsp;" + word
end
def count_undone_todos(todos_parent)
return 0 if todos_parent.nil?
if (todos_parent.is_a?(Project) && todos_parent.hidden?)
count = eval "@project_project_hidden_todo_counts[#{todos_parent.id}]"
else
count = eval "@#{todos_parent.class.to_s.downcase}_not_done_counts[#{todos_parent.id}]"
end
end
# Convert a date object to the format specified in the user's preferences in
# config/settings.yml
#
@ -248,25 +238,8 @@ class ApplicationController < ActionController::Base
@active_contexts = current_user.contexts.active
@hidden_contexts = current_user.contexts.hidden
init_not_done_counts
if prefs.show_hidden_projects_in_sidebar
init_project_hidden_todo_counts(['project'])
end
end
def init_not_done_counts(parents = ['project','context'])
parents.each do |parent|
eval("@#{parent}_not_done_counts = @#{parent}_not_done_counts || current_user.todos.active.count(:group => :#{parent}_id)")
end
end
def init_project_hidden_todo_counts(parents = ['project','context'])
parents.each do |parent|
eval("@#{parent}_project_hidden_todo_counts = @#{parent}_project_hidden_todo_counts || current_user.todos.count(:conditions => ['state = ? or state = ?', 'project_hidden', 'active'], :group => :#{parent}_id)")
end
end
# Set the contents of the flash message from a controller Usage: notify
# :warning, "This is the message" Sets the flash of type 'warning' to "This is
# the message"

View file

@ -19,8 +19,6 @@ class ContextsController < ApplicationController
@all_contexts = @active_contexts + @hidden_contexts
@count = @all_contexts.size
init_not_done_counts(['context'])
respond_to do |format|
format.html &render_contexts_html
format.m &render_contexts_mobile

View file

@ -14,13 +14,11 @@ class ProjectsController < ApplicationController
projects_and_actions
else
@contexts = current_user.contexts.all
init_not_done_counts(['project'])
if params[:only_active_with_no_next_actions]
@projects = current_user.projects.active.select { |p| count_undone_todos(p) == 0 }
@projects = current_user.projects.active.select { |p| p.todos.count == 0 }
else
@projects = current_user.projects.all
end
init_project_hidden_todo_counts(['project'])
respond_to do |format|
format.html &render_projects_html
format.m &render_projects_mobile
@ -209,16 +207,12 @@ class ProjectsController < ApplicationController
@state = params['state']
@projects = current_user.projects.alphabetize(:state => @state) if @state
@contexts = current_user.contexts
init_not_done_counts(['project'])
init_project_hidden_todo_counts(['project']) if @state == 'hidden'
end
def actionize
@state = params['state']
@projects = current_user.projects.actionize(current_user.id, :state => @state) if @state
@projects = current_user.projects.actionize(:state => @state) if @state
@contexts = current_user.contexts
init_not_done_counts(['project'])
init_project_hidden_todo_counts(['project']) if @state == 'hidden'
end
protected
@ -293,7 +287,6 @@ class ProjectsController < ApplicationController
def render_text_feed
lambda do
init_project_hidden_todo_counts(['project'])
render :action => 'index', :layout => false, :content_type => Mime::TEXT
end
end

View file

@ -54,7 +54,7 @@ class Todo < ActiveRecord::Base
include AASM
aasm_column :state
aasm_initial_state Proc.new { |todo| (todo.show_from && (todo.show_from > todo.user.date)) ? :deferred : :active}
aasm_initial_state Proc.new { |t| (t.show_from && t.user && (t.show_from > t.user.date)) ? :deferred : :active}
# when entering active state, also remove completed_at date. Looks like :exit
# of state completed is not run, see #679
@ -221,9 +221,9 @@ class Todo < ActiveRecord::Base
def show_from=(date)
# parse Date objects into the proper timezone
date = user.at_midnight(date) if (date.is_a? Date)
# show_from needs to be set before state_change because of "bug" in aasm.
# If show_From is not set, the todo will not validate and thus aasm will not save
# If show_from is not set, the todo will not validate and thus aasm will not save
# (see http://stackoverflow.com/questions/682920/persisting-the-state-column-on-transition-using-rubyist-aasm-acts-as-state-machi)
self[:show_from] = date
@ -338,4 +338,5 @@ class Todo < ActiveRecord::Base
todo.project_id = project_id unless project_id.nil?
return todo
end
end

View file

@ -59,22 +59,16 @@ class User < ActiveRecord::Base
self.update_positions(projects.map{ |p| p.id })
return projects
end
def actionize(user_id, scope_conditions = {})
query_state = scope_conditions[:state] ? "AND projects.state = '#{scope_conditions[:state]}' " : ""
projects_with_active_todos = Project.find_by_sql([
"SELECT projects.id, count(todos.id) as p_count " +
"FROM projects " +
"LEFT OUTER JOIN todos ON todos.project_id = projects.id "+
"WHERE projects.user_id = ? AND " +
"NOT (todos.state='completed' OR todos.state='deferred' OR todos.state='pending') " +
query_state +
" GROUP BY projects.id ORDER by p_count DESC",user_id])
all_project_ids = Project.find_by_sql([
"SELECT id FROM projects WHERE projects.user_id = ? " + query_state, user_id
])
ids_in_new_positions = projects_with_active_todos.map{|p| p.id}
ids_of_unselected_projects = all_project_ids.map{|p| p.id} - ids_in_new_positions
self.update_positions(ids_in_new_positions + ids_of_unselected_projects)
def actionize(scope_conditions = {})
todos_in_project = find(:all, :conditions => scope_conditions, :include => [:todos])
todos_in_project.sort!{ |x, y| -(x.todos.active.count <=> y.todos.active.count) }
todos_in_project.reject{ |p| p.todos.active.count > 0 }
sorted_project_ids = todos_in_project.map {|p| p.id}
all_project_ids = find(:all).map {|p| p.id}
other_project_ids = all_project_ids - sorted_project_ids
update_positions(sorted_project_ids + other_project_ids)
return find(:all, :conditions => scope_conditions)
end