Convert :dependent => true to :dependent => :delete_all on Project and Context, and brought acts_as_todo_container under test in anticipation of conversion to a simple module.

git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@348 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
lukemelia 2006-11-17 14:08:56 +00:00
parent 1d4c33e21d
commit b9b4330b8d
5 changed files with 60 additions and 11 deletions

View file

@ -1,6 +1,6 @@
class Context < ActiveRecord::Base
has_many :todos, :dependent => true, :order => "completed_at DESC"
has_many :todos, :dependent => :delete_all, :order => "completed_at DESC"
belongs_to :user
acts_as_list :scope => :user

View file

@ -1,5 +1,5 @@
class Project < ActiveRecord::Base
has_many :todos, :dependent => true
has_many :todos, :dependent => :delete_all
has_many :notes, :dependent => true, :order => "created_at DESC"
belongs_to :user

View file

@ -24,13 +24,11 @@ module Tracks
module InstanceMethods
def not_done_todos(opts={})
@not_done_todos = self.find_not_done_todos(opts) if @not_done_todos == nil
@not_done_todos
@not_done_todos ||= self.find_not_done_todos(opts)
end
def done_todos
@done_todos = self.find_done_todos if @done_todos == nil
@done_todos
@done_todos ||= self.find_done_todos
end
def find_not_done_todos(opts={})
@ -49,7 +47,7 @@ module Tracks
todos = Todo.find(:all, :conditions => ["todos.#{self.class.base_class.name.singularize.downcase}_id = ? AND todos.state = ?", id, "completed"],
:order => "completed_at DESC",
:include => find_todos_include,
:limit => @user.preference.show_number_completed)
:limit => self.user.preference.show_number_completed)
end
def not_done_todo_count(opts={})
@ -67,7 +65,7 @@ module Tracks
Todo.count(:conditions => ["todos.#{self.class.base_class.name.singularize.downcase}_id = ? AND todos.state = ?", id, "completed"],
:order => "completed_at DESC",
:include => find_todos_include,
:limit => @user.preference.show_number_completed)
:limit => self.user.preference.show_number_completed)
end
end

View file

@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/../test_helper'
class ContextTest < Test::Unit::TestCase
fixtures :contexts
fixtures :contexts, :todos, :users, :preferences
def setup
@agenda = contexts(:agenda)
@ -51,4 +51,29 @@ class ContextTest < Test::Unit::TestCase
assert_equal @agenda.id, c.id
end
def test_delete_context_deletes_todos_within_it
assert_equal 6, @agenda.todos.count
agenda_todo_ids = @agenda.todos.collect{|t| t.id }
@agenda.destroy
agenda_todo_ids.each do |todo_id|
assert !Todo.exists?(todo_id)
end
end
def test_not_done_todos
assert_equal 5, @agenda.not_done_todos.size
t = @agenda.not_done_todos[0]
t.complete!
t.save!
assert_equal 4, Context.find(@agenda.id).not_done_todos.size
end
def test_done_todos
assert_equal 1, @agenda.done_todos.size
t = @agenda.not_done_todos[0]
t.complete!
t.save!
assert_equal 2, Context.find(@agenda.id).done_todos.size
end
end

View file

@ -1,7 +1,7 @@
require File.dirname(__FILE__) + '/../test_helper'
class ProjectTest < Test::Unit::TestCase
fixtures :projects
fixtures :projects, :todos, :users, :preferences
def setup
@timemachine = projects(:timemachine)
@ -73,5 +73,31 @@ class ProjectTest < Test::Unit::TestCase
assert_not_nil p
assert_equal @timemachine.id, p.id
end
def test_delete_project_deletes_todos_within_it
assert_equal 2, @timemachine.todos.count
timemachine_todo_1_id = @timemachine.todos[0].id
timemachine_todo_2_id = @timemachine.todos[1].id
@timemachine.destroy
assert !Todo.exists?(timemachine_todo_1_id)
assert !Todo.exists?(timemachine_todo_2_id)
end
def test_not_done_todos
assert_equal 2, @timemachine.not_done_todos.size
t = @timemachine.not_done_todos[0]
t.complete!
t.save!
assert_equal 1, Project.find(@timemachine.id).not_done_todos.size
end
def test_done_todos
assert_equal 0, @timemachine.done_todos.size
t = @timemachine.not_done_todos[0]
t.complete!
t.save!
assert_equal 1, Project.find(@timemachine.id).done_todos.size
end
end