Implement Null Object pattern for Todo.project to save bsag and I some script/console headaches. Thanks to

Craig Ambrose for the "writeup":http://blog.craigambrose.com/articles/2006/09/22/active-record-associations-and-the-null-object-pattern



git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@342 a4c988fc-2ded-0310-b66e-134b36920a42
This commit is contained in:
lukemelia 2006-11-14 04:37:28 +00:00
parent 7fb8140129
commit 883bcb30bb
2 changed files with 22 additions and 0 deletions

View file

@ -33,6 +33,10 @@ class Project < ActiveRecord::Base
attr_protected :user
def self.null_object
NullProject.new
end
def description_present?
attribute_present?("description")
end
@ -42,3 +46,15 @@ class Project < ActiveRecord::Base
end
end
class NullProject
def hidden?
false
end
def nil?
true
end
end

View file

@ -14,6 +14,12 @@ class Todo < ActiveRecord::Base
validates_length_of :notes, :maximum => 60000, :allow_nil => true
# validates_chronic_date :due, :allow_nil => true
alias_method :original_project, :project
def project
original_project.nil? ? Project.null_object : original_project
end
def self.not_done( id=id )
self.find(:all, :conditions =>[ "done = ? AND context_id = ?", false, id], :order =>"due IS NULL, due ASC, created_at ASC")
end