mailer_callbacks 2.0.0a
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +39 -0
- data/Rakefile +23 -0
- data/lib/mailer_callbacks/mailer_callbacks.rb +40 -0
- data/lib/mailer_callbacks/version.rb +3 -0
- data/lib/mailer_callbacks.rb +8 -0
- data/mailer_callbacks.gemspec +21 -0
- data/test/mailer_callbacks_test.rb +69 -0
- data/test/test_helper.rb +25 -0
- metadata +72 -0
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/kelyar/mailer_callbacks.png?branch=2.0)](https://travis-ci.org/kelyar/mailer_callbacks)
|
2
|
+
|
3
|
+
Mailer Callbacks
|
4
|
+
================
|
5
|
+
|
6
|
+
|
7
|
+
I had a task to save all emails we send to user - not content, of course, but at least email and subject (or template). It's pretty useful if you send lots of emails and want to know which ones this particular user received.
|
8
|
+
|
9
|
+
Easy way was to put some SentMailLog.create() in the end of every method of ActionMailer::Base ancestors. Doesn't look really DRY, but it will work. I looked thru ActionMailer API but there were no hooks like AR or even ActionController has. That's why I wrote this tiny 1Kb extension to AM that adds `after_deliver` callback for my Notifier class.
|
10
|
+
|
11
|
+
Right after that I plugged in open_id_authentication which is great but many more issues came out. It appears that my users no longer have emails because they could register with openid and some providers just ignore `required => {:email}`. I had to add `unless User.current_user.openid?` to all of my deliver_* methods. That's when `before_deliver` saved me. If any of your actions specified in `before_deliver` method returns false, no email will be sent. But no one cares what your `after_deliver` returns. It's pretty similar to ActiveRecord's before_save/after_save callbacks.
|
12
|
+
|
13
|
+
Many use cases can be found: you can mark your users as bounced if there are problems with their email accounts - no emails should be sent to them. Or you can have `unsubscribe all our news` flag like we have. This can also be verified in `before_deliver`. If you need to access email data just use instance variables inside your callbacks, like `@subject, @recipients, @body`,etc.
|
14
|
+
|
15
|
+
ok, no more words, here is the code:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class Notifier < ActionMailer::Base
|
19
|
+
before_deliver :openid_stub?
|
20
|
+
after_deliver :save_email
|
21
|
+
|
22
|
+
def openid_stub?
|
23
|
+
@recipients.to_s.include?('openid_stub')
|
24
|
+
end
|
25
|
+
|
26
|
+
def save_email
|
27
|
+
...
|
28
|
+
SentMailLog.create(email: @recipients.join(','), subj: @subject)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
TODO:
|
34
|
+
- <del>run on new action_mailer</del> DONE
|
35
|
+
- add usual [:except, :only] params
|
36
|
+
- yield if block_given?
|
37
|
+
- <del>add some tests</del> DONE
|
38
|
+
|
39
|
+
Copyright (c) 2008-2013 Evgeniy Kelyarsky, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rdoc/task'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the mailer_callbacks plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the mailer_callbacks plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'MailerCallbacks'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module MailerCallbacks
|
2
|
+
def self.included(base)
|
3
|
+
base.class_eval do
|
4
|
+
cattr_accessor :before_filters, :before_options, :after_filters
|
5
|
+
|
6
|
+
def self.before_deliver(*args)
|
7
|
+
self.before_options = args.last.is_a?(Hash) ? args.pop : {}
|
8
|
+
self.before_filters = args
|
9
|
+
|
10
|
+
register_interceptor(self)
|
11
|
+
end
|
12
|
+
|
13
|
+
# before
|
14
|
+
def self.delivering_email(message)
|
15
|
+
result, skip_before = true, false
|
16
|
+
|
17
|
+
if !skip_before
|
18
|
+
self.before_filters.each do |filter|
|
19
|
+
if __send__(filter.to_sym, message).is_a? ActionMailer::Base::NullMail
|
20
|
+
message.perform_deliveries = false
|
21
|
+
break
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# after
|
28
|
+
def self.delivered_email(message)
|
29
|
+
self.after_filters.each do |filter|
|
30
|
+
self.__send__(filter.to_sym, message)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.after_deliver(*args)
|
35
|
+
self.after_filters = args
|
36
|
+
register_observer(self)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "mailer_callbacks"
|
3
|
+
s.version = "2.0.0a"
|
4
|
+
s.platform = Gem::Platform::RUBY
|
5
|
+
s.summary = "Mailer callbacks"
|
6
|
+
|
7
|
+
s.description = <<-EOF
|
8
|
+
Small set of callbacks that can be useful if you do pre-post mail processing
|
9
|
+
EOF
|
10
|
+
|
11
|
+
s.files = Dir['{lib/**/*,test/**/*}'] +
|
12
|
+
%w(mailer_callbacks.gemspec Rakefile README.md)
|
13
|
+
s.require_path = 'lib'
|
14
|
+
s.test_files = Dir['test/*_test.rb']
|
15
|
+
|
16
|
+
s.author = 'Evgeniy Kelyarsky'
|
17
|
+
s.email = 'kelyar@gmail.com'
|
18
|
+
s.homepage = 'http://github.com/kelyar/mailer_callbacks'
|
19
|
+
|
20
|
+
s.add_development_dependency 'rake'
|
21
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class MailerCallbacksTest < ActionMailer::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
ActionMailer::Base.delivery_method = :test
|
7
|
+
ActionMailer::Base.perform_deliveries = true
|
8
|
+
ActionMailer::Base.deliveries = []
|
9
|
+
|
10
|
+
@recipient = 'test@localhost'
|
11
|
+
|
12
|
+
Notifier.class_eval do
|
13
|
+
def stop(m); end
|
14
|
+
def start(m); end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
test "before: deny" do
|
19
|
+
Notifier.class_eval do
|
20
|
+
def start(message); false end
|
21
|
+
end
|
22
|
+
|
23
|
+
assert_emails(0) do
|
24
|
+
Notifier.run.deliver
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
test "before: allow" do
|
29
|
+
Notifier.class_eval do
|
30
|
+
def start(message)
|
31
|
+
true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
assert_emails(1) do
|
36
|
+
Notifier.run.deliver
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
test "after delivery" do
|
41
|
+
Notifier.class_eval do
|
42
|
+
def stop(message) raise "111" end
|
43
|
+
end
|
44
|
+
assert_raise(RuntimeError) do
|
45
|
+
Notifier.run.deliver
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class Notifier < ActionMailer::Base
|
51
|
+
|
52
|
+
def run
|
53
|
+
to = "test@example.com"
|
54
|
+
from = "tester@example.com"
|
55
|
+
body = render(inline: 'test', body: 'zhenya', layout: false)
|
56
|
+
mail(:to => to, :body => body, :from => from)
|
57
|
+
end
|
58
|
+
|
59
|
+
def start(message)
|
60
|
+
end
|
61
|
+
|
62
|
+
def stop(message)
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
before_deliver :start, :except => [:index ]
|
67
|
+
after_deliver :stop
|
68
|
+
end
|
69
|
+
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'turn/autorun'
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_support/test_case'
|
6
|
+
require 'action_mailer'
|
7
|
+
|
8
|
+
require 'mailer_callbacks'
|
9
|
+
|
10
|
+
ActionMailer::Base.logger = Logger.new("mailer.log")
|
11
|
+
|
12
|
+
Turn.config do |c|
|
13
|
+
# use one of output formats:
|
14
|
+
# :outline - turn's original case/test outline mode [default]
|
15
|
+
# :progress - indicates progress with progress bar
|
16
|
+
# :dotted - test/unit's traditional dot-progress mode
|
17
|
+
# :pretty - new pretty reporter
|
18
|
+
# :marshal - dump output as YAML (normal run mode only)
|
19
|
+
# :cue - interactive testing
|
20
|
+
c.format = :outline
|
21
|
+
# turn on invoke/execute tracing, enable full backtrace
|
22
|
+
c.trace = 100
|
23
|
+
# use humanized test names (works only with :outline format)
|
24
|
+
c.natural = true
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mailer_callbacks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0a
|
5
|
+
prerelease: 5
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Evgeniy Kelyarsky
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: ! 'Small set of callbacks that can be useful if you do pre-post mail
|
31
|
+
processing
|
32
|
+
|
33
|
+
'
|
34
|
+
email: kelyar@gmail.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- lib/mailer_callbacks/mailer_callbacks.rb
|
40
|
+
- lib/mailer_callbacks/version.rb
|
41
|
+
- lib/mailer_callbacks.rb
|
42
|
+
- test/mailer_callbacks_test.rb
|
43
|
+
- test/test_helper.rb
|
44
|
+
- mailer_callbacks.gemspec
|
45
|
+
- Rakefile
|
46
|
+
- README.md
|
47
|
+
homepage: http://github.com/kelyar/mailer_callbacks
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>'
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 1.3.1
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.23
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Mailer callbacks
|
71
|
+
test_files:
|
72
|
+
- test/mailer_callbacks_test.rb
|