From b8f289507cc6d336c8b09d438055b353181ca96a Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Tue, 9 Oct 2007 22:59:41 +0000 Subject: [PATCH] ActionItem96: find_by_code finds only active tasks --- app/models/task.rb | 12 ++++++++++++ test/unit/task_test.rb | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 0 deletions(-) diff --git a/app/models/task.rb b/app/models/task.rb index 2f8433a..1e9819f 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -108,6 +108,9 @@ class Task < ActiveRecord::Base end class << self + + # generates a random code string consisting of 36 characters in the ranges + # a-z and 0-9 def generate_code chars = ('a'..'z').to_a + ('0'..'9').to_a code = "" @@ -116,6 +119,15 @@ class Task < ActiveRecord::Base end code end + + # finds a task by its (generated) code. Only returns a task with the + # specified code AND with status = Task::Status::ACTIVE. + # + # Can be used in subclasses to find only their instances. + def find_by_code(code) + self.find(:first, :conditions => { :code => code, :status => Task::Status::ACTIVE }) + end + end end diff --git a/test/unit/task_test.rb b/test/unit/task_test.rb index 8977f3f..7067732 100644 --- a/test/unit/task_test.rb +++ b/test/unit/task_test.rb @@ -107,4 +107,22 @@ class TaskTest < Test::Unit::TestCase assert_equal 36, code.size end + should 'find only in active tasks' do + task = Task.new + task.requestor = User.create(:login => 'testfindinactivetask', :password => 'test', :password_confirmation => 'test', :email => 'testfindinactivetask@localhost.localdomain').person + task.save! + + task.cancel + + assert_nil Task.find_by_code(task.code) + end + + should 'be able to find active tasks ' do + task = Task.new + task.requestor = User.create(:login => 'testfindinactivetask', :password => 'test', :password_confirmation => 'test', :email => 'testfindinactivetask@localhost.localdomain').person + task.save! + + assert_not_nil Task.find_by_code(task.code) + end + end -- libgit2 0.21.2