effective_email_templates 1.0.7 → 1.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f32ae5d86811f78123ceee885529f4dccd62c61031b9504400d984804af7cb1
4
- data.tar.gz: a56f822ae7376f5f751721d6a26a5cfdf50c6fd2dceafe3016a0334002275bd4
3
+ metadata.gz: 4928f9db162ceb9b1b35fcbe367f38689be1f8977f75e46bff4e734c15f7ec90
4
+ data.tar.gz: 9129d4a60362284332fc2ab16685f0c325846dfe6f199cdd1e3924c974e3fb6e
5
5
  SHA512:
6
- metadata.gz: f5096521a7df9bb7b3da61b121dabfdbf2b085ada6bcae15af9a0e1145b0e59d69c699a62ab9dfdf10cbe1fac444b8292b839cd833528bc07a11b5f5b6d23ed2
7
- data.tar.gz: 279d24d7de7b3cedd6da782fa609acb8200dc666b615a0573f7b8bcc17af7be78f177b4a2a70b60adbf54fc5d78c2d933e88847b5bebe0f7569b916e791b0816
6
+ metadata.gz: 963de88d3ca0956517777a2445018ecf36bece45bf40165007403b49c347ed4a217d3d6438518318cedb25354694473639da50845cab41cd29173575192acd8c
7
+ data.tar.gz: 06f6f69fea13c1405bb5e93576ed5779ca412db1955b7543effa2ced254a4af7e781154df370ae97cba887dfb962f29ca424d140040213b3442388f17e640d39
@@ -14,6 +14,8 @@ class EffectiveEmailTemplatesDatatable < Effective::Datatable
14
14
  col :subject
15
15
  col :body
16
16
 
17
+ col :content_type, visible: false
18
+
17
19
  actions_col partial: '/admin/email_templates/actions', partial_as: 'email_template'
18
20
  end
19
21
 
@@ -21,4 +23,3 @@ class EffectiveEmailTemplatesDatatable < Effective::Datatable
21
23
  Effective::EmailTemplate.all
22
24
  end
23
25
  end
24
-
@@ -1,11 +1,20 @@
1
1
  module EffectiveEmailTemplatesHelper
2
2
 
3
- def effective_email_review_fields(form, template_name)
3
+ # We are given a form to essentially send an email
4
+ def effective_email_review_fields(form, template_name, mail = nil)
5
+ raise('expected a mail object') if mail && !mail.kind_of?(ActionMailer::MessageDelivery)
6
+ raise('expected form.object to respond to email_review') unless form.object.respond_to?(:email_review)
7
+
4
8
  email_review = form.object.email_review
5
9
 
6
10
  unless email_review&.template_name == template_name.to_s
7
11
  email_template = Effective::EmailTemplate.where(template_name: template_name).first!
8
12
  email_review = Effective::EmailReview.build(email_template: email_template)
13
+
14
+ if mail.present?
15
+ email_review.body = mail.message.body
16
+ email_review.subject = mail.message.subject
17
+ end
9
18
  end
10
19
 
11
20
  render(partial: 'effective/email_reviews/fields', locals: { email_review: email_review, form: form })
@@ -4,25 +4,32 @@ module Effective
4
4
 
5
5
  attr_accessor :email_template
6
6
  attr_accessor :template_name
7
+
7
8
  attr_accessor :body
9
+ attr_accessor :subject
10
+ attr_accessor :from
11
+ attr_accessor :cc
12
+ attr_accessor :bcc
8
13
 
9
14
  def self.build(attributes = {})
10
- new(attributes).tap do |email_review|
11
- email_review.body ||= email_review.email_template&.body
12
- email_review.template_name ||= email_review.email_template&.template_name
13
- end
14
- end
15
+ email_review = new(attributes)
16
+ template = email_review.email_template
15
17
 
16
- validates :body, presence: true
17
-
18
- validate(if: -> { body.present? }) do
19
- begin
20
- Liquid::Template.parse(body)
21
- rescue Liquid::SyntaxError => e
22
- errors.add(:body, e.message)
18
+ if template.present?
19
+ email_review.body ||= template.body
20
+ email_review.subject ||= template.subject
21
+ email_review.from ||= template.from
22
+ email_review.cc ||= template.cc
23
+ email_review.bcc ||= template.bcc
24
+ email_review.template_name ||= template.template_name
23
25
  end
26
+
27
+ email_review
24
28
  end
25
29
 
30
+ validates :body, presence: true, liquid: true
31
+ validates :subject, liquid: true
32
+
26
33
  def email_template
27
34
  @email_template ||= Effective::EmailTemplate.where(template_name: template_name).first
28
35
  end
@@ -22,21 +22,8 @@ module Effective
22
22
  self.content_type ||= CONTENT_TYPES.first
23
23
  end
24
24
 
25
- before_validation(if: -> { body.present? }) do
26
- begin
27
- Liquid::Template.parse(body)
28
- rescue Liquid::SyntaxError => e
29
- errors.add(:body, e.message)
30
- end
31
- end
32
-
33
- before_validation(if: -> { subject.present? }) do
34
- begin
35
- Liquid::Template.parse(subject)
36
- rescue Liquid::SyntaxError => e
37
- errors.add(:subject, e.message)
38
- end
39
- end
25
+ validates :body, liquid: true
26
+ validates :subject, liquid: true
40
27
 
41
28
  validates :subject, presence: true
42
29
  validates :from, presence: true
@@ -44,6 +31,20 @@ module Effective
44
31
  validates :content_type, presence: true, inclusion: { in: CONTENT_TYPES }
45
32
  validates :template_name, presence: true
46
33
 
34
+ # validate(if: -> { content_type == 'text/html' && body.present? }) do
35
+ # unless body.include?('<') && body.include?('>')
36
+ # self.errors.add(:content_type, 'expected html tags in body')
37
+ # self.errors.add(:body, 'expected html tags in body')
38
+ # end
39
+ # end
40
+
41
+ # validate(if: -> { content_type == 'text/plain' && body.present? }) do
42
+ # if body.include?('</a>') || body.include?('</p>')
43
+ # self.errors.add(:content_type, 'expected no html tags in body')
44
+ # self.errors.add(:body, 'expected no html tags in body')
45
+ # end
46
+ # end
47
+
47
48
  def to_s
48
49
  template_name.presence || 'New Email Template'
49
50
  end
@@ -51,14 +52,14 @@ module Effective
51
52
  def render(assigns = {})
52
53
  assigns = deep_stringify_assigns(assigns)
53
54
 
54
- {
55
+ result = {
55
56
  from: from,
56
- cc: cc.presence || false,
57
- bcc: bcc.presence || false,
57
+ cc: cc.presence,
58
+ bcc: bcc.presence,
58
59
  content_type: content_type,
59
60
  subject: template_subject.render(assigns),
60
61
  body: template_body.render(assigns)
61
- }
62
+ }.compact
62
63
  end
63
64
 
64
65
  def template_variables
@@ -72,11 +73,11 @@ module Effective
72
73
  private
73
74
 
74
75
  def template_body
75
- Liquid::Template.parse(body)
76
+ Liquid::Template.parse(body_was || body)
76
77
  end
77
78
 
78
79
  def template_subject
79
- Liquid::Template.parse(subject)
80
+ Liquid::Template.parse(subject_was || subject)
80
81
  end
81
82
 
82
83
  def deep_stringify_assigns(assigns)
@@ -1,6 +1,8 @@
1
1
  = effective_form_with(model: email_template, url: email_template.persisted? ? effective_email_templates.admin_email_template_path(email_template.id) : effective_email_templates.admin_email_templates_path) do |f|
2
2
  = f.static_field :template_name, label: 'Name'
3
- = f.select :content_type, Effective::EmailTemplate::CONTENT_TYPES
3
+
4
+ - if EffectiveEmailTemplates.select_content_type
5
+ = f.select :content_type, Effective::EmailTemplate::CONTENT_TYPES
4
6
 
5
7
  = f.email_field :from, hint: 'Whom the email will be sent from'
6
8
  = f.text_field :cc
@@ -32,4 +32,7 @@ EffectiveEmailTemplates.setup do |config|
32
32
  admin_email_templates: 'admin'
33
33
  }
34
34
 
35
+ # Not allowed to select text/html by default
36
+ config.select_content_type = false
37
+
35
38
  end
@@ -6,6 +6,7 @@ module EffectiveEmailTemplates
6
6
 
7
7
  mattr_accessor :email_templates_table_name
8
8
  mattr_accessor :authorization_method
9
+ mattr_accessor :select_content_type
9
10
  mattr_accessor :layout
10
11
 
11
12
  def self.setup
@@ -2,6 +2,9 @@ module EffectiveEmailTemplates
2
2
  class Engine < ::Rails::Engine
3
3
  engine_name 'effective_email_templates'
4
4
 
5
+ config.autoload_paths += Dir["#{config.root}/lib/validators/"]
6
+ config.eager_load_paths += Dir["#{config.root}/lib/validators/"]
7
+
5
8
  # Set up our default configuration options.
6
9
  initializer 'effective_email_templates.defaults', before: :load_config_initializers do |app|
7
10
  eval File.read("#{config.root}/config/effective_email_templates.rb")
@@ -16,4 +19,3 @@ module EffectiveEmailTemplates
16
19
 
17
20
  end
18
21
  end
19
-
@@ -9,16 +9,18 @@ module EffectiveEmailTemplates
9
9
  end
10
10
 
11
11
  def execute(overwrite:, quiet: false)
12
- Dir[Rails.root.join('app', 'views', '**', '*.liquid')].each do |filepath|
13
- name = File.basename(filepath, '.liquid')
14
- email_template = Effective::EmailTemplate.find_or_initialize_by(template_name: name)
12
+ ActionController::Base.view_paths.map(&:path).each do |path|
13
+ Dir[Rails.root.join(path, '**', '*_mailer/', '*.liquid')].each do |filepath|
14
+ name = File.basename(filepath, '.liquid')
15
+ email_template = Effective::EmailTemplate.find_or_initialize_by(template_name: name)
15
16
 
16
- if email_template.persisted? && !overwrite
17
- puts("SKIPPED #{filename(filepath)}") unless quiet
18
- next
19
- end
17
+ if email_template.persisted? && !overwrite
18
+ puts("SKIPPED #{filename(filepath)}") unless quiet
19
+ next
20
+ end
20
21
 
21
- save(email_template, filepath, quiet: quiet)
22
+ save(email_template, filepath, quiet: quiet)
23
+ end
22
24
  end
23
25
  end
24
26
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveEmailTemplates
2
- VERSION = '1.0.7'.freeze
2
+ VERSION = '1.0.12'.freeze
3
3
  end
@@ -0,0 +1,15 @@
1
+ # An ActiveRecord validator for any liquid field that you would use with effective_email_templates or otherwise
2
+ #
3
+ # validates :body, liquid: true
4
+
5
+ class LiquidValidator < ActiveModel::EachValidator
6
+ def validate_each(record, attribute, value)
7
+ if value.present?
8
+ begin
9
+ Liquid::Template.parse(value)
10
+ rescue Liquid::SyntaxError => e
11
+ record.errors.add(attribute, e.message)
12
+ end
13
+ end
14
+ end
15
+ 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.0.7
4
+ version: 1.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-17 00:00:00.000000000 Z
11
+ date: 2021-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -115,11 +115,12 @@ files:
115
115
  - lib/effective_email_templates/version.rb
116
116
  - lib/generators/effective_email_templates/install_generator.rb
117
117
  - lib/tasks/effective_email_templates_tasks.rake
118
+ - lib/validators/liquid_validator.rb
118
119
  homepage: https://github.com/code-and-effect/effective_email_templates
119
120
  licenses:
120
121
  - MIT
121
122
  metadata: {}
122
- post_install_message:
123
+ post_install_message:
123
124
  rdoc_options: []
124
125
  require_paths:
125
126
  - lib
@@ -134,8 +135,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
135
  - !ruby/object:Gem::Version
135
136
  version: '0'
136
137
  requirements: []
137
- rubygems_version: 3.0.3
138
- signing_key:
138
+ rubygems_version: 3.1.2
139
+ signing_key:
139
140
  specification_version: 4
140
141
  summary: Effective Email Templates provides an admin access to modify email templates
141
142
  test_files: []