From 8faae7fa469606a921eb3488dd8b0efc9871f888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Pi=C4=8Dman?= Date: Thu, 14 Dec 2023 15:36:28 +0100 Subject: [PATCH] #330 Email templates --- CHANGELOG.md | 10 +++++++++ README.md | 8 +++++++ app/models/custom_workflow_mailer.rb | 28 +++++++++++++++++++----- test/unit/custom_workflow_mailer_test.rb | 2 +- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17d3394..b17bc78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ Changelog for Custom Workflows 2.1.1 *????-??-??* ------------------ +IMPORTANT: Parameters of *CustomWorkflowMailer.deliver_custom_email* method has changed. + +before: `CustomWorkflowMailer.deliver_custom_email(user, subject, text)` + +now: `CustomWorkflowMailer.deliver_custom_email(user, headers = {})` + +To achieve the same behaviour you have to modify an existing callig as follows + +`CustomWorkflowMailer.deliver_custom_email(user, subject: subject, text_body: text)` + 2.1.0 *2023-11-15* ------------------ Member as an observable object diff --git a/README.md b/README.md index d26aed1..ab0f8bd 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,14 @@ E.g.: self.custom_workflow_env[:remote_ip] ``` +An email can be sent from within a script. + +E.g.: + +```ruby +CustomWorkflowMailer.deliver_custom_email(user, subject: subject, text_body: text) +``` + Enabling custom workflows for projects -------------------------------------- diff --git a/app/models/custom_workflow_mailer.rb b/app/models/custom_workflow_mailer.rb index c52499e..5c81da3 100644 --- a/app/models/custom_workflow_mailer.rb +++ b/app/models/custom_workflow_mailer.rb @@ -25,13 +25,29 @@ class CustomWorkflowMailer < Mailer layout 'mailer' - def self.deliver_custom_email(user, subject, text) - custom_email(user, subject, text).deliver_later + def self.deliver_custom_email(user, headers = {}) + custom_email(user, headers).deliver_later end - def custom_email(user, subject, text) - set_language_if_valid user.language - @text = text - mail to: user, subject: subject + def custom_email(user, headers) + 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 diff --git a/test/unit/custom_workflow_mailer_test.rb b/test/unit/custom_workflow_mailer_test.rb index c01a2eb..ed5ba90 100644 --- a/test/unit/custom_workflow_mailer_test.rb +++ b/test/unit/custom_workflow_mailer_test.rb @@ -40,7 +40,7 @@ def test_truth end def test_custom_email - CustomWorkflowMailer.deliver_custom_email @user2, 'Subject', 'Body' + CustomWorkflowMailer.deliver_custom_email @user2, subject: 'Subject', text_body: 'Body', html_body: 'Body' email = last_email assert text_part(email).body.include? 'Body' assert html_part(email).body.include? 'Body'