effective_email_templates 0.6.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +22 -36
  4. data/app/controllers/admin/email_templates_controller.rb +25 -16
  5. data/app/datatables/effective_email_templates_datatable.rb +10 -5
  6. data/app/mailers/effective/email_templates_mailer.rb +22 -0
  7. data/app/models/effective/email_template.rb +66 -34
  8. data/app/views/admin/email_templates/_actions.html.haml +5 -2
  9. data/app/views/admin/email_templates/_form.html.haml +16 -9
  10. data/app/views/admin/email_templates/index.html.haml +1 -1
  11. data/app/views/layouts/effective_email_templates_mailer_layout.html.haml +7 -0
  12. data/config/effective_email_templates.rb +16 -25
  13. data/config/routes.rb +6 -8
  14. data/db/migrate/01_create_effective_email_templates.rb.erb +14 -8
  15. data/lib/effective_email_templates/engine.rb +1 -1
  16. data/lib/effective_email_templates/importer.rb +55 -0
  17. data/lib/effective_email_templates/version.rb +1 -1
  18. data/lib/effective_email_templates.rb +18 -14
  19. data/lib/generators/effective_email_templates/install_generator.rb +1 -1
  20. data/lib/tasks/effective_email_templates_tasks.rake +19 -0
  21. metadata +35 -146
  22. data/Rakefile +0 -23
  23. data/app/assets/javascripts/effective_email_templates.js +0 -1
  24. data/app/assets/stylesheets/effective_email_templates.css.scss +0 -1
  25. data/app/helpers/effective_email_templates_helper.rb +0 -19
  26. data/app/mailers/effective/email_template_mailer.rb +0 -14
  27. data/app/views/effective/email_template_mailer/templated_email.html.haml +0 -1
  28. data/lib/effective/liquid_mailer.rb +0 -15
  29. data/lib/effective_email_templates/email_view_template.rb +0 -42
  30. data/lib/effective_email_templates/liquid_resolver.rb +0 -46
  31. data/lib/effective_email_templates/template_importer.rb +0 -51
  32. data/lib/tasks/effective_email_templates/import_default_views.rake +0 -19
  33. data/spec/controllers/admin/email_templates_controller_spec.rb +0 -60
  34. data/spec/dummy/README.rdoc +0 -28
  35. data/spec/dummy/Rakefile +0 -6
  36. data/spec/dummy/app/assets/javascripts/application.js +0 -13
  37. data/spec/dummy/app/assets/stylesheets/application.css +0 -15
  38. data/spec/dummy/app/controllers/application_controller.rb +0 -5
  39. data/spec/dummy/app/controllers/welcome_controller.rb +0 -4
  40. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  41. data/spec/dummy/app/mailers/liquid_resolved_mailer.rb +0 -9
  42. data/spec/dummy/app/mailers/user_liquid_mailer.rb +0 -10
  43. data/spec/dummy/app/models/user.rb +0 -15
  44. data/spec/dummy/app/views/layouts/application.html.erb +0 -17
  45. data/spec/dummy/app/views/user_liquid/after_create_user.liquid +0 -7
  46. data/spec/dummy/app/views/welcome/index.html.haml +0 -1
  47. data/spec/dummy/bin/bundle +0 -3
  48. data/spec/dummy/bin/rails +0 -4
  49. data/spec/dummy/bin/rake +0 -4
  50. data/spec/dummy/config/application.rb +0 -26
  51. data/spec/dummy/config/boot.rb +0 -5
  52. data/spec/dummy/config/database.yml +0 -25
  53. data/spec/dummy/config/environment.rb +0 -5
  54. data/spec/dummy/config/environments/development.rb +0 -37
  55. data/spec/dummy/config/environments/production.rb +0 -78
  56. data/spec/dummy/config/environments/test.rb +0 -40
  57. data/spec/dummy/config/initializers/assets.rb +0 -8
  58. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  59. data/spec/dummy/config/initializers/cookies_serializer.rb +0 -3
  60. data/spec/dummy/config/initializers/devise.rb +0 -259
  61. data/spec/dummy/config/initializers/effective_email_templates.rb +0 -51
  62. data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  63. data/spec/dummy/config/initializers/inflections.rb +0 -16
  64. data/spec/dummy/config/initializers/mime_types.rb +0 -4
  65. data/spec/dummy/config/initializers/session_store.rb +0 -3
  66. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  67. data/spec/dummy/config/locales/devise.en.yml +0 -60
  68. data/spec/dummy/config/locales/en.yml +0 -23
  69. data/spec/dummy/config/routes.rb +0 -57
  70. data/spec/dummy/config/secrets.yml +0 -22
  71. data/spec/dummy/config.ru +0 -4
  72. data/spec/dummy/db/migrate/20141126222940_devise_create_users.rb +0 -42
  73. data/spec/dummy/db/migrate/20141126222941_create_effective_email_templates.rb +0 -20
  74. data/spec/dummy/db/schema.rb +0 -46
  75. data/spec/dummy/public/404.html +0 -67
  76. data/spec/dummy/public/422.html +0 -67
  77. data/spec/dummy/public/500.html +0 -66
  78. data/spec/dummy/public/favicon.ico +0 -0
  79. data/spec/effective_email_templates_spec.rb +0 -35
  80. data/spec/factories/email_template.rb +0 -12
  81. data/spec/factories/user.rb +0 -16
  82. data/spec/factory_spec.rb +0 -10
  83. data/spec/lib/effective_email_templates/template_importer_spec.rb +0 -73
  84. data/spec/mailers/liquid_resolved_mailer_spec.rb +0 -38
  85. data/spec/models/email_template_spec.rb +0 -61
  86. data/spec/models/user_spec.rb +0 -10
  87. data/spec/sanity_spec.rb +0 -7
  88. data/spec/spec_helper.rb +0 -26
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c60f871f30ff1ce2c6f2adbccc31e906b8d19fba696bbd456f1e8f9b17064215
4
- data.tar.gz: d41c1d401f35199b7ec8b80046cade8a364611cd1771bf1e62976fc2a4c27ac7
3
+ metadata.gz: a7ceefc31750874cc34b906a00ce64e80ee805bdf247cb69ec33d080e3625e28
4
+ data.tar.gz: ba54f4fc199b22bb8aed8b01d60f7b71ddfec735b6be56fc37d1cee9853f9a3f
5
5
  SHA512:
6
- metadata.gz: 7466896b837db170f1305d889542b2faa441b39cbe7f511d1845f2a3cff8a0520469f542a13dd76baaf98fcfe85efb927d571e65f851bfa86fca07e5409d23cf
7
- data.tar.gz: b536c744a107741e773831552e9beb548cc064bb4fe7e07193deef4e16c3d897743ac7d79b4fc143ea78ebc41483b8f4adb5d9ee5d1943b1fb3ea0ff038bfb3b
6
+ metadata.gz: e24eb4a7e1fa925844d45de25fab8a7bb9b6b3f4fc631c13ba4418a43e9bb304b6be6e1e7eb16c5396d097d8cb5ac910040464af9e1a7bb2aba5122b40f92c61
7
+ data.tar.gz: fc6b6f48a42448af82136497ab39ec26a28c9ef60e1db3ef45b42dac8c7c0d7d0d093005e40f6b246e5e671a63a275e8f32ccce34520a6281fd4ff80c821768e
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2014 Code and Effect Inc.
1
+ Copyright 2020 Code and Effect Inc.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,20 +1,17 @@
1
1
 
2
2
  # Effective Email Templates
3
3
 
4
- **This gem is no longer actively maintained**
4
+ Create email templates that an admin can edit and then send.
5
5
 
6
- Create email templates that an admin or any user can safely edit (you're protected from SQL injection and other nastiness).
7
- Effective Email Templates relies on [Liquid templates](http://liquidmarkup.org/) built by Shopify to profide this safety.
6
+ Rails 3.2.x and higher
8
7
 
9
- Rails 3.2.x and Rails 4 Support
8
+ ## effective_email_templates 1.0
10
9
 
11
- ## Bootstrap3
10
+ This is the 1.0 series of effective_email_templates.
12
11
 
13
- This is the `bootstrap3` branch of effective_email_templates which supports Twitter Bootstrap 3.
12
+ This requires Twitter Bootstrap 4.
14
13
 
15
- All published effective_email_templates 0.x gems will support Twitter Bootstrap 3 and SimpleForm.
16
-
17
- For Bootstrap 4 please see the master branch and/or effective_email_templates 1.x gems.
14
+ Please check out [Effective Email Templates 0.x](https://github.com/code-and-effect/effective_email_templates/tree/bootstrap3) for more information using this gem with Bootstrap 3.
18
15
 
19
16
 
20
17
  ## Getting Started
@@ -22,7 +19,7 @@ For Bootstrap 4 please see the master branch and/or effective_email_templates 1.
22
19
  Add to your Gemfile:
23
20
 
24
21
  ```ruby
25
- gem 'effective_email_templates', :git => 'https://github.com/code-and-effect/effective_email_templates'
22
+ gem 'effective_email_templates'
26
23
  ```
27
24
 
28
25
  Run the bundle command to install it:
@@ -47,10 +44,11 @@ Then migrate the database:
47
44
  rake db:migrate
48
45
  ```
49
46
 
47
+ ## Create Email Templates
50
48
 
51
- ## Creating Email Templates
49
+ `link_to 'Email Templates', effective_email_templates.admin_email_templates_path`
52
50
 
53
- This is very similar to creating other emails in Rails.
51
+ To create your first post, visit `/admin/email_templates` and click `New Email Template`.
54
52
 
55
53
  1. Create a new mailer object (i.e. `/app/mailers/template_mailer.rb`)
56
54
  - Mailer objects need to inherit from `Effective::LiquidMailer`
@@ -119,15 +117,15 @@ mail.deliver
119
117
 
120
118
  ## Authorization
121
119
 
122
- All authorization checks are handled via the config.authorization_method found in the config/initializers/ file.
120
+ All authorization checks are handled via the config.authorization_method found in the `app/config/initializers/effective_email_templates.rb` file.
123
121
 
124
- It is intended for flow through to CanCan or Pundit, but that is not required.
122
+ It is intended for flow through to CanCan or Pundit, but neither of those gems are required.
125
123
 
126
124
  This method is called by all controller actions with the appropriate action and resource
127
125
 
128
126
  Action will be one of [:index, :show, :new, :create, :edit, :update, :destroy]
129
127
 
130
- Resource will the appropriate Effective::Something ActiveRecord object or class
128
+ Resource will the appropriate Effective::EmailTemplate object or class
131
129
 
132
130
  The authorization method is defined in the initializer file:
133
131
 
@@ -173,32 +171,20 @@ end
173
171
  To allow a user to see the admin area, using CanCan:
174
172
 
175
173
  ```ruby
174
+ can :manage, Effective::EmailTemplate
176
175
  can :admin, :effective_email_templates
177
176
  ```
178
177
 
179
-
180
178
  ## License
181
179
 
182
- MIT License. Copyright Code and Effect Inc. http://www.codeandeffect.com
183
-
184
- You are not granted rights or licenses to the trademarks of Code and Effect
185
-
186
- ## TODO
180
+ MIT License. Copyright [Code and Effect Inc.](http://www.codeandeffect.com/)
187
181
 
188
- - minimize gem dependencies
189
- - remove unused non-admin routes and config option
190
- - Add an admin alert if there are mailer methods that do not have associated email templates
191
- - Show admin what the available arguments are for each template
192
- - remove `caller` usage from Effective::LiquidMailer (or remove requirement for Ruby 2.0+)
193
- - add `EffectiveEmailTemplates::present?` to check if a template model is present
194
- - notify the app but don't raise an error if template is not present when an email is sent
195
- - configurable default from address
196
- - configurable prefix for subject line
197
- - use autoload rather than require
198
- - research panoramic gem's usage of the Resolver class and improve our Resolver usage
199
- - enable multipart emails
200
- - enable file attachments
201
- - import factories/fixtures in install task
202
182
 
203
- ## Testing
183
+ ## Contributing
204
184
 
185
+ 1. Fork it
186
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
187
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
188
+ 4. Push to the branch (`git push origin my-new-feature`)
189
+ 5. Bonus points for test coverage
190
+ 6. Create new Pull Request
@@ -1,16 +1,11 @@
1
1
  module Admin
2
2
  class EmailTemplatesController < ApplicationController
3
- before_action :authenticate_user! # This is devise, ensure we're logged in.
3
+ before_action :authenticate_user! if respond_to?(:authenticate_user!)
4
4
 
5
5
  layout (EffectiveEmailTemplates.layout.kind_of?(Hash) ? EffectiveEmailTemplates.layout[:admin_email_templates] : EffectiveEmailTemplates.layout)
6
6
 
7
7
  def index
8
- if Gem::Version.new(EffectiveDatatables::VERSION) < Gem::Version.new('3.0')
9
- @datatable = Effective::Datatables::EmailTemplates.new()
10
- else
11
- @datatable = EffectiveEmailTemplatesDatatable.new(self)
12
- end
13
-
8
+ @datatable = EffectiveEmailTemplatesDatatable.new
14
9
  @page_title = 'Email Templates'
15
10
 
16
11
  authorize_effective_email_templates!
@@ -30,10 +25,10 @@ module Admin
30
25
  authorize_effective_email_templates!
31
26
 
32
27
  if @email_template.save
33
- flash[:success] = "Email template created successfully"
28
+ flash[:success] = 'Successfully created email template'
34
29
  redirect_to effective_email_templates.admin_email_templates_path
35
30
  else
36
- flash.now[:error] = "Could not create email template"
31
+ flash.now[:danger] = 'Unable to create email template'
37
32
  render :new
38
33
  end
39
34
  end
@@ -52,23 +47,37 @@ module Admin
52
47
  authorize_effective_email_templates!
53
48
 
54
49
  if @email_template.update(email_template_params)
55
- flash[:success] = "Email template updated successfully"
50
+ flash[:success] = 'Successfully updated email template'
56
51
  redirect_to effective_email_templates.admin_email_templates_path
57
52
  else
58
- flash.now[:error] = "Could not update email template"
53
+ flash.now[:danger] = 'Unable to update email template'
59
54
  render :edit
60
55
  end
61
56
  end
62
57
 
63
- private
58
+ def destroy
59
+ @email_template = Effective::EmailTemplate.find(params[:id])
64
60
 
65
- def email_template_params
66
- params.require(:effective_email_template).permit([ :from, :cc, :bcc, :subject, :body ])
61
+ authorize_effective_email_templates!
62
+
63
+ if @email_template.destroy
64
+ flash[:success] = 'Successfully deleted email template'
65
+ else
66
+ flash[:danger] = 'Unable to delete email template'
67
+ end
68
+
69
+ redirect_to effective_email_templates.admin_email_templates_path
67
70
  end
68
71
 
72
+ private
73
+
69
74
  def authorize_effective_email_templates!
70
- EffectiveEmailTemplates.authorized?(self, :admin, :effective_email_templates)
71
- EffectiveEmailTemplates.authorized?(self, action_name.to_sym, @email_template || Effective::EmailTemplate)
75
+ EffectiveEmailTemplates.authorize!(self, :admin, :effective_email_templates)
76
+ EffectiveEmailTemplates.authorize!(self, action_name.to_sym, @email_template || Effective::EmailTemplate)
77
+ end
78
+
79
+ def email_template_params
80
+ params.require(:effective_email_template).permit(EffectiveEmailTemplates.permitted_params)
72
81
  end
73
82
 
74
83
  end
@@ -1,14 +1,18 @@
1
1
  class EffectiveEmailTemplatesDatatable < Effective::Datatable
2
2
  datatable do
3
3
  order :subject, :asc
4
+ length :all
4
5
 
6
+ col :updated_at, visible: false
7
+ col :created_at, visible: false
5
8
  col :id, visible: false
6
- col :slug
7
- col :subject
9
+
10
+ col :template_name, label: 'Name'
8
11
  col :from
9
- col :cc, visible: false, label: 'CC'
10
- col :bcc, visible: false, label: 'BCC'
11
- col :body, visible: false
12
+ col :cc
13
+ col :bcc
14
+ col :subject
15
+ col :body
12
16
 
13
17
  actions_col partial: '/admin/email_templates/actions', partial_as: 'email_template'
14
18
  end
@@ -17,3 +21,4 @@ class EffectiveEmailTemplatesDatatable < Effective::Datatable
17
21
  Effective::EmailTemplate.all
18
22
  end
19
23
  end
24
+
@@ -0,0 +1,22 @@
1
+ module Effective
2
+ class EmailTemplatesMailer < ::ActionMailer::Base
3
+
4
+ def mail(headers = {}, &block)
5
+ email_template = Effective::EmailTemplate.where(template_name: action_name).first!
6
+
7
+ assigns = route_url_assigns(email_template).merge(@assigns || {})
8
+ rendered = email_template.render(assigns)
9
+
10
+ super(rendered.merge(headers))
11
+ end
12
+
13
+ private
14
+
15
+ def route_url_assigns(email_template)
16
+ email_template.template_variables.select { |name| name.ends_with?('_url') }.inject({}) do |h, name|
17
+ h[name] = public_send(name) if respond_to?(name); h
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -1,55 +1,87 @@
1
- require 'liquid'
2
- Liquid::Template.error_mode = :strict # Raises a SyntaxError when invalid syntax is used
3
-
4
1
  module Effective
5
2
  class EmailTemplate < ActiveRecord::Base
6
-
7
3
  self.table_name = EffectiveEmailTemplates.email_templates_table_name.to_s
8
4
 
9
- serialize :template, Liquid::Template
5
+ CONTENT_TYPES = ['text/plain', 'text/html']
10
6
 
11
- validate :slug_needs_to_have_simple_format
12
- validates :slug, presence: true, uniqueness: true
13
- validates :body, presence: true
14
- validates :subject, presence: true
15
- validates :from, presence: true
16
- validates :template, presence: true
7
+ serialize :template_body, Liquid::Template
8
+ serialize :template_subject, Liquid::Template
9
+ serialize :template_variables, Array
17
10
 
18
- before_validation :try_precompile
11
+ # Attributes
12
+ # subject :string
13
+ # from :string
14
+ # cc :string
15
+ # bcc :string
16
+ # body :text
17
+ # content_type :string
18
+ #
19
+ # template_name :string
20
+ # template_body :text
21
+ # template_subject :text
22
+ # template_variables :text
23
+ #
24
+ # timestamps
19
25
 
20
- def precompile
26
+ before_validation do
27
+ self.template_name ||= subject.to_s.parameterize
28
+ self.content_type ||= CONTENT_TYPES.first
29
+ end
30
+
31
+ before_validation(if: -> { body.present? }) do
21
32
  begin
22
- self.template = Liquid::Template.parse( body )
23
- rescue Liquid::SyntaxError => error
24
- errors.add :template, error.message
33
+ self.template_body = Liquid::Template.parse(body)
34
+ rescue Liquid::SyntaxError => e
35
+ errors.add(:body, e.message)
25
36
  end
26
37
  end
27
38
 
28
- def mail_options
29
- {
30
- from: from,
31
- subject: subject,
32
- cc: cc,
33
- bcc: bcc
34
- }.delete_if { |_, v| v.blank? }
39
+ before_validation(if: -> { subject.present? }) do
40
+ begin
41
+ self.template_subject = Liquid::Template.parse(subject)
42
+ rescue Liquid::SyntaxError => e
43
+ errors.add(:subject, e.message)
44
+ end
35
45
  end
36
46
 
37
- def render( *args )
38
- template.render( *args )
47
+ validates :subject, presence: true
48
+ validates :from, presence: true
49
+ validates :body, presence: true
50
+ validates :content_type, presence: true, inclusion: { in: CONTENT_TYPES }
51
+
52
+ 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
39
58
  end
40
59
 
41
- private
60
+ def to_s
61
+ template_name.presence || 'New Email Template'
62
+ end
42
63
 
43
- def try_precompile
44
- precompile if body_changed?
64
+ def render(assigns = {})
65
+ {
66
+ from: from,
67
+ cc: cc.presence || false,
68
+ bcc: bcc.presence || false,
69
+ content_type: content_type,
70
+ subject: template_subject.render(assigns),
71
+ body: template_body.render(assigns)
72
+ }
45
73
  end
46
74
 
47
- def slug_needs_to_have_simple_format
48
- # convert slug to symbol
49
- # add error if symbol has non-simple format (i.e. `:"symbol with spaces"` vs `:symbol_with_underscores`)
50
- if /[@$"]/ =~ slug.to_sym.inspect || slug.match(/[A-Z]/)
51
- errors.add(:slug, 'must have a simple format with no spaces, capital letters, and most punctuation (good: "hello_world", bad: "hello, world")')
52
- end
75
+ private
76
+
77
+ def find_template_variables
78
+ [template_body.presence, template_subject.presence].compact.map do |template|
79
+ Liquid::ParseTreeVisitor.for(template.root).add_callback_for(Liquid::VariableLookup) do |node|
80
+ [node.name, *node.lookups].join('.')
81
+ end.visit
82
+ end.flatten.uniq.compact
53
83
  end
84
+
54
85
  end
86
+
55
87
  end
@@ -1,2 +1,5 @@
1
- %span{:style => 'white-space: nowrap;'}
2
- = link_to 'Edit', effective_email_templates.edit_admin_email_template_path(email_template)
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}?" }
@@ -1,9 +1,16 @@
1
- = simple_form_for email_template, url: email_template_form_url(email_template) do |f|
2
- = f.input :subject
3
- = f.input :slug, disabled: true
4
- = f.input :from
5
- = f.input :cc
6
- = f.input :bcc
7
- = f.input :body, input_html: { rows: 10 }
8
-
9
- = f.button :submit
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
+ - if f.object.persisted?
3
+ = f.static_field :template_name, label: 'Name'
4
+
5
+ = f.text_field :subject, hint: 'The subject of your email.'
6
+
7
+ = f.email_field :from, hint: 'Whom the email will be sent from.'
8
+
9
+ = f.text_field :cc
10
+ = f.text_field :bcc
11
+
12
+ = f.select :content_type, Effective::EmailTemplate::CONTENT_TYPES
13
+
14
+ = f.text_area :body, hint: 'The content of your email template'
15
+
16
+ = f.submit
@@ -1,3 +1,3 @@
1
1
  %h1.effective-admin-heading= @page_title
2
2
 
3
- = render_datatable(@datatable)
3
+ = render_datatable @datatable
@@ -0,0 +1,7 @@
1
+ !!!
2
+ %html{style: 'background: #fff;'}
3
+ %head
4
+ %meta{:content => 'text/html; charset=UTF-8', 'http-equiv' => 'Content-Type'}
5
+
6
+ %body{style: 'background: #fff;'}
7
+ = yield
@@ -1,31 +1,7 @@
1
1
  EffectiveEmailTemplates.setup do |config|
2
- # Database table name to store email_templates in. Default is :email_templates
2
+ # Configure Database Tables
3
3
  config.email_templates_table_name = :email_templates
4
4
 
5
- # SimpleForm Options
6
- # This Hash of options will be passed into any simple_form_for() calls
7
- config.simple_form_options = {}
8
-
9
- # config.simple_form_options = {
10
- # :html => {:class => 'form-horizontal'},
11
- # :wrapper => :horizontal_form,
12
- # :wrapper_mappings => {
13
- # :boolean => :horizontal_boolean,
14
- # :check_boxes => :horizontal_radio_and_checkboxes,
15
- # :radio_buttons => :horizontal_radio_and_checkboxes
16
- # }
17
- # }
18
-
19
- # Layout Settings
20
- # Configure the Layout per controller, or all at once
21
-
22
- # config.layout = 'application' # All EffectiveEmailTemplates controllers will use this layout
23
- config.layout = {
24
- :email_templates => 'application',
25
- :admin_email_templates => 'application'
26
- }
27
-
28
-
29
5
  # Authorization Method
30
6
  #
31
7
  # This method is called by all controller actions with the appropriate action and resource
@@ -47,5 +23,20 @@ EffectiveEmailTemplates.setup do |config|
47
23
  # config.authorization_method = false
48
24
  config.authorization_method = Proc.new { |controller, action, resource| authorize!(action, resource) } # CanCanCan
49
25
 
26
+ # Layout Settings
27
+ # Configure the Layout per controller, or all at once
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
+ }
34
+
35
+ # Mailer Settings
36
+ config.mailer = {
37
+ subject_prefix: '[example] ',
38
+ layout: 'effective_email_templates_mailer_layout',
39
+ deliver_method: nil # :deliver (rails < 4.2), :deliver_now (rails >= 4.2) or :deliver_later
40
+ }
50
41
 
51
42
  end
data/config/routes.rb CHANGED
@@ -1,11 +1,9 @@
1
- Rails.application.routes.draw do
2
- mount EffectiveEmailTemplates::Engine => '/', :as => 'effective_email_templates'
3
- end
4
-
5
1
  EffectiveEmailTemplates::Engine.routes.draw do
6
- if defined?(EffectiveDatatables)
7
- namespace :admin do
8
- resources :email_templates, only: [:index, :edit, :update]
9
- end
2
+ namespace :admin do
3
+ resources :email_templates
10
4
  end
11
5
  end
6
+
7
+ Rails.application.routes.draw do
8
+ mount EffectiveEmailTemplates::Engine => '/', as: 'effective_email_templates'
9
+ end
@@ -1,16 +1,22 @@
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 :subject
5
- t.string :from
6
- t.string :bcc
7
- t.string :cc
8
- t.string :slug, :null => false
9
- t.text :body, :null => false
10
- t.text :template, :null => false
4
+ t.string :subject
5
+ t.string :from
6
+ t.string :bcc
7
+ t.string :cc
8
+
9
+ t.string :content_type
10
+ t.text :body
11
+
12
+ t.string :template_name
13
+ t.text :template_body
14
+ t.text :template_subject
15
+ t.text :template_variables
16
+
17
+ t.timestamps
11
18
  end
12
19
 
13
- add_index <%= @email_templates_table_name %>, :slug
14
20
  end
15
21
 
16
22
  def self.down
@@ -3,7 +3,7 @@ module EffectiveEmailTemplates
3
3
  engine_name 'effective_email_templates'
4
4
 
5
5
  # Set up our default configuration options.
6
- initializer "effective_email_templates.defaults", :before => :load_config_initializers do |app|
6
+ initializer 'effective_email_templates.defaults', before: :load_config_initializers do |app|
7
7
  eval File.read("#{config.root}/config/effective_email_templates.rb")
8
8
  end
9
9
 
@@ -0,0 +1,55 @@
1
+ module EffectiveEmailTemplates
2
+ class Importer
3
+ def self.import
4
+ new().execute(overwrite: false)
5
+ end
6
+
7
+ def self.overwrite
8
+ new().execute(overwrite: true)
9
+ end
10
+
11
+ def execute(overwrite:)
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)
15
+
16
+ if email_template.persisted? && !overwrite
17
+ puts "SKIPPED #{filename(filepath)}"; next
18
+ end
19
+
20
+ save(email_template, filepath)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def save(email_template, filepath)
27
+ file = File.new(filepath, 'r')
28
+
29
+ attributes = begin
30
+ content = YAML.load(file)
31
+ content.kind_of?(Hash) ? content : {}
32
+ end
33
+
34
+ body = File.open(file) do |f|
35
+ content = f.read
36
+ match = content.match(/(---+(.|\n)+---+)/)
37
+ content.gsub(match[1], '').strip if match
38
+ end
39
+
40
+ email_template.assign_attributes(attributes)
41
+ email_template.assign_attributes(body: body)
42
+
43
+ if email_template.save
44
+ puts "SUCCESS #{filename(filepath)}"
45
+ else
46
+ puts "ERROR #{filename(filepath)}: #{email_template.errors.full_messages.to_sentence}"
47
+ end
48
+ end
49
+
50
+ def filename(filepath)
51
+ filepath.sub(Rails.root.to_s + '/app/', '')
52
+ end
53
+
54
+ end
55
+ end
@@ -1,3 +1,3 @@
1
1
  module EffectiveEmailTemplates
2
- VERSION = '0.6.1'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  end
@@ -1,33 +1,37 @@
1
- require "effective_email_templates/liquid_resolver"
2
- require "effective_email_templates/email_view_template"
3
- require "effective_email_templates/template_importer"
4
- require "effective_email_templates/version"
1
+ require "liquid"
5
2
  require "effective_email_templates/engine"
6
-
7
- require "effective/liquid_mailer"
8
- Effective::LiquidMailer.send(:append_view_path, EffectiveEmailTemplates::LiquidResolver.new)
3
+ require "effective_email_templates/version"
9
4
 
10
5
  module EffectiveEmailTemplates
11
6
 
12
7
  mattr_accessor :email_templates_table_name
13
8
  mattr_accessor :authorization_method
14
- mattr_accessor :simple_form_options
15
9
  mattr_accessor :layout
10
+ mattr_accessor :mailer
16
11
 
17
12
  def self.setup
18
13
  yield self
19
14
  end
20
15
 
21
16
  def self.authorized?(controller, action, resource)
22
- if authorization_method.respond_to?(:call) || authorization_method.kind_of?(Symbol)
23
- raise Effective::AccessDenied.new() unless (controller || self).instance_exec(controller, action, resource, &authorization_method)
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
24
26
  end
25
- true
26
27
  end
27
28
 
28
- def self.get(slug)
29
- Effective::EmailTemplate.find_by_slug(slug) || Effective::EmailTemplate.new(slug: slug)
29
+ def self.authorize!(controller, action, resource)
30
+ raise Effective::AccessDenied.new('Access Denied', action, resource) unless authorized?(controller, action, resource)
30
31
  end
31
32
 
32
- end
33
+ def self.permitted_params
34
+ [:from, :bcc, :cc, :subject, :body, :content_type]
35
+ end
33
36
 
37
+ end