Commit b8f289507cc6d336c8b09d438055b353181ca96a
1 parent
9d7da7aa
Exists in
master
and in
22 other branches
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,6 +108,9 @@ class Task < ActiveRecord::Base | ||
| 108 | end | 108 | end |
| 109 | 109 | ||
| 110 | class << self | 110 | class << self |
| 111 | + | ||
| 112 | + # generates a random code string consisting of 36 characters in the ranges | ||
| 113 | + # a-z and 0-9 | ||
| 111 | def generate_code | 114 | def generate_code |
| 112 | chars = ('a'..'z').to_a + ('0'..'9').to_a | 115 | chars = ('a'..'z').to_a + ('0'..'9').to_a |
| 113 | code = "" | 116 | code = "" |
| @@ -116,6 +119,15 @@ class Task < ActiveRecord::Base | @@ -116,6 +119,15 @@ class Task < ActiveRecord::Base | ||
| 116 | end | 119 | end |
| 117 | code | 120 | code |
| 118 | end | 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 | end | 131 | end |
| 120 | 132 | ||
| 121 | end | 133 | end |
test/unit/task_test.rb
| @@ -107,4 +107,22 @@ class TaskTest < Test::Unit::TestCase | @@ -107,4 +107,22 @@ class TaskTest < Test::Unit::TestCase | ||
| 107 | assert_equal 36, code.size | 107 | assert_equal 36, code.size |
| 108 | end | 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 | end | 128 | end |