Commit c4699cd6ffc32f63c3c489f54dbbdbaf893b3510

Authored by AntonioTerceiro
1 parent b9d996f4

ActionItem16: merging work done at Recife airport, waiting for the

flight.

As usual, keeping the local commits' log messages below so they are
referenced in the corresponding items.

r751@cabula:  terceiro | 2007-10-20 06:50:14 +0000
ActionItem16: making Task optionally send a message to the task target
upon task creation

r752@cabula:  terceiro | 2007-10-20 06:52:54 +0000
ActionItem16: refactoring: extract method of code that handles different types of messages

r753@cabula:  terceiro | 2007-10-20 07:36:30 +0000
ActionItem16: actually send e-mail

r838@cabula:  terceiro | 2007-10-21 14:50:35 +0000
ActionItem16: documenting the fact that we need ruby-gettext at least version 1.9.0

r839@cabula:  terceiro | 2007-10-21 14:51:25 +0000
ActionItem16: adding test for missing task


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@805 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/task.rb
... ... @@ -46,6 +46,11 @@ class Task < ActiveRecord::Base
46 46  
47 47 after_create do |task|
48 48 task.send(:send_notification, :created)
  49 +
  50 + target_msg = task.target_notification_message
  51 + unless target_msg.nil?
  52 + TaskMailer.deliver_target_notification(self, target_msg)
  53 + end
49 54 end
50 55  
51 56 # this method finished the task. It calls #perform, which must be overriden
... ... @@ -100,6 +105,16 @@ class Task < ActiveRecord::Base
100 105 _("The task was cancelled at %s") % (self.end_date.to_s)
101 106 end
102 107  
  108 + # The message that will be sent to the *target* on the task when it is
  109 + # created.
  110 + #
  111 + # The implementation in this class returns +nil+, what makes the notification
  112 + # not to be sent. If you want to send a notification to the target upon task
  113 + # creation, override this method and return a String.
  114 + def target_notification_message
  115 + nil
  116 + end
  117 +
103 118 protected
104 119  
105 120 # This method must be overrided in subclasses, and its implementation must do
... ...
app/models/task_mailer.rb
... ... @@ -9,16 +9,33 @@ class TaskMailer < ActionMailer::Base
9 9 end
10 10 end
11 11  
  12 + def target_notification(task, message)
  13 + msg = extract_message(message)
  14 +
  15 + recipients task.target.contact_email
  16 +
  17 + from task.requestor.environment.contact_email
  18 + subject task.description
  19 + body :requestor => task.requestor.name,
  20 + :target => task.target.name,
  21 + :message => msg,
  22 + :environment => task.requestor.environment.name,
  23 + :url => url_for(:host => task.requestor.environment.default_hostname, :controller => 'home')
  24 + end
  25 +
12 26 protected
13 27  
  28 + def extract_message(message)
  29 + if message.kind_of?(Proc)
  30 + self.instance_eval(&message)
  31 + else
  32 + message.to_s
  33 + end
  34 + end
  35 +
14 36 def send_message(task, message)
15 37  
16   - text =
17   - if message.kind_of?(Proc)
18   - self.instance_eval(&message)
19   - else
20   - message
21   - end
  38 + text = extract_message(message)
22 39  
23 40 recipients task.requestor.email
24 41 from task.requestor.environment.contact_email
... ...
app/views/task_mailer/target_notification.rhtml 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +Dear <%= @target %>,
  2 +
  3 +<%= @message %>
  4 +
  5 +--
  6 +<%= _('%s environment system') % @environment %>
  7 +<%= @url %>
... ...
doc/README_FOR_APP
... ... @@ -11,7 +11,7 @@ You need to have a Subversion client (svn) installed, as well as:
11 11  
12 12 * Ruby: http://www.ruby-lang.org/
13 13 * Rake: http://rake.rubyforge.org/
14   -* Ruby-GetText: http://www.yotabanana.com/hiki/ruby-gettext.html?ruby-gettext
  14 +* Ruby-GetText: http://www.yotabanana.com/hiki/ruby-gettext.html?ruby-gettext (at least version 1.9.0)
15 15 * Mocha: http://mocha.rubyforge.org/
16 16 * Ruby-sqlite3: http://rubyforge.org/projects/sqlite-ruby
17 17 * rcov: http://eigenclass.org/hiki/rcov
... ...
test/unit/profile_test.rb
... ... @@ -144,6 +144,10 @@ class ProfileTest &lt; Test::Unit::TestCase
144 144 assert_equal doc1.id, docs.first.id
145 145 end
146 146  
  147 + should 'provide a contact_email method which returns a ... contact email address' do
  148 + flunk 'not implemented yet'
  149 + end
  150 +
147 151 private
148 152  
149 153 def assert_invalid_identifier(id)
... ...
test/unit/task_mailer_test.rb
... ... @@ -80,6 +80,29 @@ class TaskMailerTest &lt; Test::Unit::TestCase
80 80 TaskMailer.deliver_task_created(task)
81 81 end
82 82  
  83 + should 'be able to send a "target notification" message' do
  84 + task = Task.new
  85 + task.expects(:description).returns('the task')
  86 +
  87 + requestor = mock()
  88 + requestor.expects(:name).returns('my name')
  89 +
  90 + target = mock()
  91 + target.expects(:contact_email).returns('target@example.com')
  92 + target.expects(:name).returns('Target')
  93 +
  94 + environment = mock()
  95 + environment.expects(:contact_email).returns('sender@example.com')
  96 + environment.expects(:default_hostname).returns('example.com')
  97 + environment.expects(:name).returns('example')
  98 +
  99 + task.expects(:requestor).returns(requestor).at_least_once
  100 + task.expects(:target).returns(target).at_least_once
  101 + requestor.expects(:environment).returns(environment).at_least_once
  102 +
  103 + TaskMailer.deliver_target_notification(task, 'the message')
  104 + end
  105 +
83 106  
84 107 private
85 108 def read_fixture(action)
... ...
test/unit/task_test.rb
... ... @@ -135,6 +135,20 @@ class TaskTest &lt; Test::Unit::TestCase
135 135 assert_not_nil Task.find_by_code(task.code)
136 136 end
137 137  
  138 + should 'not send notification to target when target_notification_message is nil (as in Task base class)' do
  139 + task = Task.new
  140 + TaskMailer.expects(:deliver_target_notification).never
  141 + task.save!
  142 + assert_nil task.target_notification_message
  143 + end
  144 +
  145 + should 'send notification to target just after task creation' do
  146 + task = Task.new
  147 + task.stubs(:target_notification_message).returns('some non nil message to be sent to target')
  148 + TaskMailer.expects(:deliver_target_notification).once
  149 + task.save!
  150 + end
  151 +
138 152 protected
139 153  
140 154 def sample_user
... ...