diff --git a/app/models/comment.rb b/app/models/comment.rb index 468ae23..55be515 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -62,8 +62,7 @@ class Comment < ActiveRecord::Base class Notifier < ActionMailer::Base def mail(comment) profile = comment.article.profile - # FIXME hardcoded - email = profile.person? ? profile.email : profile.contact_email + email = profile.notification_emails return unless email recipients email diff --git a/app/models/contact.rb b/app/models/contact.rb index 851ce37..03a65a0 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -22,7 +22,7 @@ class Contact < ActiveRecord::Base #WithoutTable class Sender < ActionMailer::Base def mail(contact) - emails = [contact.dest.contact_email] + contact.dest.admins.map{|i| i.email} + emails = contact.dest.notification_emails recipients emails from "#{contact.name} <#{contact.email}>" if contact.receive_a_copy diff --git a/app/models/organization.rb b/app/models/organization.rb index ee0a22a..5170439 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -90,4 +90,8 @@ class Organization < Profile ] end + def notification_emails + [contact_email].compact + admins.map(&:email) + end + end diff --git a/app/models/profile.rb b/app/models/profile.rb index f886225..1aab0e0 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -233,6 +233,12 @@ class Profile < ActiveRecord::Base raise NotImplementedError end + # This method must return a list of e-mail adresses to which notification messages must be sent. + # The implementation in this class just delegates to +contact_email+. Subclasse may override this method. + def notification_emails + [contact_email] + end + # gets recent documents in this profile, ordered from the most recent to the # oldest. # diff --git a/app/models/task_mailer.rb b/app/models/task_mailer.rb index 1d121e3..2e878e9 100644 --- a/app/models/task_mailer.rb +++ b/app/models/task_mailer.rb @@ -12,7 +12,7 @@ class TaskMailer < ActionMailer::Base def target_notification(task, message) msg = extract_message(message) - recipients task.target.contact_email + recipients task.target.notification_emails from self.class.generate_from(task) subject '[%s] %s' % [task.requestor.environment.name, task.description] @@ -37,7 +37,7 @@ class TaskMailer < ActionMailer::Base text = extract_message(message) - recipients task.requestor.email + recipients task.requestor.notification_emails from self.class.generate_from(task) subject '[%s] %s' % [task.requestor.environment.name, task.description] body :requestor => task.requestor.name, diff --git a/test/unit/organization_test.rb b/test/unit/organization_test.rb index b5546dd..b3a6e9c 100644 --- a/test/unit/organization_test.rb +++ b/test/unit/organization_test.rb @@ -96,6 +96,24 @@ class OrganizationTest < Test::Unit::TestCase assert !org.errors.invalid?(:contact_email) end + should 'list contact_email plus admin emails as "notification emails"' do + o = Organization.new(:contact_email => 'org@email.com') + admin1 = mock; admin1.stubs(:email).returns('admin1@email.com') + admin2 = mock; admin2.stubs(:email).returns('admin2@email.com') + o.stubs(:admins).returns([admin1, admin2]) + + assert_equal ['org@email.com', 'admin1@email.com', 'admin2@email.com'], o.notification_emails + end + + should 'list only admins if contact_email is blank' do + o = Organization.new(:contact_email => nil) + admin1 = mock; admin1.stubs(:email).returns('admin1@email.com') + admin2 = mock; admin2.stubs(:email).returns('admin2@email.com') + o.stubs(:admins).returns([admin1, admin2]) + + assert_equal ['admin1@email.com', 'admin2@email.com'], o.notification_emails + end + should 'list pending enterprise validations' do org = Organization.new assert_kind_of Array, org.pending_validations diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 67091a4..97e3f31 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -1139,6 +1139,12 @@ class ProfileTest < Test::Unit::TestCase end end + should 'delegate to contact_email to retrieve notification e-mails' do + p = Profile.new + p.stubs(:contact_email).returns('my@email.com') + assert_equal ['my@email.com'], p.notification_emails + end + private def assert_invalid_identifier(id) diff --git a/test/unit/task_mailer_test.rb b/test/unit/task_mailer_test.rb index e341c89..452e345 100644 --- a/test/unit/task_mailer_test.rb +++ b/test/unit/task_mailer_test.rb @@ -23,7 +23,7 @@ class TaskMailerTest < Test::Unit::TestCase task.expects(:description).returns('the task') requestor = mock() - requestor.expects(:email).returns('requestor@example.com') + requestor.expects(:notification_emails).returns(['requestor@example.com']) requestor.expects(:name).returns('my name') environment = mock() @@ -35,6 +35,7 @@ class TaskMailerTest < Test::Unit::TestCase requestor.expects(:environment).returns(environment).at_least_once TaskMailer.deliver_task_finished(task) + assert !ActionMailer::Base.deliveries.empty? end should 'be able to send a "task cancelled" message' do @@ -44,7 +45,7 @@ class TaskMailerTest < Test::Unit::TestCase task.expects(:description).returns('the task') requestor = mock() - requestor.expects(:email).returns('requestor@example.com') + requestor.expects(:notification_emails).returns(['requestor@example.com']) requestor.expects(:name).returns('my name') environment = mock() @@ -56,6 +57,7 @@ class TaskMailerTest < Test::Unit::TestCase requestor.expects(:environment).returns(environment).at_least_once TaskMailer.deliver_task_cancelled(task) + assert !ActionMailer::Base.deliveries.empty? end should 'be able to send a "task created" message' do @@ -66,7 +68,7 @@ class TaskMailerTest < Test::Unit::TestCase task.expects(:description).returns('the task') requestor = mock() - requestor.expects(:email).returns('requestor@example.com') + requestor.expects(:notification_emails).returns(['requestor@example.com']) requestor.expects(:name).returns('my name') environment = mock() @@ -78,6 +80,7 @@ class TaskMailerTest < Test::Unit::TestCase requestor.expects(:environment).returns(environment).at_least_once TaskMailer.deliver_task_created(task) + assert !ActionMailer::Base.deliveries.empty? end should 'be able to send a "target notification" message' do @@ -88,7 +91,7 @@ class TaskMailerTest < Test::Unit::TestCase requestor.expects(:name).returns('my name') target = mock() - target.expects(:contact_email).returns('target@example.com') + target.expects(:notification_emails).returns(['target@example.com']) target.expects(:name).returns('Target') environment = mock() @@ -101,6 +104,7 @@ class TaskMailerTest < Test::Unit::TestCase requestor.expects(:environment).returns(environment).at_least_once TaskMailer.deliver_target_notification(task, 'the message') + assert !ActionMailer::Base.deliveries.empty? end should 'use environment name and contact email' do -- libgit2 0.21.2