Commit 9bfd45df8d0e2ceb8d34805b3781fdfcd46262a8

Authored by Victor Costa
Committed by Leandro Santos
1 parent 4de02a20

Store the person that closes a task

app/controllers/my_profile/tasks_controller.rb
@@ -49,7 +49,7 @@ class TasksController < MyProfileController @@ -49,7 +49,7 @@ class TasksController < MyProfileController
49 task = profile.find_in_all_tasks(id) 49 task = profile.find_in_all_tasks(id)
50 begin 50 begin
51 task.update_attributes(value[:task]) 51 task.update_attributes(value[:task])
52 - task.send(decision) 52 + task.send(decision, current_person)
53 rescue Exception => ex 53 rescue Exception => ex
54 message = "#{task.title} (#{task.requestor ? task.requestor.name : task.author_name})" 54 message = "#{task.title} (#{task.requestor ? task.requestor.name : task.author_name})"
55 failed[ex.message] ? failed[ex.message] << message : failed[ex.message] = [message] 55 failed[ex.message] ? failed[ex.message] << message : failed[ex.message] = [message]
app/models/task.rb
@@ -34,6 +34,7 @@ class Task &lt; ActiveRecord::Base @@ -34,6 +34,7 @@ class Task &lt; ActiveRecord::Base
34 belongs_to :requestor, :class_name => 'Profile', :foreign_key => :requestor_id 34 belongs_to :requestor, :class_name => 'Profile', :foreign_key => :requestor_id
35 belongs_to :target, :foreign_key => :target_id, :polymorphic => true 35 belongs_to :target, :foreign_key => :target_id, :polymorphic => true
36 belongs_to :responsible, :class_name => 'Person', :foreign_key => :responsible_id 36 belongs_to :responsible, :class_name => 'Person', :foreign_key => :responsible_id
  37 + belongs_to :closed_by, :class_name => 'Person', :foreign_key => :closed_by_id
37 38
38 validates_uniqueness_of :code, :on => :create 39 validates_uniqueness_of :code, :on => :create
39 validates_presence_of :code 40 validates_presence_of :code
@@ -77,11 +78,9 @@ class Task &lt; ActiveRecord::Base @@ -77,11 +78,9 @@ class Task &lt; ActiveRecord::Base
77 # this method finished the task. It calls #perform, which must be overriden 78 # this method finished the task. It calls #perform, which must be overriden
78 # by subclasses. At the end a message (as returned by #finish_message) is 79 # by subclasses. At the end a message (as returned by #finish_message) is
79 # sent to the requestor with #notify_requestor. 80 # sent to the requestor with #notify_requestor.
80 - def finish 81 + def finish(closed_by=nil)
81 transaction do 82 transaction do
82 - self.status = Task::Status::FINISHED  
83 - self.end_date = Time.now  
84 - self.save! 83 + close(Task::Status::FINISHED, closed_by)
85 self.perform 84 self.perform
86 begin 85 begin
87 send_notification(:finished) 86 send_notification(:finished)
@@ -106,11 +105,9 @@ class Task &lt; ActiveRecord::Base @@ -106,11 +105,9 @@ class Task &lt; ActiveRecord::Base
106 105
107 # this method cancels the task. At the end a message (as returned by 106 # this method cancels the task. At the end a message (as returned by
108 # #cancel_message) is sent to the requestor with #notify_requestor. 107 # #cancel_message) is sent to the requestor with #notify_requestor.
109 - def cancel 108 + def cancel(closed_by=nil)
110 transaction do 109 transaction do
111 - self.status = Task::Status::CANCELLED  
112 - self.end_date = Time.now  
113 - self.save! 110 + close(Task::Status::CANCELLED, closed_by)
114 begin 111 begin
115 send_notification(:cancelled) 112 send_notification(:cancelled)
116 rescue NotImplementedError => ex 113 rescue NotImplementedError => ex
@@ -119,6 +116,13 @@ class Task &lt; ActiveRecord::Base @@ -119,6 +116,13 @@ class Task &lt; ActiveRecord::Base
119 end 116 end
120 end 117 end
121 118
  119 + def close(status, closed_by)
  120 + self.status = status
  121 + self.end_date = Time.now
  122 + self.closed_by = closed_by
  123 + self.save!
  124 + end
  125 +
122 # Here are the tasks customizable options. 126 # Here are the tasks customizable options.
123 127
124 def title 128 def title
db/migrate/20150602142030_add_closed_by_to_task.rb 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +class AddClosedByToTask < ActiveRecord::Migration
  2 +
  3 + def change
  4 + add_column :tasks, :closed_by_id, :integer
  5 + end
  6 +
  7 +end
test/functional/tasks_controller_test.rb
@@ -628,6 +628,12 @@ class TasksControllerTest &lt; ActionController::TestCase @@ -628,6 +628,12 @@ class TasksControllerTest &lt; ActionController::TestCase
628 628
629 assert_select ".task_responsible select", 0 629 assert_select ".task_responsible select", 0
630 assert_select ".task_responsible .value" 630 assert_select ".task_responsible .value"
  631 + end
  632 +
  633 + should 'store the person who closes a task' do
  634 + t = profile.tasks.build; t.save!
  635 + post :close, :tasks => {t.id => {:decision => 'finish', :task => {}}}
  636 + assert_equal profile, t.reload.closed_by
631 end 637 end
632 638
633 end 639 end
test/unit/task_test.rb
@@ -440,6 +440,20 @@ class TaskTest &lt; ActiveSupport::TestCase @@ -440,6 +440,20 @@ class TaskTest &lt; ActiveSupport::TestCase
440 assert_equal person, task.responsible 440 assert_equal person, task.responsible
441 end 441 end
442 442
  443 + should 'store who finish the task' do
  444 + t = Task.create
  445 + person = fast_create(Person)
  446 + t.finish(person)
  447 + assert_equal person, t.reload.closed_by
  448 + end
  449 +
  450 + should 'store who cancel the task' do
  451 + t = Task.create
  452 + person = fast_create(Person)
  453 + t.cancel(person)
  454 + assert_equal person, t.reload.closed_by
  455 + end
  456 +
443 protected 457 protected
444 458
445 def sample_user 459 def sample_user