From e6b525e9d27b51922ddceb7b6ca50a5cd981af0e Mon Sep 17 00:00:00 2001 From: Larissa Reis Date: Fri, 29 Jun 2012 16:20:12 -0300 Subject: [PATCH] Checks whether there is target email before sending notification --- app/models/task.rb | 12 +++++++----- test/unit/add_member_test.rb | 1 + test/unit/approve_article_test.rb | 1 + test/unit/task_test.rb | 20 ++++++++++++++++++-- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/models/task.rb b/app/models/task.rb index 5c461ec..f0dfa29 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -4,7 +4,7 @@ # # The specific types of tasks must override the #perform method, so # the actual action associated to the type of task can be performed. See the -# documentation of the #perform method for details. +# documentation of the #perform method for details. # # This class has a +data+ field of type text, where you can store any # type of data (as serialized Ruby objects) you need for your subclass (which @@ -64,7 +64,8 @@ class Task < ActiveRecord::Base begin target_msg = task.target_notification_message - TaskMailer.deliver_target_notification(task, target_msg) if target_msg + target_emails = task.target && task.target.notification_emails || [] + TaskMailer.deliver_target_notification(task, target_msg) if target_msg && !target_emails.empty? rescue NotImplementedError => ex RAILS_DEFAULT_LOGGER.info ex.to_s end @@ -192,7 +193,7 @@ class Task < ActiveRecord::Base # The message that will be sent to the *target* of the task when it is # created. The indent of this message is to notify the target about the - # request that was just created for him/her. + # request that was just created for him/her. # # The implementation in this class returns +nil+, what makes the notification # not to be sent. If you want to send a notification to the target upon task @@ -225,7 +226,8 @@ class Task < ActiveRecord::Base begin target_msg = target_notification_message - TaskMailer.deliver_target_notification(self, target_msg) if target_msg + target_emails = self.target && self.target.notification_emails || [] + TaskMailer.deliver_target_notification(self, target_msg) if target_msg && !target_emails.empty? rescue NotImplementedError => ex RAILS_DEFAULT_LOGGER.info ex.to_s end @@ -253,7 +255,7 @@ class Task < ActiveRecord::Base # sends notification e-mail about a task, if the task has a requestor. # - # If + # If def send_notification(action) if sends_email? if self.requestor diff --git a/test/unit/add_member_test.rb b/test/unit/add_member_test.rb index af6170e..b8aff54 100644 --- a/test/unit/add_member_test.rb +++ b/test/unit/add_member_test.rb @@ -54,6 +54,7 @@ class AddMemberTest < ActiveSupport::TestCase should 'send e-mails' do community.update_attribute(:closed, true) + community.stubs(:notification_emails).returns(["adm@example.com"]) TaskMailer.expects(:deliver_target_notification).at_least_once diff --git a/test/unit/approve_article_test.rb b/test/unit/approve_article_test.rb index e9fc784..852ab65 100644 --- a/test/unit/approve_article_test.rb +++ b/test/unit/approve_article_test.rb @@ -82,6 +82,7 @@ class ApproveArticleTest < ActiveSupport::TestCase should 'notify target if group is moderated' do community.moderated_articles = true community.save + community.stubs(:notification_emails).returns(['adm@example.com']) a = ApproveArticle.create!(:name => '', :article => article, :target => community, :requestor => profile) assert !ActionMailer::Base.deliveries.empty? diff --git a/test/unit/task_test.rb b/test/unit/task_test.rb index c7d204e..8f0d33f 100644 --- a/test/unit/task_test.rb +++ b/test/unit/task_test.rb @@ -128,7 +128,7 @@ class TaskTest < ActiveSupport::TestCase task = Task.new task.requestor = sample_user task.save! - + task.cancel assert_nil Task.find_by_code(task.code) @@ -160,6 +160,9 @@ class TaskTest < ActiveSupport::TestCase should 'send notification to target just after task creation' do task = Task.new + target = Profile.new + target.stubs(:notification_emails).returns(['adm@example.com']) + task.target = target task.stubs(:target_notification_message).returns('some non nil message to be sent to target') TaskMailer.expects(:deliver_target_notification).once task.save! @@ -204,7 +207,7 @@ class TaskTest < ActiveSupport::TestCase user.destroy end end - + should 'not deliver notification message to target' do task = Task.new assert_raise NotImplementedError do @@ -228,6 +231,16 @@ class TaskTest < ActiveSupport::TestCase task.save! end + should 'not notify target if notification emails is empty' do + task = Task.new + target = Profile.new + target.stubs(:notification_emails).returns([]) + task.target = target + task.stubs(:target_notification_message).returns('some non nil message to be sent to target') + TaskMailer.expects(:deliver_target_notification).never + task.save! + end + should 'the environment method be defined' do task = Task.new assert task.method_exists?('environment') @@ -266,6 +279,9 @@ class TaskTest < ActiveSupport::TestCase should 'send notification message to target just after task activation' do task = Task.new(:status => Task::Status::HIDDEN) + target = Profile.new + target.stubs(:notification_emails).returns(['target@example.com']) + task.target = target task.save! task.stubs(:target_notification_message).returns('some non nil message to be sent to target') TaskMailer.expects(:deliver_target_notification).once -- libgit2 0.21.2