Skip to content

Commit

Permalink
#330 Email templates
Browse files Browse the repository at this point in the history
  • Loading branch information
picman committed Dec 14, 2023
1 parent f3644a5 commit 8faae7f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------------------------

Expand Down
28 changes: 22 additions & 6 deletions app/models/custom_workflow_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/unit/custom_workflow_mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 8faae7f

Please sign in to comment.