Commit f4395fae01a6b86eb06123e8317e5cba95312f8c
1 parent
34043d4f
Exists in
master
and in
29 other branches
ActionItem78: generating unique codes for tasks
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@636 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
3 changed files
with
40 additions
and
3 deletions
Show diff stats
app/models/task.rb
| @@ -24,11 +24,23 @@ class Task < ActiveRecord::Base | @@ -24,11 +24,23 @@ class Task < ActiveRecord::Base | ||
| 24 | belongs_to :requestor, :class_name => 'Person', :foreign_key => :requestor_id | 24 | belongs_to :requestor, :class_name => 'Person', :foreign_key => :requestor_id |
| 25 | belongs_to :target, :class_name => 'Profile', :foreign_key => :target_id | 25 | belongs_to :target, :class_name => 'Profile', :foreign_key => :target_id |
| 26 | 26 | ||
| 27 | + validates_uniqueness_of :code | ||
| 28 | + validates_presence_of :code | ||
| 29 | + | ||
| 27 | def initialize(*args) | 30 | def initialize(*args) |
| 28 | super | 31 | super |
| 29 | self.status ||= Task::Status::ACTIVE | 32 | self.status ||= Task::Status::ACTIVE |
| 30 | end | 33 | end |
| 31 | 34 | ||
| 35 | + before_validation_on_create do |task| | ||
| 36 | + if task.code.nil? | ||
| 37 | + task.code = Task.generate_code | ||
| 38 | + while (Task.find_by_code(task.code)) | ||
| 39 | + task.code = Task.generate_code | ||
| 40 | + end | ||
| 41 | + end | ||
| 42 | + end | ||
| 43 | + | ||
| 32 | after_create do |task| | 44 | after_create do |task| |
| 33 | TaskMailer.deliver_task_created(task) | 45 | TaskMailer.deliver_task_created(task) |
| 34 | end | 46 | end |
| @@ -95,4 +107,15 @@ class Task < ActiveRecord::Base | @@ -95,4 +107,15 @@ class Task < ActiveRecord::Base | ||
| 95 | def perform | 107 | def perform |
| 96 | end | 108 | end |
| 97 | 109 | ||
| 110 | + class << self | ||
| 111 | + def generate_code | ||
| 112 | + chars = ('a'..'z').to_a + ('0'..'9').to_a | ||
| 113 | + code = "" | ||
| 114 | + chars.size.times do |n| | ||
| 115 | + code << chars[rand(chars.size)] | ||
| 116 | + end | ||
| 117 | + code | ||
| 118 | + end | ||
| 119 | + end | ||
| 120 | + | ||
| 98 | end | 121 | end |
db/migrate/017_create_tasks.rb
| @@ -8,7 +8,7 @@ class CreateTasks < ActiveRecord::Migration | @@ -8,7 +8,7 @@ class CreateTasks < ActiveRecord::Migration | ||
| 8 | t.column :requestor_id, :integer | 8 | t.column :requestor_id, :integer |
| 9 | t.column :target_id, :integer | 9 | t.column :target_id, :integer |
| 10 | 10 | ||
| 11 | - t.column :hash, :string | 11 | + t.column :code, :string, :limit => 40 |
| 12 | end | 12 | end |
| 13 | end | 13 | end |
| 14 | 14 |
test/unit/task_test.rb
| @@ -88,9 +88,23 @@ class TaskTest < Test::Unit::TestCase | @@ -88,9 +88,23 @@ class TaskTest < Test::Unit::TestCase | ||
| 88 | task.save! | 88 | task.save! |
| 89 | end | 89 | end |
| 90 | 90 | ||
| 91 | - should 'generate a random hash when creating' do | ||
| 92 | - flunk 'not implemented yet' | 91 | + should 'generate a random code before validation' do |
| 92 | + Task.expects(:generate_code) | ||
| 93 | + Task.new.valid? | ||
| 93 | end | 94 | end |
| 94 | 95 | ||
| 96 | + should 'make sure that codes are unique' do | ||
| 97 | + task1 = Task.create! | ||
| 98 | + task2 = Task.new(:code => task1.code) | ||
| 99 | + | ||
| 100 | + assert !task2.valid? | ||
| 101 | + assert task2.errors.invalid?(:code) | ||
| 102 | + end | ||
| 103 | + | ||
| 104 | + should 'generate a code with chars from a-z and 0-9' do | ||
| 105 | + code = Task.generate_code | ||
| 106 | + assert(code =~ /^[a-z0-9]+$/) | ||
| 107 | + assert_equal 36, code.size | ||
| 108 | + end | ||
| 95 | 109 | ||
| 96 | end | 110 | end |