mailtime 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +5 -2
- data/Rakefile +3 -1
- data/lib/mailtime/action_mailer/base.rb +4 -11
- data/lib/mailtime/active_record/models/mail_template.rb +24 -0
- data/lib/mailtime/configuration.rb +1 -0
- data/lib/mailtime/processor/mail_log_service.rb +1 -0
- data/lib/mailtime/processor/processor.rb +40 -5
- data/lib/mailtime/railtie.rb +1 -21
- data/lib/mailtime/renderers/base_renderer.rb +1 -0
- data/lib/mailtime/renderers/mail_renderer.rb +13 -22
- data/lib/mailtime/renderers/null.rb +2 -48
- data/lib/mailtime/renderers/null_layout.rb +51 -0
- data/lib/mailtime/renderers/null_template.rb +64 -0
- data/lib/mailtime/version.rb +1 -1
- data/lib/tasks/mailtime.rake +22 -0
- data/mailtime.gemspec +3 -1
- metadata +37 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a026e26ef8e6cf4a4bfea11a0fd01c016783e258
|
4
|
+
data.tar.gz: 4d12f9c970dad51e543342106807fd97f4ef9cbc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fbe09d0c3771f07f0a2dece8954c65aaf669fb74ddf711353f00de79e30f7d53db1d1100db521484e13ddab6cc9e078008d4905ebf519484ae79870b5e0c8d6a
|
7
|
+
data.tar.gz: c842cad376e1e0972b918d64511b36c38f5bca17f4e9726a236e1f26ad141f13a2203873b64469a566771f34ba3103b2764a55fa566936ebced827229a4ccc26
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -43,7 +43,7 @@ Need to customize? Sure.
|
|
43
43
|
|
44
44
|
```Ruby
|
45
45
|
class User < ActiveRecord::Base
|
46
|
-
acts_as_mailtime_loggable :email_address, :fields => [:to, :cc, :bcc]
|
46
|
+
acts_as_mailtime_loggable :email_address, :fields => [:to, :cc, :bcc]
|
47
47
|
```
|
48
48
|
|
49
49
|
will tell `Mailtime::MailLog` to find an instance variable of class `User` where attribute `email_address` matches an `ActionMailer#mail`'s `#to`, `#cc`, or `#bcc` attribute.
|
@@ -109,12 +109,15 @@ Has a `name`, `content`, `format`, and `extension`. `format` should be one of `M
|
|
109
109
|
|
110
110
|
## Todo
|
111
111
|
|
112
|
-
* Tests
|
112
|
+
* Tests. Like, way better tests.
|
113
|
+
* Attachments
|
114
|
+
* Support MySQL (uses jsonb as datatypes for storing things right now)
|
113
115
|
|
114
116
|
## Maybe todo
|
115
117
|
|
116
118
|
* Dump for production?
|
117
119
|
* Versioning for templates?
|
120
|
+
* A hell of a lot better rake import task.
|
118
121
|
|
119
122
|
## Contributing
|
120
123
|
|
data/Rakefile
CHANGED
@@ -39,19 +39,12 @@ module Mailtime
|
|
39
39
|
self.message.instance_variable_set(:@template_name, self.headers[:template_name] || mailer_action)
|
40
40
|
self.message.class.send(:attr_reader, :template_name)
|
41
41
|
|
42
|
-
self.message.instance_variable_set(:@
|
43
|
-
self.message.class.send(:attr_accessor, :
|
44
|
-
|
45
|
-
self.message.instance_variable_set(:@mailtime_layout, find_mailtime_template.mailtime_mail_layout)
|
46
|
-
self.message.class.send(:attr_accessor, :mailtime_layout)
|
42
|
+
self.message.instance_variable_set(:@mailtime_templates, find_mailtime_templates)
|
43
|
+
self.message.class.send(:attr_accessor, :mailtime_templates)
|
47
44
|
end
|
48
45
|
|
49
|
-
def
|
50
|
-
|
51
|
-
if _template.nil?
|
52
|
-
_template = Mailtime::NullTemplate.new(self)
|
53
|
-
end
|
54
|
-
_template
|
46
|
+
def find_mailtime_templates
|
47
|
+
Mailtime::MailTemplateCollection.new(self)
|
55
48
|
end
|
56
49
|
|
57
50
|
end
|
@@ -8,4 +8,28 @@ module Mailtime
|
|
8
8
|
include Mailtime::RendererConcern
|
9
9
|
|
10
10
|
end
|
11
|
+
|
12
|
+
class MailTemplateCollection
|
13
|
+
|
14
|
+
attr_reader :templates
|
15
|
+
def initialize(mail)
|
16
|
+
@mail = mail
|
17
|
+
@templates = Mailtime::MailTemplate.includes(:mail_layout).
|
18
|
+
where(:klass => mail.class.to_s, :action => mail.action_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
def html
|
22
|
+
_template = @templates.where(:format => 'html').first
|
23
|
+
return _template if _template
|
24
|
+
Mailtime.configuration.fallback ? nil : NullHTMLTemplate.new(@mail)
|
25
|
+
end
|
26
|
+
|
27
|
+
def text
|
28
|
+
_template = @templates.where(:format => 'text').first
|
29
|
+
return _template if _template
|
30
|
+
Mailtime.configuration.fallback ? nil : NullTextTemplate.new(@mail)
|
31
|
+
end
|
32
|
+
alias_method :txt, :text
|
33
|
+
|
34
|
+
end
|
11
35
|
end
|
@@ -3,11 +3,15 @@ require 'mailtime/renderers/mail_renderer'
|
|
3
3
|
require 'mailtime/renderers/base_renderer'
|
4
4
|
require 'mailtime/renderers/erb_renderer'
|
5
5
|
require 'mailtime/processor/mail_log_service'
|
6
|
-
require 'mailtime/processor/processor'
|
7
6
|
|
8
7
|
module Mailtime
|
9
8
|
class Processor
|
10
9
|
|
10
|
+
CONTENT_TYPES = {
|
11
|
+
:html => 'text/html',
|
12
|
+
:text => 'text/plain'
|
13
|
+
}
|
14
|
+
|
11
15
|
def initialize(mail)
|
12
16
|
@mail = mail
|
13
17
|
end
|
@@ -19,6 +23,7 @@ module Mailtime
|
|
19
23
|
protected
|
20
24
|
|
21
25
|
def process_mail
|
26
|
+
remove_parts
|
22
27
|
render_mail
|
23
28
|
log_mail
|
24
29
|
@mail
|
@@ -26,18 +31,27 @@ module Mailtime
|
|
26
31
|
|
27
32
|
private
|
28
33
|
|
29
|
-
def
|
30
|
-
|
31
|
-
|
34
|
+
def remove_part(format)
|
35
|
+
index = @mail.parts.index { |mail_part| mail_part.content_type.index(Mailtime::Processor::CONTENT_TYPES[format])}
|
36
|
+
@mail.parts.delete_at(index)
|
37
|
+
end
|
38
|
+
|
39
|
+
def remove_parts
|
40
|
+
|
32
41
|
end
|
33
42
|
|
34
43
|
def render_mail
|
35
44
|
return @mail unless Mailtime.configuration.render
|
36
45
|
renderer = Mailtime::Renderers::MailRenderer.new(@mail)
|
37
|
-
|
46
|
+
apply_parts(renderer)
|
38
47
|
@mail
|
39
48
|
end
|
40
49
|
|
50
|
+
def log_mail
|
51
|
+
return unless Mailtime.configuration.log
|
52
|
+
log_for_recipients
|
53
|
+
end
|
54
|
+
|
41
55
|
def log_for_recipients
|
42
56
|
Mailtime.configuration.loggables.each do |loggable, options|
|
43
57
|
svc = MailLogService.new(@mail, loggable, options)
|
@@ -45,5 +59,26 @@ module Mailtime
|
|
45
59
|
end
|
46
60
|
end
|
47
61
|
|
62
|
+
def apply_parts(renderer)
|
63
|
+
[:text, :html].each do |format|
|
64
|
+
apply_part(renderer, format)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def apply_part(renderer, format)
|
69
|
+
binding.pry
|
70
|
+
remove_part(format) if mail_has_template?(format)
|
71
|
+
part = Mail::Part.new do
|
72
|
+
content_type "#{CONTENT_TYPES[format]}; charset=UTF-8"
|
73
|
+
body renderer.execute(format)
|
74
|
+
end
|
75
|
+
mthd = "#{format}_part=".to_sym
|
76
|
+
@mail.send(mthd, part)
|
77
|
+
end
|
78
|
+
|
79
|
+
def mail_has_template?(format)
|
80
|
+
!!!@mail.mailtime_templates.send(format).nil?
|
81
|
+
end
|
82
|
+
|
48
83
|
end
|
49
84
|
end
|
data/lib/mailtime/railtie.rb
CHANGED
@@ -9,26 +9,6 @@ class Railtie < Rails::Railtie
|
|
9
9
|
end
|
10
10
|
|
11
11
|
rake_tasks do
|
12
|
-
|
13
|
-
desc 'Create a bunch of MailTemplate(s) based on existing mailers'
|
14
|
-
task :sync_mailers => :environment do
|
15
|
-
Dir.glob(::Rails.root.join("app/mailers/**/*_mailer.rb")).each { |m| require m }
|
16
|
-
mailers = ObjectSpace.each_object(Class).select { |klass| klass < ::ActionMailer::Base }
|
17
|
-
mailers.each do |mailer|
|
18
|
-
methods = mailer.public_instance_methods(false)
|
19
|
-
methods.each do |m|
|
20
|
-
formats = Dir.glob(Rails.root.join("app/views/#{mailer.to_s.underscore}/#{m}.*"))
|
21
|
-
formats.each do |f|
|
22
|
-
Mailtime::MailTemplate.create(:klass => mailer.to_s,
|
23
|
-
:action => m,
|
24
|
-
:content => File.read(f),
|
25
|
-
:format => f.scan(%r{\.(\w{3,4})\.}).to_a.first.first,
|
26
|
-
:extension => File.extname(f)[1 .. -1]
|
27
|
-
)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
12
|
+
|
33
13
|
end
|
34
14
|
end
|
@@ -4,36 +4,27 @@ module Mailtime
|
|
4
4
|
|
5
5
|
def initialize(mail)
|
6
6
|
@mail = mail
|
7
|
-
@template = @mail.mailtime_template
|
8
|
-
@layout = @mail.mailtime_layout
|
9
7
|
end
|
10
8
|
|
11
|
-
def
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.renders
|
16
|
-
[]
|
9
|
+
def execute(format)
|
10
|
+
render(format)
|
17
11
|
end
|
18
12
|
|
19
13
|
protected
|
20
14
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
15
|
+
def render(format)
|
16
|
+
template = @mail.mailtime_templates.send(format)
|
17
|
+
layout = template.try(:mail_layout)
|
18
|
+
if template.nil?
|
19
|
+
part = @mail.parts.detect { |part| part.content_type.index(Mailtime::Processor::CONTENT_TYPES[format]) }
|
20
|
+
if part.nil?
|
21
|
+
raise TemplateNotFound, "Existing template not found for format #{format}"
|
22
|
+
end
|
23
|
+
return part.body.raw_source
|
24
|
+
else
|
25
|
+
return layout.render(@mail).gsub(Mailtime.configuration.yield_keyword, template.render(@mail))
|
27
26
|
end
|
28
|
-
_template
|
29
|
-
end
|
30
|
-
|
31
|
-
def find_layout
|
32
|
-
@template.mailtime_mail_layout
|
33
|
-
end
|
34
27
|
|
35
|
-
def render_and_merge
|
36
|
-
@layout.render(@mail).gsub(Mailtime.configuration.yield_keyword, @template.render(@mail))
|
37
28
|
end
|
38
29
|
|
39
30
|
end
|
@@ -1,48 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
module Mailtime
|
4
|
-
class NullLayout
|
5
|
-
|
6
|
-
attr_reader :content, :id
|
7
|
-
def initialize
|
8
|
-
@id = nil
|
9
|
-
@content = "#{Mailtime.configuration.yield_keyword}"
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.renders
|
13
|
-
['erb']
|
14
|
-
end
|
15
|
-
|
16
|
-
def renderer
|
17
|
-
Mailtime::Renderers::ErbRenderer
|
18
|
-
end
|
19
|
-
|
20
|
-
def render(mail)
|
21
|
-
renderer.new(mail, @content).render
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
class NullTemplate
|
27
|
-
|
28
|
-
attr_reader :content, :id
|
29
|
-
def initialize(mail)
|
30
|
-
@id = nil
|
31
|
-
@content = mail.body.raw_source
|
32
|
-
end
|
33
|
-
|
34
|
-
def mail_layout
|
35
|
-
NullLayout.new
|
36
|
-
end
|
37
|
-
|
38
|
-
def renderer
|
39
|
-
Mailtime::Renderers::ErbRenderer
|
40
|
-
end
|
41
|
-
|
42
|
-
# doesn't do anything since we already know the mail.
|
43
|
-
def render(mail)
|
44
|
-
@content
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
end
|
1
|
+
require 'mailtime/renderers/null_layout'
|
2
|
+
require 'mailtime/renderers/null_template'
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Mailtime
|
2
|
+
class NullLayout
|
3
|
+
|
4
|
+
attr_reader :content, :id, :format
|
5
|
+
def initialize
|
6
|
+
@id = nil
|
7
|
+
@content = "#{Mailtime.configuration.yield_keyword}"
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.renders
|
11
|
+
['erb']
|
12
|
+
end
|
13
|
+
|
14
|
+
def renderer
|
15
|
+
Mailtime::Renderers::ErbRenderer
|
16
|
+
end
|
17
|
+
|
18
|
+
def render(mail)
|
19
|
+
renderer.new(mail, @content).render
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
class NullHTMLLayout < NullLayout
|
25
|
+
|
26
|
+
def format
|
27
|
+
:html
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
class NullTextLayout < NullLayout
|
33
|
+
|
34
|
+
def format
|
35
|
+
:text
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
class NullLayoutCollection
|
41
|
+
|
42
|
+
def html
|
43
|
+
NullHTMLLayout.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def text
|
47
|
+
NullTextLayout.new
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Mailtime
|
2
|
+
class NullTemplate
|
3
|
+
|
4
|
+
attr_reader :content, :id
|
5
|
+
def initialize(mail)
|
6
|
+
@id = nil
|
7
|
+
@content = mail.body.raw_source
|
8
|
+
end
|
9
|
+
|
10
|
+
def mail_layout
|
11
|
+
NullLayout.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def renderer
|
15
|
+
Mailtime::Renderers::ErbRenderer
|
16
|
+
end
|
17
|
+
|
18
|
+
# doesn't do anything since we already know the mail.
|
19
|
+
def render(mail)
|
20
|
+
@content
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class NullHTMLTemplate < NullTemplate
|
26
|
+
|
27
|
+
def format
|
28
|
+
:html
|
29
|
+
end
|
30
|
+
|
31
|
+
def mail_layout
|
32
|
+
NullHTMLLayout.new
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
class NullTextTemplate < NullTemplate
|
38
|
+
|
39
|
+
def format
|
40
|
+
:text
|
41
|
+
end
|
42
|
+
|
43
|
+
def mail_layout
|
44
|
+
NullTextLayout.new
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
class NullTemplateCollection
|
50
|
+
|
51
|
+
def initialize(mail)
|
52
|
+
@mail = mail
|
53
|
+
end
|
54
|
+
|
55
|
+
def html
|
56
|
+
NullHTMLTemplate.new(@mail)
|
57
|
+
end
|
58
|
+
|
59
|
+
def text
|
60
|
+
NullTextTemplate.new(@mail)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
data/lib/mailtime/version.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
# for the love of god, don't use this.
|
2
|
+
namespace :mailtime do
|
3
|
+
desc 'Create a bunch of MailTemplate(s) based on existing mailers'
|
4
|
+
task :sync_mailers => :environment do
|
5
|
+
Dir.glob(::Rails.root.join("app/mailers/**/*_mailer.rb")).each { |m| require m }
|
6
|
+
mailers = ObjectSpace.each_object(Class).select { |klass| klass < ::ActionMailer::Base }
|
7
|
+
mailers.each do |mailer|
|
8
|
+
methods = mailer.public_instance_methods(false)
|
9
|
+
methods.each do |m|
|
10
|
+
formats = Dir.glob(Rails.root.join("app/views/#{mailer.to_s.underscore}/#{m}.*"))
|
11
|
+
formats.each do |f|
|
12
|
+
Mailtime::MailTemplate.create(:klass => mailer.to_s,
|
13
|
+
:action => m,
|
14
|
+
:content => File.read(f),
|
15
|
+
:format => f.scan(%r{\.(\w{3,4})\.}).to_a.first.first,
|
16
|
+
:extension => File.extname(f)[1 .. -1]
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/mailtime.gemspec
CHANGED
@@ -29,8 +29,10 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
30
|
spec.require_paths = ["lib"]
|
31
31
|
|
32
|
-
spec.add_development_dependency "rails", "4.
|
32
|
+
spec.add_development_dependency "rails", ">= 4.1"
|
33
33
|
spec.add_development_dependency "rake", ">= 10.0"
|
34
|
+
spec.add_development_dependency "pg"
|
35
|
+
spec.add_development_dependency "pry"
|
34
36
|
spec.add_development_dependency "minitest"
|
35
37
|
spec.add_development_dependency "minitest-reporters"
|
36
38
|
spec.add_development_dependency "mocha"
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mailtime
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Brody
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '4.
|
19
|
+
version: '4.1'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '4.
|
26
|
+
version: '4.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +38,34 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pg
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
41
69
|
- !ruby/object:Gem::Dependency
|
42
70
|
name: minitest
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -133,7 +161,10 @@ files:
|
|
133
161
|
- lib/mailtime/renderers/erb_renderer.rb
|
134
162
|
- lib/mailtime/renderers/mail_renderer.rb
|
135
163
|
- lib/mailtime/renderers/null.rb
|
164
|
+
- lib/mailtime/renderers/null_layout.rb
|
165
|
+
- lib/mailtime/renderers/null_template.rb
|
136
166
|
- lib/mailtime/version.rb
|
167
|
+
- lib/tasks/mailtime.rake
|
137
168
|
- mailtime.gemspec
|
138
169
|
homepage: https://github.com/joshmn/mailtime
|
139
170
|
licenses:
|