effective_email_templates 1.0.12 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/README.md +3 -49
- data/app/controllers/admin/email_templates_controller.rb +8 -76
- data/app/datatables/effective_email_templates_datatable.rb +18 -5
- data/app/mailers/concerns/effective_email_templates_mailer.rb +35 -0
- data/app/mailers/effective/email_templates_mailer.rb +1 -27
- data/app/models/effective/email_template.rb +17 -14
- data/app/views/admin/email_templates/_form.html.haml +1 -1
- data/config/effective_email_templates.rb +1 -29
- data/lib/effective_email_templates/importer.rb +10 -6
- data/lib/effective_email_templates/version.rb +1 -1
- data/lib/effective_email_templates.rb +8 -27
- metadata +3 -6
- data/app/models/effective/access_denied.rb +0 -17
- data/app/views/admin/email_templates/_actions.html.haml +0 -5
- data/app/views/admin/email_templates/edit.html.haml +0 -3
- data/app/views/admin/email_templates/index.html.haml +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27a4c6ef0659bba64c76e45a9363fe649d2316ac988ef56bcd3fae6e87efe13f
|
4
|
+
data.tar.gz: effe7e9aa525fae2923b0e195ea3ec81ed492944e2cf7616f3db7af58197218d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a31ed5dba96c21b94f282faf4023e66de6fd809e1f15ffdaf22d6179d158d0c82c01535fced61ca074d770b02ab5c9188919bca703a00ff5ae982cdf8f89d3c0
|
7
|
+
data.tar.gz: 8f5cf72c409cc8d4ef3a52ef12068af275963d7e1ee5b66efd96268b812df598c794eaea5fa612e8e314fe54a8a584fed7dd43216b07e7db864e9f163f21dc82
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -114,67 +114,21 @@ EmailTemplatesMailer.welcome(user).deliver
|
|
114
114
|
|
115
115
|
## Authorization
|
116
116
|
|
117
|
-
All authorization checks are handled via the
|
117
|
+
All authorization checks are handled via the effective_resources gem found in the `config/initializers/effective_resources.rb` file.
|
118
118
|
|
119
|
-
It is intended for flow through to CanCan or Pundit, but neither of those gems are required.
|
120
|
-
|
121
|
-
This method is called by all controller actions with the appropriate action and resource
|
122
|
-
|
123
|
-
Action will be one of [:index, :show, :new, :create, :edit, :update, :destroy]
|
124
|
-
|
125
|
-
Resource will the appropriate Effective::EmailTemplate object or class
|
126
|
-
|
127
|
-
The authorization method is defined in the initializer file:
|
128
|
-
|
129
|
-
```ruby
|
130
|
-
# As a Proc (with CanCan)
|
131
|
-
config.authorization_method = Proc.new { |controller, action, resource| authorize!(action, resource) }
|
132
|
-
```
|
133
|
-
|
134
|
-
```ruby
|
135
|
-
# As a Custom Method
|
136
|
-
config.authorization_method = :my_authorization_method
|
137
|
-
```
|
138
|
-
|
139
|
-
and then in your application_controller.rb:
|
140
|
-
|
141
|
-
```ruby
|
142
|
-
def my_authorization_method(action, resource)
|
143
|
-
current_user.is?(:admin) || EffectivePunditPolicy.new(current_user, resource).send('#{action}?')
|
144
|
-
end
|
145
|
-
```
|
146
|
-
|
147
|
-
or disabled entirely:
|
148
|
-
|
149
|
-
```ruby
|
150
|
-
config.authorization_method = false
|
151
|
-
```
|
152
|
-
|
153
|
-
If the method or proc returns false (user is not authorized) an Effective::AccessDenied exception will be raised
|
154
|
-
|
155
|
-
You can rescue from this exception by adding the following to your application_controller.rb:
|
156
|
-
|
157
|
-
```ruby
|
158
|
-
rescue_from Effective::AccessDenied do |exception|
|
159
|
-
respond_to do |format|
|
160
|
-
format.html { render 'static_pages/access_denied', :status => 403 }
|
161
|
-
format.any { render :text => 'Access Denied', :status => 403 }
|
162
|
-
end
|
163
|
-
end
|
164
|
-
```
|
165
119
|
|
166
120
|
### Permissions
|
167
121
|
|
168
122
|
To allow a user to see the admin area, using CanCan:
|
169
123
|
|
170
124
|
```ruby
|
171
|
-
can :
|
125
|
+
can [:index, :edit, :update, :destroy], Effective::EmailTemplate
|
172
126
|
can :admin, :effective_email_templates
|
173
127
|
```
|
174
128
|
|
175
129
|
## License
|
176
130
|
|
177
|
-
MIT License.
|
131
|
+
MIT License. Copyright [Code and Effect Inc.](http://www.codeandeffect.com/)
|
178
132
|
|
179
133
|
|
180
134
|
## Contributing
|
@@ -1,87 +1,19 @@
|
|
1
1
|
module Admin
|
2
2
|
class EmailTemplatesController < ApplicationController
|
3
|
-
before_action
|
3
|
+
before_action(:authenticate_user!) if defined?(Devise)
|
4
|
+
before_action { EffectiveResources.authorize!(self, :admin, :effective_email_templates) }
|
4
5
|
|
5
|
-
|
6
|
+
include Effective::CrudController
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
@page_title = 'Email Templates'
|
10
|
-
|
11
|
-
authorize_effective_email_templates!
|
12
|
-
end
|
13
|
-
|
14
|
-
def new
|
15
|
-
@email_template = Effective::EmailTemplate.new
|
16
|
-
@page_title = 'New Email Template'
|
17
|
-
|
18
|
-
authorize_effective_email_templates!
|
19
|
-
end
|
20
|
-
|
21
|
-
def create
|
22
|
-
@email_template = Effective::EmailTemplate.new(email_template_params)
|
23
|
-
@page_title = 'New Email Template'
|
24
|
-
|
25
|
-
authorize_effective_email_templates!
|
26
|
-
|
27
|
-
if @email_template.save
|
28
|
-
flash[:success] = 'Successfully created email template'
|
29
|
-
redirect_to effective_email_templates.admin_email_templates_path
|
30
|
-
else
|
31
|
-
flash.now[:danger] = 'Unable to create email template'
|
32
|
-
render :new
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def edit
|
37
|
-
@email_template =
|
38
|
-
Effective::EmailTemplate.where(id: params[:id]).or(
|
39
|
-
Effective::EmailTemplate.where(template_name: params[:id])
|
40
|
-
).first!
|
41
|
-
|
42
|
-
@page_title = 'Edit Email Template'
|
43
|
-
|
44
|
-
authorize_effective_email_templates!
|
8
|
+
if (config = EffectiveEmailTemplates.layout)
|
9
|
+
layout(config.kind_of?(Hash) ? config[:admin] : config)
|
45
10
|
end
|
46
11
|
|
47
|
-
|
48
|
-
|
49
|
-
@page_title = 'Edit Email Template'
|
50
|
-
|
51
|
-
authorize_effective_email_templates!
|
52
|
-
|
53
|
-
if @email_template.update(email_template_params)
|
54
|
-
flash[:success] = 'Successfully updated email template'
|
55
|
-
redirect_to effective_email_templates.admin_email_templates_path
|
56
|
-
else
|
57
|
-
flash.now[:danger] = 'Unable to update email template'
|
58
|
-
render :edit
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def destroy
|
63
|
-
@email_template = Effective::EmailTemplate.find(params[:id])
|
64
|
-
|
65
|
-
authorize_effective_email_templates!
|
66
|
-
|
67
|
-
if @email_template.destroy
|
68
|
-
flash[:success] = 'Successfully deleted email template'
|
69
|
-
else
|
70
|
-
flash[:danger] = 'Unable to delete email template'
|
71
|
-
end
|
72
|
-
|
73
|
-
redirect_to effective_email_templates.admin_email_templates_path
|
74
|
-
end
|
75
|
-
|
76
|
-
private
|
77
|
-
|
78
|
-
def authorize_effective_email_templates!
|
79
|
-
EffectiveEmailTemplates.authorize!(self, :admin, :effective_email_templates)
|
80
|
-
EffectiveEmailTemplates.authorize!(self, action_name.to_sym, @email_template || Effective::EmailTemplate)
|
81
|
-
end
|
12
|
+
submit :save, 'Save'
|
13
|
+
submit :save, 'Save and Add New', redirect: :new
|
82
14
|
|
83
15
|
def email_template_params
|
84
|
-
params.require(:effective_email_template).permit
|
16
|
+
params.require(:effective_email_template).permit!
|
85
17
|
end
|
86
18
|
|
87
19
|
end
|
@@ -8,15 +8,28 @@ class EffectiveEmailTemplatesDatatable < Effective::Datatable
|
|
8
8
|
col :id, visible: false
|
9
9
|
|
10
10
|
col :template_name, label: 'Name'
|
11
|
-
|
12
|
-
col :
|
13
|
-
|
11
|
+
|
12
|
+
col :from do |email_template|
|
13
|
+
html_escape(email_template.from)
|
14
|
+
end
|
15
|
+
|
16
|
+
col :cc do |email_template|
|
17
|
+
html_escape(email_template.cc)
|
18
|
+
end
|
19
|
+
|
20
|
+
col :bcc do |email_template|
|
21
|
+
html_escape(email_template.bcc)
|
22
|
+
end
|
23
|
+
|
14
24
|
col :subject
|
15
|
-
|
25
|
+
|
26
|
+
col :body do |email_template|
|
27
|
+
simple_format(email_template.body)
|
28
|
+
end
|
16
29
|
|
17
30
|
col :content_type, visible: false
|
18
31
|
|
19
|
-
actions_col
|
32
|
+
actions_col
|
20
33
|
end
|
21
34
|
|
22
35
|
collection do
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# HasOneEmailReview
|
2
|
+
# Allows any model to easily review an email template and make changes to the body
|
3
|
+
|
4
|
+
module EffectiveEmailTemplatesMailer
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def mail(headers = {}, &block)
|
8
|
+
email_template = Effective::EmailTemplate.where(template_name: action_name).first!
|
9
|
+
assigns = (@assigns || {})
|
10
|
+
|
11
|
+
# Parse assigns. Special keys for body and subject.
|
12
|
+
body = assigns.delete(:body) || headers[:body] || headers['body']
|
13
|
+
email_template.body = body if body.present?
|
14
|
+
|
15
|
+
subject = assigns.delete(:subject) || headers[:subject] || headers['subject']
|
16
|
+
email_template.subject = subject if subject.present?
|
17
|
+
|
18
|
+
# Add any _url helpers
|
19
|
+
assigns = route_url_assigns(email_template).merge(assigns)
|
20
|
+
|
21
|
+
# Render from the template, possibly with updated body
|
22
|
+
rendered = email_template.render(assigns)
|
23
|
+
|
24
|
+
super(rendered.merge(headers.except(:body, :subject, 'body', 'subject')))
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def route_url_assigns(email_template)
|
30
|
+
email_template.template_variables.select { |name| name.ends_with?('_url') }.inject({}) do |h, name|
|
31
|
+
h[name] = public_send(name) if respond_to?(name); h
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -1,31 +1,5 @@
|
|
1
1
|
module Effective
|
2
2
|
class EmailTemplatesMailer < ::ActionMailer::Base
|
3
|
-
|
4
|
-
def mail(headers = {}, &block)
|
5
|
-
email_template = Effective::EmailTemplate.where(template_name: action_name).first!
|
6
|
-
|
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
|
17
|
-
rendered = email_template.render(assigns)
|
18
|
-
|
19
|
-
super(rendered.merge(headers))
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def route_url_assigns(email_template)
|
25
|
-
email_template.template_variables.select { |name| name.ends_with?('_url') }.inject({}) do |h, name|
|
26
|
-
h[name] = public_send(name) if respond_to?(name); h
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
3
|
+
include EffectiveEmailTemplatesMailer
|
30
4
|
end
|
31
5
|
end
|
@@ -2,21 +2,24 @@ module Effective
|
|
2
2
|
class EmailTemplate < ActiveRecord::Base
|
3
3
|
self.table_name = EffectiveEmailTemplates.email_templates_table_name.to_s
|
4
4
|
|
5
|
+
attr_accessor :current_user
|
6
|
+
|
5
7
|
log_changes if respond_to?(:log_changes)
|
6
8
|
|
7
9
|
CONTENT_TYPES = ['text/plain', 'text/html']
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
11
|
+
effective_resource do
|
12
|
+
template_name :string
|
13
|
+
content_type :string
|
14
|
+
|
15
|
+
subject :string
|
16
|
+
from :string
|
17
|
+
cc :string
|
18
|
+
bcc :string
|
19
|
+
body :text
|
20
|
+
|
21
|
+
timestamps
|
22
|
+
end
|
20
23
|
|
21
24
|
before_validation do
|
22
25
|
self.content_type ||= CONTENT_TYPES.first
|
@@ -26,7 +29,7 @@ module Effective
|
|
26
29
|
validates :subject, liquid: true
|
27
30
|
|
28
31
|
validates :subject, presence: true
|
29
|
-
validates :from, presence: true
|
32
|
+
validates :from, presence: true, email: true
|
30
33
|
validates :body, presence: true
|
31
34
|
validates :content_type, presence: true, inclusion: { in: CONTENT_TYPES }
|
32
35
|
validates :template_name, presence: true
|
@@ -73,11 +76,11 @@ module Effective
|
|
73
76
|
private
|
74
77
|
|
75
78
|
def template_body
|
76
|
-
Liquid::Template.parse(
|
79
|
+
Liquid::Template.parse(body)
|
77
80
|
end
|
78
81
|
|
79
82
|
def template_subject
|
80
|
-
Liquid::Template.parse(
|
83
|
+
Liquid::Template.parse(subject)
|
81
84
|
end
|
82
85
|
|
83
86
|
def deep_stringify_assigns(assigns)
|
@@ -4,7 +4,7 @@
|
|
4
4
|
- if EffectiveEmailTemplates.select_content_type
|
5
5
|
= f.select :content_type, Effective::EmailTemplate::CONTENT_TYPES
|
6
6
|
|
7
|
-
= f.
|
7
|
+
= f.text_field :from, hint: 'Whom the email will be sent from'
|
8
8
|
= f.text_field :cc
|
9
9
|
= f.text_field :bcc
|
10
10
|
|
@@ -2,37 +2,9 @@ EffectiveEmailTemplates.setup do |config|
|
|
2
2
|
# Configure Database Tables
|
3
3
|
config.email_templates_table_name = :email_templates
|
4
4
|
|
5
|
-
# Authorization Method
|
6
|
-
#
|
7
|
-
# This method is called by all controller actions with the appropriate action and resource
|
8
|
-
# If the method returns false, an Effective::AccessDenied Error will be raised (see README.md for complete info)
|
9
|
-
#
|
10
|
-
# Use via Proc (and with CanCan):
|
11
|
-
# config.authorization_method = Proc.new { |controller, action, resource| can?(action, resource) }
|
12
|
-
#
|
13
|
-
# Use via custom method:
|
14
|
-
# config.authorization_method = :my_authorization_method
|
15
|
-
#
|
16
|
-
# And then in your application_controller.rb:
|
17
|
-
#
|
18
|
-
# def my_authorization_method(action, resource)
|
19
|
-
# current_user.is?(:admin)
|
20
|
-
# end
|
21
|
-
#
|
22
|
-
# Or disable the check completely:
|
23
|
-
# config.authorization_method = false
|
24
|
-
config.authorization_method = Proc.new { |controller, action, resource| authorize!(action, resource) } # CanCanCan
|
25
|
-
|
26
5
|
# Layout Settings
|
27
|
-
#
|
28
|
-
|
29
|
-
# config.layout = 'application' # All EffectiveEmailTemplates controllers will use this layout
|
30
|
-
config.layout = {
|
31
|
-
email_templates: 'application',
|
32
|
-
admin_email_templates: 'admin'
|
33
|
-
}
|
6
|
+
# config.layout = { application: 'application', admin: 'admin' }
|
34
7
|
|
35
8
|
# Not allowed to select text/html by default
|
36
9
|
config.select_content_type = false
|
37
|
-
|
38
10
|
end
|
@@ -1,15 +1,19 @@
|
|
1
1
|
module EffectiveEmailTemplates
|
2
2
|
class Importer
|
3
|
-
def self.import(quiet: false)
|
4
|
-
new().execute(overwrite: false, quiet: quiet)
|
3
|
+
def self.import(quiet: false, paths: nil)
|
4
|
+
new().execute(overwrite: false, paths: paths, quiet: quiet)
|
5
5
|
end
|
6
6
|
|
7
|
-
def self.overwrite(quiet: false)
|
8
|
-
new().execute(overwrite: true, quiet: quiet)
|
7
|
+
def self.overwrite(quiet: false, paths: nil)
|
8
|
+
new().execute(overwrite: true, paths: paths, quiet: quiet)
|
9
9
|
end
|
10
10
|
|
11
|
-
def execute(overwrite:, quiet: false)
|
12
|
-
|
11
|
+
def execute(overwrite:, paths: nil, quiet: false)
|
12
|
+
return false unless ActiveRecord::Base.connection.table_exists?(EffectiveEmailTemplates.email_templates_table_name)
|
13
|
+
|
14
|
+
paths ||= ActionController::Base.view_paths.map(&:path)
|
15
|
+
|
16
|
+
paths.each do |path|
|
13
17
|
Dir[Rails.root.join(path, '**', '*_mailer/', '*.liquid')].each do |filepath|
|
14
18
|
name = File.basename(filepath, '.liquid')
|
15
19
|
email_template = Effective::EmailTemplate.find_or_initialize_by(template_name: name)
|
@@ -1,37 +1,18 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'liquid'
|
2
|
+
require 'effective_resources'
|
3
|
+
require 'effective_email_templates/engine'
|
4
|
+
require 'effective_email_templates/version'
|
4
5
|
|
5
6
|
module EffectiveEmailTemplates
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
mattr_accessor :select_content_type
|
10
|
-
mattr_accessor :layout
|
11
|
-
|
12
|
-
def self.setup
|
13
|
-
yield self
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.authorized?(controller, action, resource)
|
17
|
-
@_exceptions ||= [Effective::AccessDenied, (CanCan::AccessDenied if defined?(CanCan)), (Pundit::NotAuthorizedError if defined?(Pundit))].compact
|
18
|
-
|
19
|
-
return !!authorization_method unless authorization_method.respond_to?(:call)
|
20
|
-
controller = controller.controller if controller.respond_to?(:controller)
|
21
|
-
|
22
|
-
begin
|
23
|
-
!!(controller || self).instance_exec((controller || self), action, resource, &authorization_method)
|
24
|
-
rescue *@_exceptions
|
25
|
-
false
|
26
|
-
end
|
8
|
+
def self.config_keys
|
9
|
+
[:email_templates_table_name, :select_content_type, :layout]
|
27
10
|
end
|
28
11
|
|
29
|
-
|
30
|
-
raise Effective::AccessDenied.new('Access Denied', action, resource) unless authorized?(controller, action, resource)
|
31
|
-
end
|
12
|
+
include EffectiveGem
|
32
13
|
|
33
14
|
def self.permitted_params
|
34
|
-
[:from, :bcc, :cc, :subject, :body, :content_type]
|
15
|
+
@permitted_params ||= [:from, :bcc, :cc, :subject, :body, :content_type]
|
35
16
|
end
|
36
17
|
|
37
18
|
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.
|
4
|
+
version: 1.1.2
|
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:
|
11
|
+
date: 2022-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -92,15 +92,12 @@ files:
|
|
92
92
|
- app/controllers/admin/email_templates_controller.rb
|
93
93
|
- app/datatables/effective_email_templates_datatable.rb
|
94
94
|
- app/helpers/effective_email_templates_helper.rb
|
95
|
+
- app/mailers/concerns/effective_email_templates_mailer.rb
|
95
96
|
- app/mailers/effective/email_templates_mailer.rb
|
96
97
|
- app/models/concerns/has_one_email_review.rb
|
97
|
-
- app/models/effective/access_denied.rb
|
98
98
|
- app/models/effective/email_review.rb
|
99
99
|
- app/models/effective/email_template.rb
|
100
|
-
- app/views/admin/email_templates/_actions.html.haml
|
101
100
|
- app/views/admin/email_templates/_form.html.haml
|
102
|
-
- app/views/admin/email_templates/edit.html.haml
|
103
|
-
- app/views/admin/email_templates/index.html.haml
|
104
101
|
- app/views/effective/email_reviews/_fields.html.haml
|
105
102
|
- app/views/layouts/effective_email_templates_mailer_layout.html.haml
|
106
103
|
- config/effective_email_templates.rb
|
@@ -1,17 +0,0 @@
|
|
1
|
-
unless defined?(Effective::AccessDenied)
|
2
|
-
module Effective
|
3
|
-
class AccessDenied < StandardError
|
4
|
-
attr_reader :action, :subject
|
5
|
-
|
6
|
-
def initialize(message = nil, action = nil, subject = nil)
|
7
|
-
@message = message
|
8
|
-
@action = action
|
9
|
-
@subject = subject
|
10
|
-
end
|
11
|
-
|
12
|
-
def to_s
|
13
|
-
@message || I18n.t(:'unauthorized.default', :default => 'Access Denied')
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,5 +0,0 @@
|
|
1
|
-
= dropdown(variation: :dropleft) do
|
2
|
-
= dropdown_link_to 'Edit', effective_email_templates.edit_admin_email_template_path(email_template)
|
3
|
-
|
4
|
-
= dropdown_link_to "Delete #{email_template}", effective_email_templates.admin_email_template_path(email_template),
|
5
|
-
data: { method: :delete, confirm: "Really delete #{email_template}?" }
|