effective_email_templates 1.7.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8d32e71af524fb3658debbf8210a4239b4bbd98f7a806f0a743735e62bc135dc
4
- data.tar.gz: f6064f01a1a679b3c4bf1abca99222ab4ba191d4bb0ee16a1ac23ddad644c368
3
+ metadata.gz: 3f5c8f6d7991aa4a349fecda1ea4fc687df0facb55214d176a0030720ede39db
4
+ data.tar.gz: 7882e4790ff27ff4c60d5985bc501df2fac90bba84e96dcb7b44f7d63dfe3657
5
5
  SHA512:
6
- metadata.gz: 360328a697866d2d802d7c882bcfbd9c0be6e148c0b6d98114417088af72b9ea74cc935d275b6396a8be64becd88749536f491b1cfcb0f953e8ab1c739ba276e
7
- data.tar.gz: 5cad8e018ad138e6050278c36e39a69a631a06773bc8d87aeee9986a96b56de9ab3f491198634eea514126f30241a0536b2b8ccceb9721f5e6a7da928e7f0b63
6
+ metadata.gz: f36ac864bc2efe007f7f06555aefe5a5a9abdae549dc2b4ed44db66033858efb1c26cac1ab7679ce6bb4321c2a3468fc8b80399347682c76b412e05ba74acdbf
7
+ data.tar.gz: 4ef1228155f5f2940e36af9a9b1c7373147954eb241fe5573c15f880b00231e8f86b6dec1eba53b24bea6b29c688c861b983ffc94a716f55a13c349b39a7f329
@@ -9,7 +9,10 @@ class EffectiveEmailTemplatesDatatable < Effective::Datatable
9
9
 
10
10
  col :template_name, label: 'Name'
11
11
 
12
- col :from, search: EffectiveEmailTemplates.mailer_froms
12
+ col :from, search: EffectiveEmailTemplates.mailer_froms do |email_template|
13
+ ERB::Util.html_escape_once(email_template.from)
14
+ end
15
+
13
16
  col :cc
14
17
  col :bcc
15
18
  col :subject
@@ -42,11 +42,40 @@ module EffectiveEmailTemplatesMailer
42
42
  end
43
43
  end
44
44
 
45
- super(merged)
45
+ # Process body as plain text or html
46
+ body = merged.fetch(:body) || ''
47
+
48
+ # For text/plain emails
49
+ if email_template.plain?
50
+ stripped = body.strip
51
+
52
+ if stripped.starts_with?('<p>') || stripped.ends_with?('</p>')
53
+ raise("unexpected html content found when when rendering text/plain email_template #{action_name}")
54
+ end
55
+
56
+ return super(merged)
57
+ end
58
+
59
+ # For text/html emails
60
+ layout = mailer_layout().presence || raise("expected a mailer layout when rendering text/html email_template #{action_name}")
61
+
62
+ super(merged.except(:body, :content_type)) do |format|
63
+ format.text { strip_tags(body) }
64
+ format.html { render(layout: layout, inline: body) }
65
+ end
66
+ end
67
+
68
+ def mailer_layout
69
+ try(:mailer_settings).try(:mailer_layout) || EffectiveEmailTemplates.mailer_layout
46
70
  end
47
71
 
48
72
  private
49
73
 
74
+ def strip_tags(html)
75
+ return html if html.blank?
76
+ view_context.strip_tags(html.gsub(/<\/(div|p|br|h[1-6])>/, "\n"))
77
+ end
78
+
50
79
  def route_url_assigns(email_template, existing)
51
80
  route_variables = email_template.template_variables
52
81
  .select { |name| name.ends_with?('_url') }
@@ -1,29 +1,35 @@
1
1
  module Effective
2
2
  class EmailTemplate < ActiveRecord::Base
3
+ include ActionView::Helpers::TextHelper
4
+
3
5
  self.table_name = (EffectiveEmailTemplates.email_templates_table_name || :email_templates).to_s
4
6
 
5
7
  attr_accessor :current_user
6
8
 
7
9
  log_changes if respond_to?(:log_changes)
8
10
 
9
- CONTENT_TYPES = ['text/plain', 'text/html']
11
+ CONTENT_TYPE_PLAIN = 'text/plain'
12
+ CONTENT_TYPE_HTML = 'text/html'
13
+ CONTENT_TYPES = [CONTENT_TYPE_PLAIN, CONTENT_TYPE_HTML]
10
14
 
11
15
  effective_resource do
12
16
  template_name :string
13
- content_type :string
14
17
 
15
- subject :string
16
18
  from :string
17
19
  cc :string
18
20
  bcc :string
21
+
22
+ subject :string
19
23
  body :text
20
24
 
25
+ content_type :string
26
+
21
27
  timestamps
22
28
  end
23
29
 
24
30
  before_validation do
25
- self.content_type ||= CONTENT_TYPES.first
26
31
  self.from ||= EffectiveEmailTemplates.mailer_froms.first
32
+ self.content_type ||= CONTENT_TYPES.first
27
33
  end
28
34
 
29
35
  validates :body, liquid: true
@@ -35,28 +41,38 @@ module Effective
35
41
  validates :content_type, presence: true, inclusion: { in: CONTENT_TYPES }
36
42
  validates :template_name, presence: true
37
43
 
38
- # validate(if: -> { content_type == 'text/html' && body.present? }) do
39
- # unless body.include?('<') && body.include?('>')
40
- # self.errors.add(:content_type, 'expected html tags in body')
41
- # self.errors.add(:body, 'expected html tags in body')
42
- # end
43
- # end
44
+ validate(if: -> { html? && body.present? }) do
45
+ errors.add(:body, 'expected html tags in body') if body_plain?
46
+ end
44
47
 
45
- # validate(if: -> { content_type == 'text/plain' && body.present? }) do
46
- # if body.include?('</a>') || body.include?('</p>')
47
- # self.errors.add(:content_type, 'expected no html tags in body')
48
- # self.errors.add(:body, 'expected no html tags in body')
49
- # end
50
- # end
48
+ validate(if: -> { plain? && body.present? }) do
49
+ errors.add(:body, 'unexpected html tags found in body') if body_html?
50
+ end
51
51
 
52
52
  def to_s
53
53
  template_name.presence || 'New Email Template'
54
54
  end
55
55
 
56
+ def html?
57
+ content_type == CONTENT_TYPE_HTML
58
+ end
59
+
60
+ def plain?
61
+ content_type == CONTENT_TYPE_PLAIN
62
+ end
63
+
64
+ def body_html?
65
+ body.present? && (body.include?('</p>') || body.include?('</div>'))
66
+ end
67
+
68
+ def body_plain?
69
+ body.present? && !(body.include?('</p>') || body.include?('</div>'))
70
+ end
71
+
56
72
  def render(assigns = {})
57
73
  assigns = deep_stringify_assigns(assigns)
58
74
 
59
- result = {
75
+ {
60
76
  from: from,
61
77
  cc: cc.presence,
62
78
  bcc: bcc.presence,
@@ -74,6 +90,18 @@ module Effective
74
90
  end.flatten.uniq.compact
75
91
  end
76
92
 
93
+ def save_as_html!
94
+ assign_attributes(content_type: 'text/html')
95
+ assign_attributes(body: simple_format(body)) if body_plain?
96
+ save!
97
+ end
98
+
99
+ def save_as_plain!
100
+ assign_attributes(content_type: 'text/plain')
101
+ assign_attributes(body: strip_tags(body)) if body_html?
102
+ save!
103
+ end
104
+
77
105
  private
78
106
 
79
107
  def template_body
@@ -9,21 +9,22 @@
9
9
  = f.text_field :bcc
10
10
 
11
11
  - prefix_hint = EffectiveResources.mailer_subject_prefix_hint.to_s.strip.presence
12
+ = f.text_field :subject, hint: (prefix_hint ? "The subject of your email. It will be automatically prefixed with: #{prefix_hint}" : 'The subject of your email')
12
13
 
13
- = f.text_field :subject,
14
- hint: (prefix_hint ? "The subject of your email. It will be automatically prefixed with #{prefix_hint}." : 'The subject of your email')
14
+ = f.show_if(:content_type, Effective::EmailTemplate::CONTENT_TYPE_PLAIN) do
15
+ = f.text_area :body, hint: 'The text/plain content of your email template', rows: 10
16
+
17
+ = f.show_if(:content_type, Effective::EmailTemplate::CONTENT_TYPE_HTML) do
18
+ = f.article_editor :body, hint: 'The text/html content of your email template', mode: :email
15
19
 
16
- = f.text_area :body, hint: 'The content of your email template', rows: 10
20
+ = card do
21
+ %p The available variables for this email template are:
17
22
 
18
- .card
19
- .card-body
20
- %p The available variables for this email template are:
23
+ %ul
24
+ - Array(f.object.template_variables).each do |variable|
25
+ %li {{ #{variable} }}
21
26
 
22
- %ul
23
- - Array(f.object.template_variables).each do |variable|
24
- %li {{ #{variable} }}
25
-
26
- %small.text-muted Please contact us to add additional variables
27
+ %small.text-muted Please contact us to add additional variables
27
28
 
28
29
  = f.submit do
29
30
  = f.save 'Save'
@@ -1,3 +1,3 @@
1
1
  module EffectiveEmailTemplates
2
- VERSION = '1.7.0'.freeze
2
+ VERSION = '1.9.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_email_templates
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.0
4
+ version: 1.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-26 00:00:00.000000000 Z
11
+ date: 2024-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: haml
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: pry-byebug
113
127
  requirement: !ruby/object:Gem::Requirement