From 04f0aad7d0188cc520f8610ee3fe86808276a0a7 Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Sun, 18 Dec 2011 16:47:53 +0000 Subject: [PATCH] Fix plugins mailers --- lib/noosfero/plugin/mailer_base.rb | 20 +++++--------------- plugins/bsc/lib/bsc_plugin/mailer.rb | 2 -- plugins/bsc/test/functional/bsc_plugin_environment_controller_test.rb | 2 +- plugins/bsc/test/functional/bsc_plugin_myprofile_controller_test.rb | 2 +- plugins/foo/db/migrate/20111218120200_create_foo_plugin_bars.rb | 12 ++++++++++++ plugins/foo/lib/foo_plugin.rb | 11 +++++++++++ plugins/foo/lib/foo_plugin/bar.rb | 2 ++ plugins/foo/test/unit/foo_plugin_test.rb | 7 +++++++ plugins/send_email/controllers/send_email_plugin_base_controller.rb | 2 +- plugins/send_email/lib/send_email_plugin/mail.rb | 4 ++-- plugins/send_email/lib/send_email_plugin/sender.rb | 3 +-- plugins/send_email/test/functional/send_email_plugin_admin_controller_test.rb | 2 +- plugins/send_email/test/functional/send_email_plugin_base_controller_test.rb | 4 ++-- plugins/send_email/test/unit/send_email_plugin_sender_test.rb | 6 +++--- plugins/send_email/views/send_email_plugin/sender/mail.rhtml | 8 -------- plugins/send_email/views/send_email_plugin/sender/message.rhtml | 8 ++++++++ plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb | 2 -- plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb | 2 +- plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb | 2 +- 19 files changed, 59 insertions(+), 42 deletions(-) create mode 100644 plugins/foo/db/migrate/20111218120200_create_foo_plugin_bars.rb create mode 100644 plugins/foo/lib/foo_plugin.rb create mode 100644 plugins/foo/lib/foo_plugin/bar.rb create mode 100644 plugins/foo/test/unit/foo_plugin_test.rb delete mode 100644 plugins/send_email/views/send_email_plugin/sender/mail.rhtml create mode 100644 plugins/send_email/views/send_email_plugin/sender/message.rhtml diff --git a/lib/noosfero/plugin/mailer_base.rb b/lib/noosfero/plugin/mailer_base.rb index 4c54836..efd74b1 100644 --- a/lib/noosfero/plugin/mailer_base.rb +++ b/lib/noosfero/plugin/mailer_base.rb @@ -1,25 +1,15 @@ class Noosfero::Plugin::MailerBase < ActionMailer::Base - class_inheritable_accessor :view_paths - def self.prepend_view_path(path) - view_paths.unshift(*path) - ActionView::TemplateFinder.process_view_paths(path) + def self.inherited(child) + child.template_root = File.expand_path(File.join(Rails.root, 'plugins', child.plugin_name, 'views')) end - def self.append_view_path(path) - view_paths.push(*path) - ActionView::TemplateFinder.process_view_paths(path) - end - - def self.view_paths - @view_paths ||= [template_root] - end - - def view_paths - self.class.view_paths + def self.plugin_name + name.split('::').first.gsub(/Plugin$/, '').underscore end def initialize_template_class(assigns) ActionView::Base.new(view_paths, assigns, self) end + end diff --git a/plugins/bsc/lib/bsc_plugin/mailer.rb b/plugins/bsc/lib/bsc_plugin/mailer.rb index 009fab9..35bad98 100644 --- a/plugins/bsc/lib/bsc_plugin/mailer.rb +++ b/plugins/bsc/lib/bsc_plugin/mailer.rb @@ -1,7 +1,5 @@ class BscPlugin::Mailer < Noosfero::Plugin::MailerBase - prepend_view_path(BscPlugin.root_path+'/views') - def admin_notification(admin, bsc) domain = bsc.hostname || bsc.environment.default_hostname recipients admin.contact_email diff --git a/plugins/bsc/test/functional/bsc_plugin_environment_controller_test.rb b/plugins/bsc/test/functional/bsc_plugin_environment_controller_test.rb index 36abd93..bbb7b86 100644 --- a/plugins/bsc/test/functional/bsc_plugin_environment_controller_test.rb +++ b/plugins/bsc/test/functional/bsc_plugin_environment_controller_test.rb @@ -5,7 +5,7 @@ require File.dirname(__FILE__) + '/../../../../app/models/uploaded_file' # Re-raise errors caught by the controller. class BscPluginEnvironmentController; def rescue_action(e) raise e end; end -class BscPluginEnvironmentControllerTest < ActiveSupport::TestCase +class BscPluginEnvironmentControllerTest < ActionController::TestCase VALID_CNPJ = '94.132.024/0001-48' diff --git a/plugins/bsc/test/functional/bsc_plugin_myprofile_controller_test.rb b/plugins/bsc/test/functional/bsc_plugin_myprofile_controller_test.rb index 539b7b3..e9ccc40 100644 --- a/plugins/bsc/test/functional/bsc_plugin_myprofile_controller_test.rb +++ b/plugins/bsc/test/functional/bsc_plugin_myprofile_controller_test.rb @@ -5,7 +5,7 @@ require File.dirname(__FILE__) + '/../../../../app/models/uploaded_file' # Re-raise errors caught by the controller. class BscPluginMyprofileController; def rescue_action(e) raise e end; end -class BscPluginMyprofileControllerTest < ActiveSupport::TestCase +class BscPluginMyprofileControllerTest < ActionController::TestCase VALID_CNPJ = '94.132.024/0001-48' diff --git a/plugins/foo/db/migrate/20111218120200_create_foo_plugin_bars.rb b/plugins/foo/db/migrate/20111218120200_create_foo_plugin_bars.rb new file mode 100644 index 0000000..b34e4df --- /dev/null +++ b/plugins/foo/db/migrate/20111218120200_create_foo_plugin_bars.rb @@ -0,0 +1,12 @@ +class CreateFooPluginBars < ActiveRecord::Migration + def self.up + create_table :foo_plugin_bars do |t| + t.string :name + end + add_column :profiles, :bar_id, :integer + end + def self.down + drop_table :foo_plugin_bars + remove_column :profile, :bar_id + end +end diff --git a/plugins/foo/lib/foo_plugin.rb b/plugins/foo/lib/foo_plugin.rb new file mode 100644 index 0000000..b74f6f7 --- /dev/null +++ b/plugins/foo/lib/foo_plugin.rb @@ -0,0 +1,11 @@ +class FooPlugin < Noosfero::Plugin + + def self.plugin_name + "Foo" + end + + def self.plugin_description + _("A sample plugin to test autoload craziness.") + end + +end diff --git a/plugins/foo/lib/foo_plugin/bar.rb b/plugins/foo/lib/foo_plugin/bar.rb new file mode 100644 index 0000000..a6904d9 --- /dev/null +++ b/plugins/foo/lib/foo_plugin/bar.rb @@ -0,0 +1,2 @@ +class FooPlugin::Bar < Noosfero::Plugin::ActiveRecord +end diff --git a/plugins/foo/test/unit/foo_plugin_test.rb b/plugins/foo/test/unit/foo_plugin_test.rb new file mode 100644 index 0000000..e3bf10f --- /dev/null +++ b/plugins/foo/test/unit/foo_plugin_test.rb @@ -0,0 +1,7 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' + +class FooPluginTest < ActiveSupport::TestCase + def test_foo + FooPlugin::Bar.create! + end +end diff --git a/plugins/send_email/controllers/send_email_plugin_base_controller.rb b/plugins/send_email/controllers/send_email_plugin_base_controller.rb index c45aaee..06ace29 100644 --- a/plugins/send_email/controllers/send_email_plugin_base_controller.rb +++ b/plugins/send_email/controllers/send_email_plugin_base_controller.rb @@ -11,7 +11,7 @@ module SendEmailPluginBaseController ) @mail.subject = params[:subject] unless params[:subject].blank? if @mail.valid? - SendEmailPlugin::Sender.deliver_mail(request.referer, @context_url, @mail) + SendEmailPlugin::Sender.deliver_message(request.referer, @context_url, @mail) if request.xhr? render :text => _('Message sent') else diff --git a/plugins/send_email/lib/send_email_plugin/mail.rb b/plugins/send_email/lib/send_email_plugin/mail.rb index b3a4f9e..2858030 100644 --- a/plugins/send_email/lib/send_email_plugin/mail.rb +++ b/plugins/send_email/lib/send_email_plugin/mail.rb @@ -16,14 +16,14 @@ class SendEmailPlugin::Mail < ActiveRecord::Base #WithoutTable def validate if to_as_list.any? do |value| if value !~ Noosfero::Constants::EMAIL_FORMAT - self.errors.add(:to, _("%{fn} '%s' isn't a valid e-mail address") % value) + self.errors.add(:to, _("'%s' isn't a valid e-mail address") % value) end end else allowed_emails = environment ? environment.send_email_plugin_allow_to.to_s.gsub(/\s+/, '').split(/,/) : [] if to_as_list.any? do |value| if !allowed_emails.include?(value) - self.errors.add(:to, _("%{fn} '%s' address is not allowed (see SendEmailPlugin config)") % value) + self.errors.add(:to, _("'%s' address is not allowed (see SendEmailPlugin config)") % value) end end end diff --git a/plugins/send_email/lib/send_email_plugin/sender.rb b/plugins/send_email/lib/send_email_plugin/sender.rb index 6f69c06..129f9ce 100644 --- a/plugins/send_email/lib/send_email_plugin/sender.rb +++ b/plugins/send_email/lib/send_email_plugin/sender.rb @@ -1,7 +1,6 @@ class SendEmailPlugin::Sender < Noosfero::Plugin::MailerBase - prepend_view_path(SendEmailPlugin.root_path+'/views') - def mail(referer, url, mail) + def message(referer, url, mail) recipients mail.to from mail.from subject "[#{mail.environment.name}] #{mail.subject}" diff --git a/plugins/send_email/test/functional/send_email_plugin_admin_controller_test.rb b/plugins/send_email/test/functional/send_email_plugin_admin_controller_test.rb index e85c033..5b2191d 100644 --- a/plugins/send_email/test/functional/send_email_plugin_admin_controller_test.rb +++ b/plugins/send_email/test/functional/send_email_plugin_admin_controller_test.rb @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../../controllers/send_email_plugin_admin_con # Re-raise errors caught by the controller. class SendEmailPluginAdminController; def rescue_action(e) raise e end; end -class SendEmailPluginAdminControllerTest < ActiveSupport::TestCase +class SendEmailPluginAdminControllerTest < ActionController::TestCase def setup @controller = SendEmailPluginAdminController.new diff --git a/plugins/send_email/test/functional/send_email_plugin_base_controller_test.rb b/plugins/send_email/test/functional/send_email_plugin_base_controller_test.rb index 23248b3..64001a4 100644 --- a/plugins/send_email/test/functional/send_email_plugin_base_controller_test.rb +++ b/plugins/send_email/test/functional/send_email_plugin_base_controller_test.rb @@ -51,7 +51,7 @@ def run_common_tests end end -class SendEmailPluginProfileControllerTest < ActiveSupport::TestCase +class SendEmailPluginProfileControllerTest < ActionController::TestCase def setup @controller = SendEmailPluginProfileController.new @request = ActionController::TestRequest.new @@ -66,7 +66,7 @@ class SendEmailPluginProfileControllerTest < ActiveSupport::TestCase run_common_tests() end -class SendEmailPluginEnvironmentControllerTest < ActiveSupport::TestCase +class SendEmailPluginEnvironmentControllerTest < ActionController::TestCase def setup @controller = SendEmailPluginEnvironmentController.new @request = ActionController::TestRequest.new diff --git a/plugins/send_email/test/unit/send_email_plugin_sender_test.rb b/plugins/send_email/test/unit/send_email_plugin_sender_test.rb index 42c03b7..802a45e 100644 --- a/plugins/send_email/test/unit/send_email_plugin_sender_test.rb +++ b/plugins/send_email/test/unit/send_email_plugin_sender_test.rb @@ -15,19 +15,19 @@ class SendEmailPluginSenderTest < ActiveSupport::TestCase end should 'be able to deliver mail' do - response = SendEmailPlugin::Sender.deliver_mail("http://localhost/contact", 'http//profile', @mail) + response = SendEmailPlugin::Sender.deliver_message("http://localhost/contact", 'http//profile', @mail) assert_equal 'noreply@localhost', response.from.to_s assert_equal "[Noosfero] #{@mail.subject}", response.subject end should 'deliver mail to john@example.com' do - response = SendEmailPlugin::Sender.deliver_mail("http://localhost/contact", 'http//profile', @mail) + response = SendEmailPlugin::Sender.deliver_message("http://localhost/contact", 'http//profile', @mail) assert_equal ['john@example.com'], response.to end should 'add each key value pair to message body' do @mail.params = {:param1 => 'value1', :param2 => 'value2'} - response = SendEmailPlugin::Sender.deliver_mail("http://localhost/contact", 'http//profile', @mail) + response = SendEmailPlugin::Sender.deliver_message("http://localhost/contact", 'http//profile', @mail) assert_match /param1.+value1/m, response.body assert_match /param2.+value2/m, response.body end diff --git a/plugins/send_email/views/send_email_plugin/sender/mail.rhtml b/plugins/send_email/views/send_email_plugin/sender/mail.rhtml deleted file mode 100644 index 251f4a8..0000000 --- a/plugins/send_email/views/send_email_plugin/sender/mail.rhtml +++ /dev/null @@ -1,8 +0,0 @@ -<%= _('Contact from %s') % @referer %> - -<%= word_wrap(@message || @mail.message) %> -<% (@params || @mail.params).each_pair do |key, value| %> -<%= key %>: <%= word_wrap(value) %> -<% end %> ---- -<%= url_for @context_url %> diff --git a/plugins/send_email/views/send_email_plugin/sender/message.rhtml b/plugins/send_email/views/send_email_plugin/sender/message.rhtml new file mode 100644 index 0000000..251f4a8 --- /dev/null +++ b/plugins/send_email/views/send_email_plugin/sender/message.rhtml @@ -0,0 +1,8 @@ +<%= _('Contact from %s') % @referer %> + +<%= word_wrap(@message || @mail.message) %> +<% (@params || @mail.params).each_pair do |key, value| %> +<%= key %>: <%= word_wrap(value) %> +<% end %> +--- +<%= url_for @context_url %> diff --git a/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb b/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb index 0c8ee0b..6a064f6 100644 --- a/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb +++ b/plugins/shopping_cart/lib/shopping_cart_plugin/mailer.rb @@ -1,7 +1,5 @@ class ShoppingCartPlugin::Mailer < Noosfero::Plugin::MailerBase - prepend_view_path(ShoppingCartPlugin.root_path+'/views') - def customer_notification(customer, supplier, items) domain = supplier.hostname || supplier.environment.default_hostname recipients customer[:email] diff --git a/plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb b/plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb index 955084b..c6bd0f1 100644 --- a/plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb +++ b/plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../../controllers/shopping_cart_plugin_myprof # Re-raise errors caught by the controller. class ShoppingCartPluginMyprofileController; def rescue_action(e) raise e end; end -class ShoppingCartPluginMyprofileControllerTest < ActiveSupport::TestCase +class ShoppingCartPluginMyprofileControllerTest < ActionController::TestCase TIME_FORMAT = '%Y-%m-%d' diff --git a/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb b/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb index 3b6eaa7..362576f 100644 --- a/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb +++ b/plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb @@ -4,7 +4,7 @@ require File.dirname(__FILE__) + '/../../controllers/shopping_cart_plugin_profil # Re-raise errors caught by the controller. class ShoppingCartPluginProfileController; def rescue_action(e) raise e end; end -class ShoppingCartPluginProfileControllerTest < ActiveSupport::TestCase +class ShoppingCartPluginProfileControllerTest < ActionController::TestCase def setup @controller = ShoppingCartPluginProfileController.new -- libgit2 0.21.2