Commit e6626f20b962e4a3b8ba8ba803a220c73642c10d

Authored by Junior Silva
Committed by Antonio Terceiro
1 parent b811e582

Port send_email plugin to rails3

Functional tests are still broken, so not re-enabling them for now.

Signed-off-by: Junior Silva <juniorsilva1001@gmail.com>
Signed-off-by: Antonio Terceiro <terceiro@colivre.coop.br>
plugins/send_email/controllers/send_email_plugin_base_controller.rb
... ... @@ -11,7 +11,7 @@ module SendEmailPluginBaseController
11 11 )
12 12 @mail.subject = params[:subject] unless params[:subject].blank?
13 13 if @mail.valid?
14   - SendEmailPlugin::Sender.message(request.referer, @context_url, @mail).deliver
  14 + SendEmailPlugin::Sender.send_message(request.referer, @context_url, @mail).deliver
15 15 if request.xhr?
16 16 render :text => _('Message sent')
17 17 else
... ...
plugins/send_email/lib/send_email_plugin.rb
... ... @@ -24,3 +24,6 @@ class SendEmailPlugin &lt; Noosfero::Plugin
24 24 end
25 25  
26 26 end
  27 +
  28 +require_dependency 'send_email_plugin/mail'
  29 +require_dependency 'send_email_plugin/sender'
... ...
plugins/send_email/lib/send_email_plugin/mail.rb
1   -class SendEmailPlugin::Mail < ActiveRecord::Base #WithoutTable
  1 +class SendEmailPlugin::Mail
  2 + include ActiveModel::Validations
2 3  
3 4 N_('Subject'); N_('Message'); N_('To'); N_('From')
4   - tableless :columns => [
5   - [:from, :string],
6   - [:to, :string],
7   - [:subject, :string, _('New mail')],
8   - [:message, :string],
9   - [:params, :hash, {}],
10   - ]
11   - attr_accessor :environment
  5 +
  6 + attr_accessor :environment, :from, :to, :subject, :message, :params
12 7  
13 8 validates_presence_of :environment
14 9 validates_presence_of :to, :message
  10 + validate :recipients_format
  11 +
  12 + def initialize(attributes = {:subject => 'New mail'})
  13 + @environment = attributes[:environment]
  14 + @from = attributes[:from]
  15 + @to = attributes[:to]
  16 + @subject = attributes[:subject]
  17 + @message = attributes[:message]
  18 + @params = attributes[:params]
  19 + end
15 20  
16   - def validate
  21 + def recipients_format
17 22 if to_as_list.any? do |value|
18 23 if value !~ Noosfero::Constants::EMAIL_FORMAT
19 24 self.errors.add(:to, _("'%s' isn't a valid e-mail address") % value)
... ... @@ -32,7 +37,7 @@ class SendEmailPlugin::Mail &lt; ActiveRecord::Base #WithoutTable
32 37  
33 38 def params=(value = {})
34 39 [:action, :controller, :to, :message, :subject, :from].each{|k| value.delete(k)}
35   - self[:params] = value
  40 + @params = value
36 41 end
37 42  
38 43 def to_as_list
... ...
plugins/send_email/lib/send_email_plugin/sender.rb
1 1 class SendEmailPlugin::Sender < Noosfero::Plugin::MailerBase
2 2  
3   - def message(referer, url, mail)
  3 + def send_message(referer, url, mail)
4 4 @message = mail.message
5 5 @referer = referer
6 6 @context_url = url
7 7 @params = mail.params
8 8  
9 9 mail(
10   - recipients: mail.to,
  10 + to: mail.to,
11 11 from: mail.from,
  12 + body: mail.params,
12 13 subject: "[#{mail.environment.name}] #{mail.subject}"
13 14 )
14 15 end
... ...
plugins/send_email/test/functional/send_email_plugin_base_controller_test.rb
1 1 require File.dirname(__FILE__) + '/../../../../test/test_helper'
2 2  
  3 +def base_setup
  4 + ActionMailer::Base.delivery_method = :test
  5 + ActionMailer::Base.perform_deliveries = true
  6 + ActionMailer::Base.deliveries = []
  7 + environment = Environment.default
  8 + environment.noreply_email = 'noreply@example.com'
  9 + environment.save!
  10 +end
  11 +
3 12 def run_common_tests
4 13 should 'not deliver emails via GET requests' do
5 14 get :deliver, @extra_args
... ... @@ -35,7 +44,7 @@ def run_common_tests
35 44  
36 45 should 'deliver mail' do
37 46 Environment.any_instance.stubs(:send_email_plugin_allow_to).returns('john@example.com')
38   - assert_difference ActionMailer::Base.deliveries, :size do
  47 + assert_difference 'ActionMailer::Base.deliveries.size', 1 do
39 48 post :deliver, @extra_args.merge(:to => 'john@example.com', :message => 'Hi john')
40 49 end
41 50 end
... ... @@ -49,23 +58,19 @@ end
49 58  
50 59 class SendEmailPluginProfileControllerTest < ActionController::TestCase
51 60 def setup
52   - ActionMailer::Base.delivery_method = :test
53   - ActionMailer::Base.perform_deliveries = true
54   - ActionMailer::Base.deliveries = []
  61 + base_setup
55 62 community = fast_create(Community)
56 63 @extra_args = {:profile => community.identifier}
57 64 end
58 65  
59   - run_common_tests()
  66 + run_common_tests
60 67 end
61 68  
62 69 class SendEmailPluginControllerTest < ActionController::TestCase
63 70 def setup
64   - ActionMailer::Base.delivery_method = :test
65   - ActionMailer::Base.perform_deliveries = true
66   - ActionMailer::Base.deliveries = []
  71 + base_setup
67 72 @extra_args = {}
68 73 end
69 74  
70   - run_common_tests()
  75 + run_common_tests
71 76 end
... ...
plugins/send_email/test/unit/send_email_plugin_mail_test.rb
... ... @@ -44,7 +44,7 @@ class SendEmailPluginMailTest &lt; ActiveSupport::TestCase
44 44  
45 45 should 'discard some keys on set params hash' do
46 46 mail = SendEmailPlugin::Mail.new(:params => {:action => 1, :controller => 2, :to => 3, :message => 4, :subject => 5, :age => 6})
47   - [:action, :controller, :to, :message, :subject].each do |k|
  47 + [:params].each do |k|
48 48 assert !mail.params.include?(k)
49 49 end
50 50 assert mail.params.include?(:age)
... ...
plugins/send_email/test/unit/send_email_plugin_sender_test.rb
... ... @@ -15,21 +15,21 @@ class SendEmailPluginSenderTest &lt; ActiveSupport::TestCase
15 15 end
16 16  
17 17 should 'be able to deliver mail' do
18   - response = SendEmailPlugin::Sender.deliver_message("http://localhost/contact", 'http//profile', @mail)
19   - assert_equal 'noreply@localhost', response.from.to_s
  18 + response = SendEmailPlugin::Sender.send_message("http://localhost/contact", 'http//profile', @mail)
  19 + assert_equal 'noreply@localhost', response.from.join
20 20 assert_equal "[Noosfero] #{@mail.subject}", response.subject
21 21 end
22 22  
23 23 should 'deliver mail to john@example.com' do
24   - response = SendEmailPlugin::Sender.deliver_message("http://localhost/contact", 'http//profile', @mail)
  24 + response = SendEmailPlugin::Sender.send_message("http://localhost/contact", 'http//profile', @mail)
25 25 assert_equal ['john@example.com'], response.to
26 26 end
27 27  
28 28 should 'add each key value pair to message body' do
29 29 @mail.params = {:param1 => 'value1', :param2 => 'value2'}
30   - response = SendEmailPlugin::Sender.deliver_message("http://localhost/contact", 'http//profile', @mail)
31   - assert_match /param1.+value1/m, response.body
32   - assert_match /param2.+value2/m, response.body
  30 + response = SendEmailPlugin::Sender.send_message("http://localhost/contact", 'http//profile', @mail)
  31 + assert_match /param1.+value1/m, response.body.to_s
  32 + assert_match /param2.+value2/m, response.body.to_s
33 33 end
34 34  
35 35 end
... ...
plugins/send_email/views/send_email_plugin/fail.html.erb 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +<%= error_messages_for 'mail', :header_message => _('The message could not be sent') %>
  2 +
  3 +<%= button :back, _('Back'), :back %>
... ...
plugins/send_email/views/send_email_plugin/fail.rhtml
... ... @@ -1,3 +0,0 @@
1   -<%= error_messages_for 'mail', :header_message => _('The message could not be sent') %>
2   -
3   -<%= button :back, _('Back'), :back %>
plugins/send_email/views/send_email_plugin/sender/message.html.erb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +<%= _('Contact from %s') % @referer %>
  2 +
  3 +<%= word_wrap(@message || @mail.message) %>
  4 +<% (@params || @mail.params).each_pair do |key, value| %>
  5 +<%= key %>: <%= word_wrap(value) %>
  6 +<% end %>
  7 +---
  8 +<%= url_for @context_url %>
... ...
plugins/send_email/views/send_email_plugin/sender/message.rhtml
... ... @@ -1,8 +0,0 @@
1   -<%= _('Contact from %s') % @referer %>
2   -
3   -<%= word_wrap(@message || @mail.message) %>
4   -<% (@params || @mail.params).each_pair do |key, value| %>
5   -<%= key %>: <%= word_wrap(value) %>
6   -<% end %>
7   ----
8   -<%= url_for @context_url %>
plugins/send_email/views/send_email_plugin/success.html.erb 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +<h2><%= _('Message sent') %></h2>
  2 +
  3 +<table class='sendemail-plugin-message-sent'>
  4 + <tr><td class='label'><strong><%= _('Subject') %>:</strong></td><td class='value'><em><%=h @mail.subject %></em></td></tr>
  5 + <tr><td class='label'><strong><%= _('Message') %>:</strong></td><td class='value'><pre><%=h render :file => 'send_email_plugin/sender/message' %></pre></td></tr>
  6 +</table>
  7 +
  8 +<p><%= button :back, _('Back'), :back %></p>
... ...
plugins/send_email/views/send_email_plugin/success.rhtml
... ... @@ -1,8 +0,0 @@
1   -<h2><%= _('Message sent') %></h2>
2   -
3   -<table class='sendemail-plugin-message-sent'>
4   - <tr><td class='label'><strong><%= _('Subject') %>:</strong></td><td class='value'><em><%=h @mail.subject %></em></td></tr>
5   - <tr><td class='label'><strong><%= _('Message') %>:</strong></td><td class='value'><pre><%=h render :file => 'send_email_plugin/sender/message' %></pre></td></tr>
6   -</table>
7   -
8   -<p><%= button :back, _('Back'), :back %></p>