Commit 04f0aad7d0188cc520f8610ee3fe86808276a0a7

Authored by Antonio Terceiro
1 parent eb6670c4

Fix plugins mailers

lib/noosfero/plugin/mailer_base.rb
1 class Noosfero::Plugin::MailerBase < ActionMailer::Base 1 class Noosfero::Plugin::MailerBase < ActionMailer::Base
2 - class_inheritable_accessor :view_paths  
3 2
4 - def self.prepend_view_path(path)  
5 - view_paths.unshift(*path)  
6 - ActionView::TemplateFinder.process_view_paths(path) 3 + def self.inherited(child)
  4 + child.template_root = File.expand_path(File.join(Rails.root, 'plugins', child.plugin_name, 'views'))
7 end 5 end
8 6
9 - def self.append_view_path(path)  
10 - view_paths.push(*path)  
11 - ActionView::TemplateFinder.process_view_paths(path)  
12 - end  
13 -  
14 - def self.view_paths  
15 - @view_paths ||= [template_root]  
16 - end  
17 -  
18 - def view_paths  
19 - self.class.view_paths 7 + def self.plugin_name
  8 + name.split('::').first.gsub(/Plugin$/, '').underscore
20 end 9 end
21 10
22 def initialize_template_class(assigns) 11 def initialize_template_class(assigns)
23 ActionView::Base.new(view_paths, assigns, self) 12 ActionView::Base.new(view_paths, assigns, self)
24 end 13 end
  14 +
25 end 15 end
plugins/bsc/lib/bsc_plugin/mailer.rb
1 class BscPlugin::Mailer < Noosfero::Plugin::MailerBase 1 class BscPlugin::Mailer < Noosfero::Plugin::MailerBase
2 2
3 - prepend_view_path(BscPlugin.root_path+'/views')  
4 -  
5 def admin_notification(admin, bsc) 3 def admin_notification(admin, bsc)
6 domain = bsc.hostname || bsc.environment.default_hostname 4 domain = bsc.hostname || bsc.environment.default_hostname
7 recipients admin.contact_email 5 recipients admin.contact_email
plugins/bsc/test/functional/bsc_plugin_environment_controller_test.rb
@@ -5,7 +5,7 @@ require File.dirname(__FILE__) + &#39;/../../../../app/models/uploaded_file&#39; @@ -5,7 +5,7 @@ require File.dirname(__FILE__) + &#39;/../../../../app/models/uploaded_file&#39;
5 # Re-raise errors caught by the controller. 5 # Re-raise errors caught by the controller.
6 class BscPluginEnvironmentController; def rescue_action(e) raise e end; end 6 class BscPluginEnvironmentController; def rescue_action(e) raise e end; end
7 7
8 -class BscPluginEnvironmentControllerTest < ActiveSupport::TestCase 8 +class BscPluginEnvironmentControllerTest < ActionController::TestCase
9 9
10 VALID_CNPJ = '94.132.024/0001-48' 10 VALID_CNPJ = '94.132.024/0001-48'
11 11
plugins/bsc/test/functional/bsc_plugin_myprofile_controller_test.rb
@@ -5,7 +5,7 @@ require File.dirname(__FILE__) + &#39;/../../../../app/models/uploaded_file&#39; @@ -5,7 +5,7 @@ require File.dirname(__FILE__) + &#39;/../../../../app/models/uploaded_file&#39;
5 # Re-raise errors caught by the controller. 5 # Re-raise errors caught by the controller.
6 class BscPluginMyprofileController; def rescue_action(e) raise e end; end 6 class BscPluginMyprofileController; def rescue_action(e) raise e end; end
7 7
8 -class BscPluginMyprofileControllerTest < ActiveSupport::TestCase 8 +class BscPluginMyprofileControllerTest < ActionController::TestCase
9 9
10 VALID_CNPJ = '94.132.024/0001-48' 10 VALID_CNPJ = '94.132.024/0001-48'
11 11
plugins/foo/db/migrate/20111218120200_create_foo_plugin_bars.rb 0 → 100644
@@ -0,0 +1,12 @@ @@ -0,0 +1,12 @@
  1 +class CreateFooPluginBars < ActiveRecord::Migration
  2 + def self.up
  3 + create_table :foo_plugin_bars do |t|
  4 + t.string :name
  5 + end
  6 + add_column :profiles, :bar_id, :integer
  7 + end
  8 + def self.down
  9 + drop_table :foo_plugin_bars
  10 + remove_column :profile, :bar_id
  11 + end
  12 +end
plugins/foo/lib/foo_plugin.rb 0 → 100644
@@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
  1 +class FooPlugin < Noosfero::Plugin
  2 +
  3 + def self.plugin_name
  4 + "Foo"
  5 + end
  6 +
  7 + def self.plugin_description
  8 + _("A sample plugin to test autoload craziness.")
  9 + end
  10 +
  11 +end
plugins/foo/lib/foo_plugin/bar.rb 0 → 100644
@@ -0,0 +1,2 @@ @@ -0,0 +1,2 @@
  1 +class FooPlugin::Bar < Noosfero::Plugin::ActiveRecord
  2 +end
plugins/foo/test/unit/foo_plugin_test.rb 0 → 100644
@@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +
  3 +class FooPluginTest < ActiveSupport::TestCase
  4 + def test_foo
  5 + FooPlugin::Bar.create!
  6 + end
  7 +end
plugins/send_email/controllers/send_email_plugin_base_controller.rb
@@ -11,7 +11,7 @@ module SendEmailPluginBaseController @@ -11,7 +11,7 @@ module SendEmailPluginBaseController
11 ) 11 )
12 @mail.subject = params[:subject] unless params[:subject].blank? 12 @mail.subject = params[:subject] unless params[:subject].blank?
13 if @mail.valid? 13 if @mail.valid?
14 - SendEmailPlugin::Sender.deliver_mail(request.referer, @context_url, @mail) 14 + SendEmailPlugin::Sender.deliver_message(request.referer, @context_url, @mail)
15 if request.xhr? 15 if request.xhr?
16 render :text => _('Message sent') 16 render :text => _('Message sent')
17 else 17 else
plugins/send_email/lib/send_email_plugin/mail.rb
@@ -16,14 +16,14 @@ class SendEmailPlugin::Mail &lt; ActiveRecord::Base #WithoutTable @@ -16,14 +16,14 @@ class SendEmailPlugin::Mail &lt; ActiveRecord::Base #WithoutTable
16 def validate 16 def validate
17 if to_as_list.any? do |value| 17 if to_as_list.any? do |value|
18 if value !~ Noosfero::Constants::EMAIL_FORMAT 18 if value !~ Noosfero::Constants::EMAIL_FORMAT
19 - self.errors.add(:to, _("%{fn} '%s' isn't a valid e-mail address") % value) 19 + self.errors.add(:to, _("'%s' isn't a valid e-mail address") % value)
20 end 20 end
21 end 21 end
22 else 22 else
23 allowed_emails = environment ? environment.send_email_plugin_allow_to.to_s.gsub(/\s+/, '').split(/,/) : [] 23 allowed_emails = environment ? environment.send_email_plugin_allow_to.to_s.gsub(/\s+/, '').split(/,/) : []
24 if to_as_list.any? do |value| 24 if to_as_list.any? do |value|
25 if !allowed_emails.include?(value) 25 if !allowed_emails.include?(value)
26 - self.errors.add(:to, _("%{fn} '%s' address is not allowed (see SendEmailPlugin config)") % value) 26 + self.errors.add(:to, _("'%s' address is not allowed (see SendEmailPlugin config)") % value)
27 end 27 end
28 end 28 end
29 end 29 end
plugins/send_email/lib/send_email_plugin/sender.rb
1 class SendEmailPlugin::Sender < Noosfero::Plugin::MailerBase 1 class SendEmailPlugin::Sender < Noosfero::Plugin::MailerBase
2 - prepend_view_path(SendEmailPlugin.root_path+'/views')  
3 2
4 - def mail(referer, url, mail) 3 + def message(referer, url, mail)
5 recipients mail.to 4 recipients mail.to
6 from mail.from 5 from mail.from
7 subject "[#{mail.environment.name}] #{mail.subject}" 6 subject "[#{mail.environment.name}] #{mail.subject}"
plugins/send_email/test/functional/send_email_plugin_admin_controller_test.rb
@@ -4,7 +4,7 @@ require File.dirname(__FILE__) + &#39;/../../controllers/send_email_plugin_admin_con @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + &#39;/../../controllers/send_email_plugin_admin_con
4 # Re-raise errors caught by the controller. 4 # Re-raise errors caught by the controller.
5 class SendEmailPluginAdminController; def rescue_action(e) raise e end; end 5 class SendEmailPluginAdminController; def rescue_action(e) raise e end; end
6 6
7 -class SendEmailPluginAdminControllerTest < ActiveSupport::TestCase 7 +class SendEmailPluginAdminControllerTest < ActionController::TestCase
8 8
9 def setup 9 def setup
10 @controller = SendEmailPluginAdminController.new 10 @controller = SendEmailPluginAdminController.new
plugins/send_email/test/functional/send_email_plugin_base_controller_test.rb
@@ -51,7 +51,7 @@ def run_common_tests @@ -51,7 +51,7 @@ def run_common_tests
51 end 51 end
52 end 52 end
53 53
54 -class SendEmailPluginProfileControllerTest < ActiveSupport::TestCase 54 +class SendEmailPluginProfileControllerTest < ActionController::TestCase
55 def setup 55 def setup
56 @controller = SendEmailPluginProfileController.new 56 @controller = SendEmailPluginProfileController.new
57 @request = ActionController::TestRequest.new 57 @request = ActionController::TestRequest.new
@@ -66,7 +66,7 @@ class SendEmailPluginProfileControllerTest &lt; ActiveSupport::TestCase @@ -66,7 +66,7 @@ class SendEmailPluginProfileControllerTest &lt; ActiveSupport::TestCase
66 run_common_tests() 66 run_common_tests()
67 end 67 end
68 68
69 -class SendEmailPluginEnvironmentControllerTest < ActiveSupport::TestCase 69 +class SendEmailPluginEnvironmentControllerTest < ActionController::TestCase
70 def setup 70 def setup
71 @controller = SendEmailPluginEnvironmentController.new 71 @controller = SendEmailPluginEnvironmentController.new
72 @request = ActionController::TestRequest.new 72 @request = ActionController::TestRequest.new
plugins/send_email/test/unit/send_email_plugin_sender_test.rb
@@ -15,19 +15,19 @@ class SendEmailPluginSenderTest &lt; ActiveSupport::TestCase @@ -15,19 +15,19 @@ class SendEmailPluginSenderTest &lt; ActiveSupport::TestCase
15 end 15 end
16 16
17 should 'be able to deliver mail' do 17 should 'be able to deliver mail' do
18 - response = SendEmailPlugin::Sender.deliver_mail("http://localhost/contact", 'http//profile', @mail) 18 + response = SendEmailPlugin::Sender.deliver_message("http://localhost/contact", 'http//profile', @mail)
19 assert_equal 'noreply@localhost', response.from.to_s 19 assert_equal 'noreply@localhost', response.from.to_s
20 assert_equal "[Noosfero] #{@mail.subject}", response.subject 20 assert_equal "[Noosfero] #{@mail.subject}", response.subject
21 end 21 end
22 22
23 should 'deliver mail to john@example.com' do 23 should 'deliver mail to john@example.com' do
24 - response = SendEmailPlugin::Sender.deliver_mail("http://localhost/contact", 'http//profile', @mail) 24 + response = SendEmailPlugin::Sender.deliver_message("http://localhost/contact", 'http//profile', @mail)
25 assert_equal ['john@example.com'], response.to 25 assert_equal ['john@example.com'], response.to
26 end 26 end
27 27
28 should 'add each key value pair to message body' do 28 should 'add each key value pair to message body' do
29 @mail.params = {:param1 => 'value1', :param2 => 'value2'} 29 @mail.params = {:param1 => 'value1', :param2 => 'value2'}
30 - response = SendEmailPlugin::Sender.deliver_mail("http://localhost/contact", 'http//profile', @mail) 30 + response = SendEmailPlugin::Sender.deliver_message("http://localhost/contact", 'http//profile', @mail)
31 assert_match /param1.+value1/m, response.body 31 assert_match /param1.+value1/m, response.body
32 assert_match /param2.+value2/m, response.body 32 assert_match /param2.+value2/m, response.body
33 end 33 end
plugins/send_email/views/send_email_plugin/sender/mail.rhtml
@@ -1,8 +0,0 @@ @@ -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/sender/message.rhtml 0 → 100644
@@ -0,0 +1,8 @@ @@ -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/shopping_cart/lib/shopping_cart_plugin/mailer.rb
1 class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase 1 class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase
2 2
3 - prepend_view_path(ShoppingCartPlugin.root_path+'/views')  
4 -  
5 def customer_notification(customer, supplier, items) 3 def customer_notification(customer, supplier, items)
6 domain = supplier.hostname || supplier.environment.default_hostname 4 domain = supplier.hostname || supplier.environment.default_hostname
7 recipients customer[:email] 5 recipients customer[:email]
plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb
@@ -4,7 +4,7 @@ require File.dirname(__FILE__) + &#39;/../../controllers/shopping_cart_plugin_myprof @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + &#39;/../../controllers/shopping_cart_plugin_myprof
4 # Re-raise errors caught by the controller. 4 # Re-raise errors caught by the controller.
5 class ShoppingCartPluginMyprofileController; def rescue_action(e) raise e end; end 5 class ShoppingCartPluginMyprofileController; def rescue_action(e) raise e end; end
6 6
7 -class ShoppingCartPluginMyprofileControllerTest < ActiveSupport::TestCase 7 +class ShoppingCartPluginMyprofileControllerTest < ActionController::TestCase
8 8
9 TIME_FORMAT = '%Y-%m-%d' 9 TIME_FORMAT = '%Y-%m-%d'
10 10
plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb
@@ -4,7 +4,7 @@ require File.dirname(__FILE__) + &#39;/../../controllers/shopping_cart_plugin_profil @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + &#39;/../../controllers/shopping_cart_plugin_profil
4 # Re-raise errors caught by the controller. 4 # Re-raise errors caught by the controller.
5 class ShoppingCartPluginProfileController; def rescue_action(e) raise e end; end 5 class ShoppingCartPluginProfileController; def rescue_action(e) raise e end; end
6 6
7 -class ShoppingCartPluginProfileControllerTest < ActiveSupport::TestCase 7 +class ShoppingCartPluginProfileControllerTest < ActionController::TestCase
8 8
9 def setup 9 def setup
10 @controller = ShoppingCartPluginProfileController.new 10 @controller = ShoppingCartPluginProfileController.new