effective_email_templates 1.0.2 → 1.0.3

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: eac065491013a9a2c051a9c35e24bdf06dab0df7a5e99b4c5e94c90611207d4b
4
- data.tar.gz: 4a461d58c903499e753d86563b315c43e4067a53bff47d39a134b575b8bf6f76
3
+ metadata.gz: d25efc5fcbfdd39c8db6714777574361d0658757e68cfee0e88c075c0b84faa4
4
+ data.tar.gz: fc54755485ddc9a80b081ea1448ce4b2ecbccdb546c1b08146dbc36982499100
5
5
  SHA512:
6
- metadata.gz: 86639e64193989056fe756a73602823431d7d6d4efd86f7fa02e52c4f09f707ee94b94640be9fbd79fe5092b3fe603357dae11bc01b816ebc8019fa3ec39d18b
7
- data.tar.gz: '06669f2e153895afc3db34e147b63d017e7930c68c09cd464dc2e2ff0ff185462b7e9562244f3834fa6f47b19301368c8328f9c3752cd0d6ae5f3f4d22cf04b7'
6
+ metadata.gz: 6727c12728c12a03a74c9b126b0a16a3c2bf12b16e2dcb8efc53c66b3f9efef9a0f71df34c4a97bf3336b6e1586e606b1ae91e3cbaccfbc833c4c0653c273f51
7
+ data.tar.gz: cc09e802861ebbfb5564a6670fb8422c9acf9f522a5230ffdeeb56a0c5e5af7f42d80826335c4ada8cb36649b45a3589e7395228ce45b9541e0d9441d4c70a0a
@@ -0,0 +1,14 @@
1
+ module EffectiveEmailTemplatesHelper
2
+
3
+ def effective_email_review_fields(form, template_name)
4
+ email_review = form.object.email_review
5
+
6
+ email_review ||= begin
7
+ email_template = Effective::EmailTemplate.where(template_name: template_name).first!
8
+ form.object.build_email_review(email_template: email_template)
9
+ end
10
+
11
+ render(partial: 'effective/email_reviews/fields', locals: { email_review: email_review, form: form })
12
+ end
13
+
14
+ end
@@ -4,7 +4,16 @@ module Effective
4
4
  def mail(headers = {}, &block)
5
5
  email_template = Effective::EmailTemplate.where(template_name: action_name).first!
6
6
 
7
- assigns = route_url_assigns(email_template).merge(@assigns || {})
7
+ # Parse Assigns. :body is a special key
8
+ assigns = (@assigns || {})
9
+
10
+ if (body = assigns.delete(:body))
11
+ email_template.body = body
12
+ end
13
+
14
+ assigns = route_url_assigns(email_template).merge(assigns)
15
+
16
+ # Render from the template, possibly with updated body
8
17
  rendered = email_template.render(assigns)
9
18
 
10
19
  super(rendered.merge(headers))
@@ -0,0 +1,30 @@
1
+ # HasOneEmailReview
2
+ # Allows any model to easily review an email template and make changes to the body
3
+
4
+ module HasOneEmailReview
5
+ extend ActiveSupport::Concern
6
+
7
+ module ActiveRecord
8
+ def has_one_email_review
9
+ include ::HasOneEmailReview
10
+ end
11
+ end
12
+
13
+ included do
14
+ attr_accessor :email_review
15
+
16
+ validate(if: -> { email_review.present? }) do
17
+ self.errors.add(:base, 'reviewed email is invalid') unless email_review.valid?
18
+ end
19
+ end
20
+
21
+ def build_email_review(atts = {})
22
+ self.email_review ||= Effective::EmailReview.build(atts)
23
+ end
24
+
25
+ def email_review_attributes=(atts)
26
+ self.email_review = Effective::EmailReview.new(atts)
27
+ end
28
+
29
+ end
30
+
@@ -0,0 +1,25 @@
1
+ module Effective
2
+ class EmailReview
3
+ include ActiveModel::Model
4
+
5
+ attr_accessor :email_template
6
+ attr_accessor :body
7
+
8
+ def self.build(attributes = {})
9
+ new(attributes).tap do |email_review|
10
+ email_review.body ||= email_review.email_template&.body
11
+ end
12
+ end
13
+
14
+ validates :body, presence: true
15
+
16
+ validate(if: -> { body.present? }) do
17
+ begin
18
+ Liquid::Template.parse(body)
19
+ rescue Liquid::SyntaxError => e
20
+ errors.add(:body, e.message)
21
+ end
22
+ end
23
+
24
+ end
25
+ end
@@ -4,10 +4,6 @@ module Effective
4
4
 
5
5
  CONTENT_TYPES = ['text/plain', 'text/html']
6
6
 
7
- serialize :template_body, Liquid::Template
8
- serialize :template_subject, Liquid::Template
9
- serialize :template_variables, Array
10
-
11
7
  # Attributes
12
8
  # subject :string
13
9
  # from :string
@@ -16,21 +12,17 @@ module Effective
16
12
  # body :text
17
13
  # content_type :string
18
14
  #
19
- # template_name :string
20
- # template_body :text
21
- # template_subject :text
22
- # template_variables :text
15
+ # template_name :string
23
16
  #
24
17
  # timestamps
25
18
 
26
19
  before_validation do
27
- self.template_name ||= subject.to_s.parameterize
28
20
  self.content_type ||= CONTENT_TYPES.first
29
21
  end
30
22
 
31
23
  before_validation(if: -> { body.present? }) do
32
24
  begin
33
- self.template_body = Liquid::Template.parse(body)
25
+ Liquid::Template.parse(body)
34
26
  rescue Liquid::SyntaxError => e
35
27
  errors.add(:body, e.message)
36
28
  end
@@ -38,7 +30,7 @@ module Effective
38
30
 
39
31
  before_validation(if: -> { subject.present? }) do
40
32
  begin
41
- self.template_subject = Liquid::Template.parse(subject)
33
+ Liquid::Template.parse(subject)
42
34
  rescue Liquid::SyntaxError => e
43
35
  errors.add(:subject, e.message)
44
36
  end
@@ -48,21 +40,14 @@ module Effective
48
40
  validates :from, presence: true
49
41
  validates :body, presence: true
50
42
  validates :content_type, presence: true, inclusion: { in: CONTENT_TYPES }
51
-
52
43
  validates :template_name, presence: true
53
- validates :template_body, presence: true
54
- validates :template_subject, presence: true
55
-
56
- before_save do
57
- self.template_variables = find_template_variables
58
- end
59
44
 
60
45
  def to_s
61
46
  template_name.presence || 'New Email Template'
62
47
  end
63
48
 
64
49
  def render(assigns = {})
65
- assigns = assigns.deep_stringify_keys() if assigns.respond_to?(:deep_stringify_keys)
50
+ assigns = deep_stringify_assigns(assigns)
66
51
 
67
52
  {
68
53
  from: from,
@@ -74,9 +59,7 @@ module Effective
74
59
  }
75
60
  end
76
61
 
77
- private
78
-
79
- def find_template_variables
62
+ def template_variables
80
63
  [template_body.presence, template_subject.presence].compact.map do |template|
81
64
  Liquid::ParseTreeVisitor.for(template.root).add_callback_for(Liquid::VariableLookup) do |node|
82
65
  [node.name, *node.lookups].join('.')
@@ -84,6 +67,30 @@ module Effective
84
67
  end.flatten.uniq.compact
85
68
  end
86
69
 
70
+ private
71
+
72
+ def template_body
73
+ Liquid::Template.parse(body)
74
+ end
75
+
76
+ def template_subject
77
+ Liquid::Template.parse(subject)
78
+ end
79
+
80
+ def deep_stringify_assigns(assigns)
81
+ if assigns.respond_to?(:deep_stringify_keys!)
82
+ assigns.deep_stringify_keys!
83
+ end
84
+
85
+ if assigns.respond_to?(:deep_transform_values!)
86
+ assigns.deep_transform_values! do |value|
87
+ value.kind_of?(ActiveRecord::Base) ? value.to_s : value
88
+ end
89
+ end
90
+
91
+ assigns
92
+ end
93
+
87
94
  end
88
95
 
89
96
  end
@@ -0,0 +1,2 @@
1
+ = form.fields_for(:email_review, email_review) do |f|
2
+ = f.text_area :body, rows: 10
@@ -1,6 +1,8 @@
1
1
  class CreateEffectiveEmailTemplates < ActiveRecord::Migration[4.2]
2
2
  def self.up
3
3
  create_table <%= @email_templates_table_name %> do |t|
4
+ t.string :template_name
5
+
4
6
  t.string :subject
5
7
  t.string :from
6
8
  t.string :bcc
@@ -9,11 +11,6 @@ class CreateEffectiveEmailTemplates < ActiveRecord::Migration[4.2]
9
11
  t.string :content_type
10
12
  t.text :body
11
13
 
12
- t.string :template_name
13
- t.text :template_body
14
- t.text :template_subject
15
- t.text :template_variables
16
-
17
14
  t.timestamps
18
15
  end
19
16
 
@@ -7,6 +7,13 @@ module EffectiveEmailTemplates
7
7
  eval File.read("#{config.root}/config/effective_email_templates.rb")
8
8
  end
9
9
 
10
+ # Include has_one_email_review concern and allow any ActiveRecord object to call it
11
+ initializer 'effective_email_templates.active_record' do |app|
12
+ ActiveSupport.on_load :active_record do
13
+ ActiveRecord::Base.extend(HasOneEmailReview::ActiveRecord)
14
+ end
15
+ end
16
+
10
17
  end
11
18
  end
12
19
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveEmailTemplates
2
- VERSION = '1.0.2'.freeze
2
+ VERSION = '1.0.3'.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.0.2
4
+ version: 1.0.3
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: 2020-03-18 00:00:00.000000000 Z
11
+ date: 2020-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -91,13 +91,17 @@ files:
91
91
  - README.md
92
92
  - app/controllers/admin/email_templates_controller.rb
93
93
  - app/datatables/effective_email_templates_datatable.rb
94
+ - app/helpers/effective_email_templates_helper.rb
94
95
  - app/mailers/effective/email_templates_mailer.rb
96
+ - app/models/concerns/has_one_email_review.rb
95
97
  - app/models/effective/access_denied.rb
98
+ - app/models/effective/email_review.rb
96
99
  - app/models/effective/email_template.rb
97
100
  - app/views/admin/email_templates/_actions.html.haml
98
101
  - app/views/admin/email_templates/_form.html.haml
99
102
  - app/views/admin/email_templates/edit.html.haml
100
103
  - app/views/admin/email_templates/index.html.haml
104
+ - app/views/effective/email_reviews/_fields.html.haml
101
105
  - app/views/layouts/effective_email_templates_mailer_layout.html.haml
102
106
  - config/effective_email_templates.rb
103
107
  - config/email_templates_mailer.rb