diff --git a/app/models/task.rb b/app/models/task.rb index 4311443..7cd55a3 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -46,6 +46,11 @@ class Task < ActiveRecord::Base after_create do |task| task.send(:send_notification, :created) + + target_msg = task.target_notification_message + unless target_msg.nil? + TaskMailer.deliver_target_notification(self, target_msg) + end end # this method finished the task. It calls #perform, which must be overriden @@ -100,6 +105,16 @@ class Task < ActiveRecord::Base _("The task was cancelled at %s") % (self.end_date.to_s) end + # The message that will be sent to the *target* on the task when it is + # created. + # + # 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 + # creation, override this method and return a String. + def target_notification_message + nil + end + protected # This method must be overrided in subclasses, and its implementation must do diff --git a/app/models/task_mailer.rb b/app/models/task_mailer.rb index 3fbb48b..2109b16 100644 --- a/app/models/task_mailer.rb +++ b/app/models/task_mailer.rb @@ -9,16 +9,33 @@ class TaskMailer < ActionMailer::Base end end + def target_notification(task, message) + msg = extract_message(message) + + recipients task.target.contact_email + + from task.requestor.environment.contact_email + subject task.description + body :requestor => task.requestor.name, + :target => task.target.name, + :message => msg, + :environment => task.requestor.environment.name, + :url => url_for(:host => task.requestor.environment.default_hostname, :controller => 'home') + end + protected + def extract_message(message) + if message.kind_of?(Proc) + self.instance_eval(&message) + else + message.to_s + end + end + def send_message(task, message) - text = - if message.kind_of?(Proc) - self.instance_eval(&message) - else - message - end + text = extract_message(message) recipients task.requestor.email from task.requestor.environment.contact_email diff --git a/app/views/task_mailer/target_notification.rhtml b/app/views/task_mailer/target_notification.rhtml new file mode 100644 index 0000000..f6b8b37 --- /dev/null +++ b/app/views/task_mailer/target_notification.rhtml @@ -0,0 +1,7 @@ +Dear <%= @target %>, + +<%= @message %> + +-- +<%= _('%s environment system') % @environment %> +<%= @url %> diff --git a/doc/README_FOR_APP b/doc/README_FOR_APP index 8cc7783..a160604 100644 --- a/doc/README_FOR_APP +++ b/doc/README_FOR_APP @@ -11,7 +11,7 @@ You need to have a Subversion client (svn) installed, as well as: * Ruby: http://www.ruby-lang.org/ * Rake: http://rake.rubyforge.org/ -* Ruby-GetText: http://www.yotabanana.com/hiki/ruby-gettext.html?ruby-gettext +* Ruby-GetText: http://www.yotabanana.com/hiki/ruby-gettext.html?ruby-gettext (at least version 1.9.0) * Mocha: http://mocha.rubyforge.org/ * Ruby-sqlite3: http://rubyforge.org/projects/sqlite-ruby * rcov: http://eigenclass.org/hiki/rcov diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 2ac3212..3e937dd 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -144,6 +144,10 @@ class ProfileTest < Test::Unit::TestCase assert_equal doc1.id, docs.first.id end + should 'provide a contact_email method which returns a ... contact email address' do + flunk 'not implemented yet' + end + private def assert_invalid_identifier(id) diff --git a/test/unit/task_mailer_test.rb b/test/unit/task_mailer_test.rb index a9f49ac..d6bd144 100644 --- a/test/unit/task_mailer_test.rb +++ b/test/unit/task_mailer_test.rb @@ -80,6 +80,29 @@ class TaskMailerTest < Test::Unit::TestCase TaskMailer.deliver_task_created(task) end + should 'be able to send a "target notification" message' do + task = Task.new + task.expects(:description).returns('the task') + + requestor = mock() + requestor.expects(:name).returns('my name') + + target = mock() + target.expects(:contact_email).returns('target@example.com') + target.expects(:name).returns('Target') + + environment = mock() + environment.expects(:contact_email).returns('sender@example.com') + environment.expects(:default_hostname).returns('example.com') + environment.expects(:name).returns('example') + + task.expects(:requestor).returns(requestor).at_least_once + task.expects(:target).returns(target).at_least_once + requestor.expects(:environment).returns(environment).at_least_once + + TaskMailer.deliver_target_notification(task, 'the message') + end + private def read_fixture(action) diff --git a/test/unit/task_test.rb b/test/unit/task_test.rb index 98632cb..b63bcb8 100644 --- a/test/unit/task_test.rb +++ b/test/unit/task_test.rb @@ -135,6 +135,20 @@ class TaskTest < Test::Unit::TestCase assert_not_nil Task.find_by_code(task.code) end + should 'not send notification to target when target_notification_message is nil (as in Task base class)' do + task = Task.new + TaskMailer.expects(:deliver_target_notification).never + task.save! + assert_nil task.target_notification_message + end + + should 'send notification to target just after task creation' do + task = Task.new + task.stubs(:target_notification_message).returns('some non nil message to be sent to target') + TaskMailer.expects(:deliver_target_notification).once + task.save! + end + protected def sample_user -- libgit2 0.21.2