Commit 83230b3bf7dc13db77ca1b31cc1674acb2793059

Authored by Antonio Terceiro
1 parent e2e0e99d

ActionItem628: extracting method notification_emails

Extracted the logic of getting "who should be notified
for a given profile" in its own method. Added a default implementation
for Profile and overrided it in Organization to list both the
contact_email entered and the administrators e-mails.
app/models/comment.rb
... ... @@ -62,8 +62,7 @@ class Comment < ActiveRecord::Base
62 62 class Notifier < ActionMailer::Base
63 63 def mail(comment)
64 64 profile = comment.article.profile
65   - # FIXME hardcoded
66   - email = profile.person? ? profile.email : profile.contact_email
  65 + email = profile.notification_emails
67 66 return unless email
68 67 recipients email
69 68  
... ...
app/models/contact.rb
... ... @@ -22,7 +22,7 @@ class Contact &lt; ActiveRecord::Base #WithoutTable
22 22  
23 23 class Sender < ActionMailer::Base
24 24 def mail(contact)
25   - emails = [contact.dest.contact_email] + contact.dest.admins.map{|i| i.email}
  25 + emails = contact.dest.notification_emails
26 26 recipients emails
27 27 from "#{contact.name} <#{contact.email}>"
28 28 if contact.receive_a_copy
... ...
app/models/organization.rb
... ... @@ -90,4 +90,8 @@ class Organization &lt; Profile
90 90 ]
91 91 end
92 92  
  93 + def notification_emails
  94 + [contact_email].compact + admins.map(&:email)
  95 + end
  96 +
93 97 end
... ...
app/models/profile.rb
... ... @@ -233,6 +233,12 @@ class Profile &lt; ActiveRecord::Base
233 233 raise NotImplementedError
234 234 end
235 235  
  236 + # This method must return a list of e-mail adresses to which notification messages must be sent.
  237 + # The implementation in this class just delegates to +contact_email+. Subclasse may override this method.
  238 + def notification_emails
  239 + [contact_email]
  240 + end
  241 +
236 242 # gets recent documents in this profile, ordered from the most recent to the
237 243 # oldest.
238 244 #
... ...
app/models/task_mailer.rb
... ... @@ -12,7 +12,7 @@ class TaskMailer &lt; ActionMailer::Base
12 12 def target_notification(task, message)
13 13 msg = extract_message(message)
14 14  
15   - recipients task.target.contact_email
  15 + recipients task.target.notification_emails
16 16  
17 17 from self.class.generate_from(task)
18 18 subject '[%s] %s' % [task.requestor.environment.name, task.description]
... ... @@ -37,7 +37,7 @@ class TaskMailer &lt; ActionMailer::Base
37 37  
38 38 text = extract_message(message)
39 39  
40   - recipients task.requestor.email
  40 + recipients task.requestor.notification_emails
41 41 from self.class.generate_from(task)
42 42 subject '[%s] %s' % [task.requestor.environment.name, task.description]
43 43 body :requestor => task.requestor.name,
... ...
test/unit/organization_test.rb
... ... @@ -96,6 +96,24 @@ class OrganizationTest &lt; Test::Unit::TestCase
96 96 assert !org.errors.invalid?(:contact_email)
97 97 end
98 98  
  99 + should 'list contact_email plus admin emails as "notification emails"' do
  100 + o = Organization.new(:contact_email => 'org@email.com')
  101 + admin1 = mock; admin1.stubs(:email).returns('admin1@email.com')
  102 + admin2 = mock; admin2.stubs(:email).returns('admin2@email.com')
  103 + o.stubs(:admins).returns([admin1, admin2])
  104 +
  105 + assert_equal ['org@email.com', 'admin1@email.com', 'admin2@email.com'], o.notification_emails
  106 + end
  107 +
  108 + should 'list only admins if contact_email is blank' do
  109 + o = Organization.new(:contact_email => nil)
  110 + admin1 = mock; admin1.stubs(:email).returns('admin1@email.com')
  111 + admin2 = mock; admin2.stubs(:email).returns('admin2@email.com')
  112 + o.stubs(:admins).returns([admin1, admin2])
  113 +
  114 + assert_equal ['admin1@email.com', 'admin2@email.com'], o.notification_emails
  115 + end
  116 +
99 117 should 'list pending enterprise validations' do
100 118 org = Organization.new
101 119 assert_kind_of Array, org.pending_validations
... ...
test/unit/profile_test.rb
... ... @@ -1139,6 +1139,12 @@ class ProfileTest &lt; Test::Unit::TestCase
1139 1139 end
1140 1140 end
1141 1141  
  1142 + should 'delegate to contact_email to retrieve notification e-mails' do
  1143 + p = Profile.new
  1144 + p.stubs(:contact_email).returns('my@email.com')
  1145 + assert_equal ['my@email.com'], p.notification_emails
  1146 + end
  1147 +
1142 1148 private
1143 1149  
1144 1150 def assert_invalid_identifier(id)
... ...
test/unit/task_mailer_test.rb
... ... @@ -23,7 +23,7 @@ class TaskMailerTest &lt; Test::Unit::TestCase
23 23 task.expects(:description).returns('the task')
24 24  
25 25 requestor = mock()
26   - requestor.expects(:email).returns('requestor@example.com')
  26 + requestor.expects(:notification_emails).returns(['requestor@example.com'])
27 27 requestor.expects(:name).returns('my name')
28 28  
29 29 environment = mock()
... ... @@ -35,6 +35,7 @@ class TaskMailerTest &lt; Test::Unit::TestCase
35 35 requestor.expects(:environment).returns(environment).at_least_once
36 36  
37 37 TaskMailer.deliver_task_finished(task)
  38 + assert !ActionMailer::Base.deliveries.empty?
38 39 end
39 40  
40 41 should 'be able to send a "task cancelled" message' do
... ... @@ -44,7 +45,7 @@ class TaskMailerTest &lt; Test::Unit::TestCase
44 45 task.expects(:description).returns('the task')
45 46  
46 47 requestor = mock()
47   - requestor.expects(:email).returns('requestor@example.com')
  48 + requestor.expects(:notification_emails).returns(['requestor@example.com'])
48 49 requestor.expects(:name).returns('my name')
49 50  
50 51 environment = mock()
... ... @@ -56,6 +57,7 @@ class TaskMailerTest &lt; Test::Unit::TestCase
56 57 requestor.expects(:environment).returns(environment).at_least_once
57 58  
58 59 TaskMailer.deliver_task_cancelled(task)
  60 + assert !ActionMailer::Base.deliveries.empty?
59 61 end
60 62  
61 63 should 'be able to send a "task created" message' do
... ... @@ -66,7 +68,7 @@ class TaskMailerTest &lt; Test::Unit::TestCase
66 68 task.expects(:description).returns('the task')
67 69  
68 70 requestor = mock()
69   - requestor.expects(:email).returns('requestor@example.com')
  71 + requestor.expects(:notification_emails).returns(['requestor@example.com'])
70 72 requestor.expects(:name).returns('my name')
71 73  
72 74 environment = mock()
... ... @@ -78,6 +80,7 @@ class TaskMailerTest &lt; Test::Unit::TestCase
78 80 requestor.expects(:environment).returns(environment).at_least_once
79 81  
80 82 TaskMailer.deliver_task_created(task)
  83 + assert !ActionMailer::Base.deliveries.empty?
81 84 end
82 85  
83 86 should 'be able to send a "target notification" message' do
... ... @@ -88,7 +91,7 @@ class TaskMailerTest &lt; Test::Unit::TestCase
88 91 requestor.expects(:name).returns('my name')
89 92  
90 93 target = mock()
91   - target.expects(:contact_email).returns('target@example.com')
  94 + target.expects(:notification_emails).returns(['target@example.com'])
92 95 target.expects(:name).returns('Target')
93 96  
94 97 environment = mock()
... ... @@ -101,6 +104,7 @@ class TaskMailerTest &lt; Test::Unit::TestCase
101 104 requestor.expects(:environment).returns(environment).at_least_once
102 105  
103 106 TaskMailer.deliver_target_notification(task, 'the message')
  107 + assert !ActionMailer::Base.deliveries.empty?
104 108 end
105 109  
106 110 should 'use environment name and contact email' do
... ...