Commit b8f289507cc6d336c8b09d438055b353181ca96a

Authored by AntonioTerceiro
1 parent 9d7da7aa

ActionItem96: find_by_code finds only active tasks



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@649 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing 2 changed files with 30 additions and 0 deletions   Show diff stats
app/models/task.rb
... ... @@ -108,6 +108,9 @@ class Task < ActiveRecord::Base
108 108 end
109 109  
110 110 class << self
  111 +
  112 + # generates a random code string consisting of 36 characters in the ranges
  113 + # a-z and 0-9
111 114 def generate_code
112 115 chars = ('a'..'z').to_a + ('0'..'9').to_a
113 116 code = ""
... ... @@ -116,6 +119,15 @@ class Task &lt; ActiveRecord::Base
116 119 end
117 120 code
118 121 end
  122 +
  123 + # finds a task by its (generated) code. Only returns a task with the
  124 + # specified code AND with status = Task::Status::ACTIVE.
  125 + #
  126 + # Can be used in subclasses to find only their instances.
  127 + def find_by_code(code)
  128 + self.find(:first, :conditions => { :code => code, :status => Task::Status::ACTIVE })
  129 + end
  130 +
119 131 end
120 132  
121 133 end
... ...
test/unit/task_test.rb
... ... @@ -107,4 +107,22 @@ class TaskTest &lt; Test::Unit::TestCase
107 107 assert_equal 36, code.size
108 108 end
109 109  
  110 + should 'find only in active tasks' do
  111 + task = Task.new
  112 + task.requestor = User.create(:login => 'testfindinactivetask', :password => 'test', :password_confirmation => 'test', :email => 'testfindinactivetask@localhost.localdomain').person
  113 + task.save!
  114 +
  115 + task.cancel
  116 +
  117 + assert_nil Task.find_by_code(task.code)
  118 + end
  119 +
  120 + should 'be able to find active tasks ' do
  121 + task = Task.new
  122 + task.requestor = User.create(:login => 'testfindinactivetask', :password => 'test', :password_confirmation => 'test', :email => 'testfindinactivetask@localhost.localdomain').person
  123 + task.save!
  124 +
  125 + assert_not_nil Task.find_by_code(task.code)
  126 + end
  127 +
110 128 end
... ...