From 9bfd45df8d0e2ceb8d34805b3781fdfcd46262a8 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Tue, 2 Jun 2015 15:26:14 -0300 Subject: [PATCH] Store the person that closes a task --- app/controllers/my_profile/tasks_controller.rb | 2 +- app/models/task.rb | 20 ++++++++++++-------- db/migrate/20150602142030_add_closed_by_to_task.rb | 7 +++++++ test/functional/tasks_controller_test.rb | 6 ++++++ test/unit/task_test.rb | 14 ++++++++++++++ 5 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20150602142030_add_closed_by_to_task.rb diff --git a/app/controllers/my_profile/tasks_controller.rb b/app/controllers/my_profile/tasks_controller.rb index 6296dc3..78dcee3 100644 --- a/app/controllers/my_profile/tasks_controller.rb +++ b/app/controllers/my_profile/tasks_controller.rb @@ -49,7 +49,7 @@ class TasksController < MyProfileController task = profile.find_in_all_tasks(id) begin task.update_attributes(value[:task]) - task.send(decision) + task.send(decision, current_person) rescue Exception => ex message = "#{task.title} (#{task.requestor ? task.requestor.name : task.author_name})" failed[ex.message] ? failed[ex.message] << message : failed[ex.message] = [message] diff --git a/app/models/task.rb b/app/models/task.rb index 1400e3e..927031d 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -34,6 +34,7 @@ class Task < ActiveRecord::Base belongs_to :requestor, :class_name => 'Profile', :foreign_key => :requestor_id belongs_to :target, :foreign_key => :target_id, :polymorphic => true belongs_to :responsible, :class_name => 'Person', :foreign_key => :responsible_id + belongs_to :closed_by, :class_name => 'Person', :foreign_key => :closed_by_id validates_uniqueness_of :code, :on => :create validates_presence_of :code @@ -77,11 +78,9 @@ class Task < ActiveRecord::Base # this method finished the task. It calls #perform, which must be overriden # by subclasses. At the end a message (as returned by #finish_message) is # sent to the requestor with #notify_requestor. - def finish + def finish(closed_by=nil) transaction do - self.status = Task::Status::FINISHED - self.end_date = Time.now - self.save! + close(Task::Status::FINISHED, closed_by) self.perform begin send_notification(:finished) @@ -106,11 +105,9 @@ class Task < ActiveRecord::Base # this method cancels the task. At the end a message (as returned by # #cancel_message) is sent to the requestor with #notify_requestor. - def cancel + def cancel(closed_by=nil) transaction do - self.status = Task::Status::CANCELLED - self.end_date = Time.now - self.save! + close(Task::Status::CANCELLED, closed_by) begin send_notification(:cancelled) rescue NotImplementedError => ex @@ -119,6 +116,13 @@ class Task < ActiveRecord::Base end end + def close(status, closed_by) + self.status = status + self.end_date = Time.now + self.closed_by = closed_by + self.save! + end + # Here are the tasks customizable options. def title diff --git a/db/migrate/20150602142030_add_closed_by_to_task.rb b/db/migrate/20150602142030_add_closed_by_to_task.rb new file mode 100644 index 0000000..1d7735a --- /dev/null +++ b/db/migrate/20150602142030_add_closed_by_to_task.rb @@ -0,0 +1,7 @@ +class AddClosedByToTask < ActiveRecord::Migration + + def change + add_column :tasks, :closed_by_id, :integer + end + +end diff --git a/test/functional/tasks_controller_test.rb b/test/functional/tasks_controller_test.rb index ed943d2..49d0d6d 100644 --- a/test/functional/tasks_controller_test.rb +++ b/test/functional/tasks_controller_test.rb @@ -628,6 +628,12 @@ class TasksControllerTest < ActionController::TestCase assert_select ".task_responsible select", 0 assert_select ".task_responsible .value" + end + + should 'store the person who closes a task' do + t = profile.tasks.build; t.save! + post :close, :tasks => {t.id => {:decision => 'finish', :task => {}}} + assert_equal profile, t.reload.closed_by end end diff --git a/test/unit/task_test.rb b/test/unit/task_test.rb index 16448ff..00388e3 100644 --- a/test/unit/task_test.rb +++ b/test/unit/task_test.rb @@ -440,6 +440,20 @@ class TaskTest < ActiveSupport::TestCase assert_equal person, task.responsible end + should 'store who finish the task' do + t = Task.create + person = fast_create(Person) + t.finish(person) + assert_equal person, t.reload.closed_by + end + + should 'store who cancel the task' do + t = Task.create + person = fast_create(Person) + t.cancel(person) + assert_equal person, t.reload.closed_by + end + protected def sample_user -- libgit2 0.21.2