Commit 4fdb1cf0afeffb67c519fc3405388164f3592ea4
1 parent
7c8e3b68
Exists in
master
and in
22 other branches
rails3: fix and rewrite task_mailer
Showing
2 changed files
with
40 additions
and
48 deletions
Show diff stats
app/models/task_mailer.rb
| 1 | 1 | class TaskMailer < ActionMailer::Base |
| 2 | 2 | |
| 3 | - def method_missing(name, *args) | |
| 4 | - task = args.shift | |
| 5 | - if task.kind_of?(Task) && task.respond_to?("#{name}_message") | |
| 6 | - send_message(task, task.send("#{name}_message"), *args) | |
| 7 | - else | |
| 8 | - super | |
| 9 | - end | |
| 10 | - end | |
| 11 | - | |
| 12 | 3 | def target_notification(task, message) |
| 13 | - msg = extract_message(message) | |
| 14 | - | |
| 15 | - recipients task.target.notification_emails | |
| 16 | - | |
| 4 | + @message = extract_message(message) | |
| 5 | + @target = task.target.name | |
| 6 | + @environment = task.environment.name | |
| 7 | + @url = generate_environment_url(task, :controller => 'home') | |
| 17 | 8 | url_for_tasks_list = task.target.kind_of?(Environment) ? '' : url_for(task.target.tasks_url) |
| 9 | + @tasks_url = url_for_tasks_list | |
| 18 | 10 | |
| 19 | - from self.class.generate_from(task) | |
| 20 | - subject '[%s] %s' % [task.environment.name, task.target_notification_description] | |
| 21 | - body :target => task.target.name, | |
| 22 | - :message => msg, | |
| 23 | - :environment => task.environment.name, | |
| 24 | - :url => generate_environment_url(task, :controller => 'home'), | |
| 25 | - :tasks_url => url_for_tasks_list | |
| 11 | + mail( | |
| 12 | + to: task.target.notification_emails, | |
| 13 | + from: self.class.generate_from(task), | |
| 14 | + subject: "[%s] %s" % [task.environment.name, task.target_notification_description] | |
| 15 | + ) | |
| 26 | 16 | end |
| 27 | 17 | |
| 28 | 18 | def invitation_notification(task) |
| 29 | 19 | msg = task.expanded_message |
| 30 | - msg = msg.gsub /<url>/, generate_environment_url(task, :controller => 'account', :action => 'signup', :invitation_code => task.code) | |
| 20 | + @message = msg.gsub /<url>/, generate_environment_url(task, :controller => 'account', :action => 'signup', :invitation_code => task.code) | |
| 31 | 21 | |
| 32 | - recipients task.friend_email | |
| 22 | + mail( | |
| 23 | + to: task.friend_email, | |
| 24 | + from: self.class.generate_from(task), | |
| 25 | + subject: '[%s] %s' % [ task.requestor.environment.name, task.target_notification_description ] | |
| 26 | + ) | |
| 27 | + end | |
| 33 | 28 | |
| 34 | - from self.class.generate_from(task) | |
| 35 | - subject '[%s] %s' % [ task.requestor.environment.name, task.target_notification_description ] | |
| 36 | - body :message => msg | |
| 29 | + def generic_message(name, task) | |
| 30 | + return if !task.respond_to?("#{name}_message") | |
| 31 | + | |
| 32 | + @message = extract_message(task.send("#{name}_message")) | |
| 33 | + @requestor = task.requestor.name | |
| 34 | + @environment = task.requestor.environment.name | |
| 35 | + @url = url_for(:host => task.requestor.environment.default_hostname, :controller => 'home') | |
| 36 | + | |
| 37 | + mail( | |
| 38 | + to: task.requestor.notification_emails, | |
| 39 | + from: self.class.generate_from(task), | |
| 40 | + subject: '[%s] %s' % [task.requestor.environment.name, task.target_notification_description] | |
| 41 | + ) | |
| 37 | 42 | end |
| 38 | 43 | |
| 39 | 44 | protected |
| ... | ... | @@ -46,19 +51,6 @@ class TaskMailer < ActionMailer::Base |
| 46 | 51 | end |
| 47 | 52 | end |
| 48 | 53 | |
| 49 | - def send_message(task, message) | |
| 50 | - | |
| 51 | - text = extract_message(message) | |
| 52 | - | |
| 53 | - recipients task.requestor.notification_emails | |
| 54 | - from self.class.generate_from(task) | |
| 55 | - subject '[%s] %s' % [task.requestor.environment.name, task.target_notification_description] | |
| 56 | - body :requestor => task.requestor.name, | |
| 57 | - :message => text, | |
| 58 | - :environment => task.requestor.environment.name, | |
| 59 | - :url => url_for(:host => task.requestor.environment.default_hostname, :controller => 'home') | |
| 60 | - end | |
| 61 | - | |
| 62 | 54 | def self.generate_from(task) |
| 63 | 55 | "#{task.environment.name} <#{task.environment.contact_email}>" |
| 64 | 56 | end | ... | ... |
test/unit/task_mailer_test.rb
| ... | ... | @@ -4,8 +4,6 @@ class TaskMailerTest < ActiveSupport::TestCase |
| 4 | 4 | FIXTURES_PATH = File.dirname(__FILE__) + '/../fixtures' |
| 5 | 5 | CHARSET = "utf-8" |
| 6 | 6 | |
| 7 | - include ActionMailer::Quoting | |
| 8 | - | |
| 9 | 7 | def setup |
| 10 | 8 | ActionMailer::Base.delivery_method = :test |
| 11 | 9 | ActionMailer::Base.perform_deliveries = true |
| ... | ... | @@ -32,7 +30,7 @@ class TaskMailerTest < ActiveSupport::TestCase |
| 32 | 30 | requestor.expects(:environment).returns(environment).at_least_once |
| 33 | 31 | task.expects(:environment).returns(environment).at_least_once |
| 34 | 32 | |
| 35 | - TaskMailer.deliver_task_finished(task) | |
| 33 | + TaskMailer.generic_message(:task_finished, task).deliver | |
| 36 | 34 | assert !ActionMailer::Base.deliveries.empty? |
| 37 | 35 | end |
| 38 | 36 | |
| ... | ... | @@ -55,7 +53,7 @@ class TaskMailerTest < ActiveSupport::TestCase |
| 55 | 53 | requestor.expects(:environment).returns(environment).at_least_once |
| 56 | 54 | task.expects(:environment).returns(environment).at_least_once |
| 57 | 55 | |
| 58 | - TaskMailer.deliver_task_cancelled(task) | |
| 56 | + TaskMailer.generic_message(:task_cancelled, task).deliver | |
| 59 | 57 | assert !ActionMailer::Base.deliveries.empty? |
| 60 | 58 | end |
| 61 | 59 | |
| ... | ... | @@ -79,15 +77,17 @@ class TaskMailerTest < ActiveSupport::TestCase |
| 79 | 77 | requestor.expects(:environment).returns(environment).at_least_once |
| 80 | 78 | task.expects(:environment).returns(environment).at_least_once |
| 81 | 79 | |
| 82 | - TaskMailer.deliver_task_created(task) | |
| 80 | + TaskMailer.generic_message(:task_created, task).deliver | |
| 83 | 81 | assert !ActionMailer::Base.deliveries.empty? |
| 84 | 82 | end |
| 85 | 83 | |
| 86 | 84 | should 'be able to send a "target notification" message' do |
| 87 | - task = Task.new(:target => fast_create(Person)) | |
| 85 | + requestor = fast_create(Person) | |
| 86 | + requestor.expects(:notification_emails).returns(['requestor@example.com']) | |
| 87 | + task = Task.new(:target => requestor) | |
| 88 | 88 | task.expects(:target_notification_description).returns('the task') |
| 89 | 89 | |
| 90 | - TaskMailer.deliver_target_notification(task, 'the message') | |
| 90 | + TaskMailer.target_notification(task, 'the message').deliver | |
| 91 | 91 | assert !ActionMailer::Base.deliveries.empty? |
| 92 | 92 | end |
| 93 | 93 | |
| ... | ... | @@ -114,13 +114,13 @@ class TaskMailerTest < ActiveSupport::TestCase |
| 114 | 114 | requestor.expects(:environment).returns(environment).at_least_once |
| 115 | 115 | task.expects(:environment).returns(environment).at_least_once |
| 116 | 116 | |
| 117 | - mail = TaskMailer.create_invitation_notification(task) | |
| 117 | + mail = TaskMailer.invitation_notification(task) | |
| 118 | 118 | |
| 119 | 119 | assert_match(/#{task.target_notification_description}/, mail.subject) |
| 120 | 120 | |
| 121 | - assert_equal "Hello friend name, my name invite you, please follow this link: http://example.com/account/signup?invitation_code=123456", mail.body | |
| 121 | + assert_equal "Hello friend name, my name invite you, please follow this link: http://example.com/account/signup?invitation_code=123456", mail.body.to_s | |
| 122 | 122 | |
| 123 | - TaskMailer.deliver(mail) | |
| 123 | + mail.deliver | |
| 124 | 124 | assert !ActionMailer::Base.deliveries.empty? |
| 125 | 125 | end |
| 126 | 126 | ... | ... |