From ec22524b445e88e7574daf191f0ebfb142d6d3a5 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Fri, 12 Oct 2007 23:39:45 +0000 Subject: [PATCH] ActionItem14: modularizing notification sending to check for an existing requestor. Also extended the system to allow other types of notification just by defining a message with the proper name --- app/models/task.rb | 35 +++++++++++++++++++++-------------- app/models/task_mailer.rb | 17 +++++++---------- test/unit/task_mailer_test.rb | 13 +++++++------ test/unit/task_test.rb | 20 ++++++++++++++++++-- 4 files changed, 53 insertions(+), 32 deletions(-) diff --git a/app/models/task.rb b/app/models/task.rb index 724ccf0..71adf58 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -45,7 +45,7 @@ class Task < ActiveRecord::Base end after_create do |task| - TaskMailer.deliver_task_created(task) + task.send(:send_notification, :created) end # this method finished the task. It calls #perform, which must be overriden @@ -57,7 +57,7 @@ class Task < ActiveRecord::Base self.end_date = Time.now self.save! self.perform - TaskMailer.deliver_task_finished(self) + send_notification(:finished) end end @@ -68,36 +68,38 @@ class Task < ActiveRecord::Base self.status = Task::Status::CANCELLED self.end_date = Time.now self.save! - TaskMailer.deliver_task_cancelled(self) + send_notification(:cancelled) end end + + # Returns the description of the task. + # + # This method +must+ be overriden in subclasses to return something + # meaningful for each kind of task + def description + _('Generic task') + end + # The message that will be sent to the requestor of the task when the task is # created. - def create_message + def task_created_message + # FIXME: use a date properly recorded. _("The task was created at %s") % Time.now end # The message that will be sent to the requestor of the task when its # finished. - def finish_message + def task_finished_message _("The task was finished at %s") % (self.end_date.to_s) end # The message that will be sent to the requestor of the task when its # cancelled. - def cancel_message + def task_cancelled_message _("The task was cancelled at %s") % (self.end_date.to_s) end - # Returns the description of the task. - # - # This method +must+ be overriden in subclasses to return something - # meaningful for each kind of task - def description - _('Generic task') - end - protected # This method must be overrided in subclasses, and its implementation must do @@ -110,6 +112,11 @@ class Task < ActiveRecord::Base def perform end + # sends notification e-mail about a task, if the task has a requestor. + def send_notification(action) + TaskMailer.send("deliver_task_#{action}", self) if self.requestor + end + class << self # generates a random code string consisting of 36 characters in the ranges diff --git a/app/models/task_mailer.rb b/app/models/task_mailer.rb index 18b5782..3fbb48b 100644 --- a/app/models/task_mailer.rb +++ b/app/models/task_mailer.rb @@ -1,15 +1,12 @@ class TaskMailer < ActionMailer::Base - def task_finished(task) - send_message(task, task.finish_message) - end - - def task_created(task) - send_message(task, task.create_message) - end - - def task_cancelled(task) - send_message(task, task.cancel_message) + def method_missing(name, *args) + task = args.shift + if task.kind_of?(Task) && task.respond_to?("#{name}_message") + send_message(task, task.send("#{name}_message"), *args) + else + super + end end protected diff --git a/test/unit/task_mailer_test.rb b/test/unit/task_mailer_test.rb index 60c3cc9..a9f49ac 100644 --- a/test/unit/task_mailer_test.rb +++ b/test/unit/task_mailer_test.rb @@ -18,8 +18,8 @@ class TaskMailerTest < Test::Unit::TestCase should 'be able to send a "task finished" message' do - task = mock() - task.expects(:finish_message).returns('the message') + task = Task.new + task.expects(:task_finished_message).returns('the message') task.expects(:description).returns('the task') requestor = mock() @@ -39,8 +39,8 @@ class TaskMailerTest < Test::Unit::TestCase should 'be able to send a "task cancelled" message' do - task = mock() - task.expects(:cancel_message).returns('the message') + task = Task.new + task.expects(:task_cancelled_message).returns('the message') task.expects(:description).returns('the task') requestor = mock() @@ -60,8 +60,9 @@ class TaskMailerTest < Test::Unit::TestCase should 'be able to send a "task created" message' do - task = mock() - task.expects(:create_message).returns('the message') + task = Task.new + + task.expects(:task_created_message).returns('the message') task.expects(:description).returns('the task') requestor = mock() diff --git a/test/unit/task_test.rb b/test/unit/task_test.rb index 7067732..98632cb 100644 --- a/test/unit/task_test.rb +++ b/test/unit/task_test.rb @@ -33,6 +33,7 @@ class TaskTest < Test::Unit::TestCase def test_should_call_perform_in_finish TaskMailer.expects(:deliver_task_finished) t = Task.create + t.requestor = sample_user t.expects(:perform) t.finish assert_equal Task::Status::FINISHED, t.status @@ -41,6 +42,7 @@ class TaskTest < Test::Unit::TestCase def test_should_have_cancelled_status_after_cancel TaskMailer.expects(:deliver_task_cancelled) t = Task.create + t.requestor = sample_user t.cancel assert_equal Task::Status::CANCELLED, t.status end @@ -52,13 +54,19 @@ class TaskTest < Test::Unit::TestCase def test_should_notify_finish t = Task.create + t.requestor = sample_user + TaskMailer.expects(:deliver_task_finished).with(t) + t.finish end def test_should_notify_cancel t = Task.create + t.requestor = sample_user + TaskMailer.expects(:deliver_task_cancelled).with(t) + t.cancel end @@ -84,6 +92,8 @@ class TaskTest < Test::Unit::TestCase should 'notify just after the task is created' do task = Task.new + task.requestor = sample_user + TaskMailer.expects(:deliver_task_created).with(task) task.save! end @@ -109,7 +119,7 @@ class TaskTest < Test::Unit::TestCase should 'find only in active tasks' do task = Task.new - task.requestor = User.create(:login => 'testfindinactivetask', :password => 'test', :password_confirmation => 'test', :email => 'testfindinactivetask@localhost.localdomain').person + task.requestor = sample_user task.save! task.cancel @@ -119,10 +129,16 @@ class TaskTest < Test::Unit::TestCase should 'be able to find active tasks ' do task = Task.new - task.requestor = User.create(:login => 'testfindinactivetask', :password => 'test', :password_confirmation => 'test', :email => 'testfindinactivetask@localhost.localdomain').person + task.requestor = sample_user task.save! assert_not_nil Task.find_by_code(task.code) end + protected + + def sample_user + User.create(:login => 'testfindinactivetask', :password => 'test', :password_confirmation => 'test', :email => 'testfindinactivetask@localhost.localdomain').person + end + end -- libgit2 0.21.2