diff --git a/app/models/custom_workflow_mailer.rb b/app/models/custom_workflow_mailer.rb new file mode 100644 index 0000000..45dc4bb --- /dev/null +++ b/app/models/custom_workflow_mailer.rb @@ -0,0 +1,37 @@ +# encoding: utf-8 +# +# Redmine plugin for Custom Workflows +# +# Copyright Anton Argirov +# Copyright Karel Pičman +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require 'mailer' + +class CustomWorkflowMailer < Mailer + layout 'mailer' + + def self.deliver_custom_email(user, subject, text) + custom_email(user, subject, text).deliver_later + end + + def custom_email(user, subject, text) + set_language_if_valid user.language + @text = text + mail to: user.mail, subject: subject + end + +end diff --git a/app/views/custom_workflow_mailer/custom_email.html.erb b/app/views/custom_workflow_mailer/custom_email.html.erb new file mode 100644 index 0000000..b565bd1 --- /dev/null +++ b/app/views/custom_workflow_mailer/custom_email.html.erb @@ -0,0 +1,26 @@ +<% + # encoding: utf-8 + # + # Redmine plugin for Custom Workflows + # + # Copyright Anton Argirov + # Copyright Karel Pičman + # + # This program is free software; you can redistribute it and/or + # modify it under the terms of the GNU General Public License + # as published by the Free Software Foundation; either version 2 + # of the License, or (at your option) any later version. + # + # This program is distributed in the hope that it will be useful, + # but WITHOUT ANY WARRANTY; without even the implied warranty of + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + # GNU General Public License for more details. + # + # You should have received a copy of the GNU General Public License + # along with this program; if not, write to the Free Software + # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +%> + +

+ <%= @text %> +

diff --git a/app/views/custom_workflow_mailer/custom_email.text.erb b/app/views/custom_workflow_mailer/custom_email.text.erb new file mode 100644 index 0000000..8bde1f4 --- /dev/null +++ b/app/views/custom_workflow_mailer/custom_email.text.erb @@ -0,0 +1,24 @@ +<% + # encoding: utf-8 + # + # Redmine plugin for Custom Workflows + # + # Copyright Anton Argirov + # Copyright Karel Pičman + # + # This program is free software; you can redistribute it and/or + # modify it under the terms of the GNU General Public License + # as published by the Free Software Foundation; either version 2 + # of the License, or (at your option) any later version. + # + # This program is distributed in the hope that it will be useful, + # but WITHOUT ANY WARRANTY; without even the implied warranty of + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + # GNU General Public License for more details. + # + # You should have received a copy of the GNU General Public License + # along with this program; if not, write to the Free Software + # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +%> + +<%= @text %> diff --git a/lib/redmine_custom_workflows.rb b/lib/redmine_custom_workflows.rb index 7232284..198fed5 100644 --- a/lib/redmine_custom_workflows.rb +++ b/lib/redmine_custom_workflows.rb @@ -29,7 +29,6 @@ require 'redmine_custom_workflows/errors/workflow_error' require 'redmine_custom_workflows/patches/attachment_patch' require 'redmine_custom_workflows/patches/group_patch' require 'redmine_custom_workflows/patches/issue_patch' -require 'redmine_custom_workflows/patches/mailer_patch' require 'redmine_custom_workflows/patches/project_patch' require 'redmine_custom_workflows/patches/projects_helper_patch' require 'redmine_custom_workflows/patches/time_entry_patch' diff --git a/lib/redmine_custom_workflows/patches/mailer_patch.rb b/lib/redmine_custom_workflows/patches/mailer_patch.rb deleted file mode 100644 index 4a35ead..0000000 --- a/lib/redmine_custom_workflows/patches/mailer_patch.rb +++ /dev/null @@ -1,65 +0,0 @@ -# encoding: utf-8 -# -# Redmine plugin for Custom Workflows -# -# Copyright Anton Argirov -# Copyright Karel Pičman -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -module RedmineCustomWorkflows - module Patches - module MailerPatch - - def self.included(base) - base.send(:include, InstanceMethods) - base.class_eval do - end - end - - module InstanceMethods - - def self.deliver_custom_email(headers={}) - user = headers.delete :user - headers[:to] = user.mail if user - text_body = headers.delete :text_body - html_body = headers.delete :html_body - template_name = headers.delete :template_name - template_params = headers.delete(:template_params) || {} - if text_body || html_body - mail headers do |format| - format.text { render :text => text_body } if text_body - format.html { render :text => html_body } if html_body - end - elsif template_name - template_params.each { |k,v| instance_variable_set("@#{k}", v) } - mail headers do |format| - format.text { render template_name } - format.html { render template_name } unless Setting.plain_text_mail? - end - else - raise 'Not :text_body, :html_body or :template_name specified' - end - end - - end - - end - end -end - -unless Mailer.include?(RedmineCustomWorkflows::Patches::MailerPatch) - Mailer.send(:include, RedmineCustomWorkflows::Patches::MailerPatch) -end \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 76383ba..867893b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -20,4 +20,6 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Load the normal Rails helper -require File.expand_path('../../../../test/test_helper', __FILE__) \ No newline at end of file +require File.expand_path('../../../../test/test_helper', __FILE__) + +require_relative 'unit_test' \ No newline at end of file diff --git a/test/unit/custom_workflow_mailer_test.rb b/test/unit/custom_workflow_mailer_test.rb new file mode 100644 index 0000000..6c42c13 --- /dev/null +++ b/test/unit/custom_workflow_mailer_test.rb @@ -0,0 +1,65 @@ +# encoding: utf-8 +# +# Redmine plugin for Custom Workflows +# +# Copyright Anton Argirov +# Copyright Karel Pičman +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require File.expand_path('../../test_helper', __FILE__) + +class CustomWorkflowMailerTest < RedmineCustomWorkflows::Test::UnitTest + include Redmine::I18n + + fixtures :users, :email_addresses + + def setup + @user2 = User.find 2 + # Mailer settings + ActionMailer::Base.deliveries.clear + Setting.plain_text_mail = '0' + Setting.default_language = 'en' + User.current = nil + end + + def test_truth + assert_kind_of User, @user2 + end + + def test_custom_email + CustomWorkflowMailer.deliver_custom_email @user2, 'Subject', 'Body' + email = last_email + assert text_part(email).body.include? 'Body' + assert html_part(email).body.include? 'Body' + end + + private + + def last_email + mail = ActionMailer::Base.deliveries.last + assert_not_nil mail + mail + end + + def text_part(email) + email.parts.detect { |part| part.content_type.include?('text/plain') } + end + + def html_part(email) + email.parts.detect { |part| part.content_type.include?('text/html') } + end + +end \ No newline at end of file diff --git a/test/unit_test.rb b/test/unit_test.rb new file mode 100644 index 0000000..8c9eaf6 --- /dev/null +++ b/test/unit_test.rb @@ -0,0 +1,40 @@ +# encoding: utf-8 +# +# Redmine plugin for Custom Workflows +# +# Copyright Anton Argirov +# Copyright Karel Pičman +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module RedmineCustomWorkflows + module Test + + class UnitTest < ActiveSupport::TestCase + + # Allow us to override the fixtures method to implement fixtures for our plugin. + # Ultimately it allows for better integration without blowing redmine fixtures up, + # and allowing us to suppliment redmine fixtures if we need to. + def self.fixtures(*table_names) + dir = File.join( File.dirname(__FILE__), '/fixtures') + table_names.each do |x| + ActiveRecord::FixtureSet.create_fixtures(dir, x) if File.exist?("#{dir}/#{x}.yml") + end + super(table_names) + end + end + + end +end \ No newline at end of file