actionmailer-callbacks 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +81 -0
- data/Rakefile +13 -0
- data/actionmailer-callbacks.gemspec +26 -0
- data/lib/actionmailer-callbacks.rb +14 -0
- data/lib/actionmailer/callbacks/callback.rb +22 -0
- data/lib/actionmailer/callbacks/methods.rb +116 -0
- data/lib/actionmailer/callbacks/version.rb +5 -0
- data/spec/callback_spec.rb +53 -0
- data/spec/fixtures/mailer_with_callbacks.rb +49 -0
- data/spec/fixtures/mailer_without_callbacks.rb +8 -0
- data/spec/integration/action_mailer_integration_spec.rb +135 -0
- data/spec/methods_spec.rb +82 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/support/flag.rb +21 -0
- metadata +132 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# ActionMailer Callbacks
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/spaghetticode/actionmailer-callbacks.png)](http://travis-ci.org/spaghetticode/actionmailer-callbacks)
|
4
|
+
|
5
|
+
This gem adds the following methods to ActionMailer, similar to ActionController
|
6
|
+
before/after filters:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
before_create :log_params, :except => :test_email
|
10
|
+
after_create :alert_police, :only => :robbery_alert
|
11
|
+
before_deliver :apply_stamp, :except => [:postage_prepaid, :test_email]
|
12
|
+
after_deliver :log_success
|
13
|
+
around_create :benchmark
|
14
|
+
around_deliver :benckmark
|
15
|
+
```
|
16
|
+
|
17
|
+
## Requirements
|
18
|
+
|
19
|
+
This gem is tested only with ActionMailer 2.3.x, gem version dependencies are
|
20
|
+
strict because I needed it to work only on those specific versions. Probably it
|
21
|
+
will work with other versions too, as long as the creation/delivery interface of
|
22
|
+
ActionMailer::Base class is still the same.
|
23
|
+
|
24
|
+
## Notes
|
25
|
+
|
26
|
+
*before_create* is executed even if the mail instantiation process fails due to
|
27
|
+
some error.
|
28
|
+
Since no mail has been created at this point, you can't do that much here,
|
29
|
+
basically it's useful to inspect or log params. You can access the params
|
30
|
+
anywhere via *@params* instance variable.
|
31
|
+
|
32
|
+
*around_create* and *around_deliver* wrap the mail method execution (and all
|
33
|
+
callbacks). You can use them for rescuing from errors or for benchmarking, for
|
34
|
+
example.
|
35
|
+
There can be only 1 around_create or around_deliver method for each email method,
|
36
|
+
if there are more than 1 only the first will be executed.
|
37
|
+
|
38
|
+
## Example
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class UserMailer < ActionMailer::Base
|
42
|
+
before_create :log_params
|
43
|
+
after_deliver :log_success
|
44
|
+
around_create :rescue_from_errors
|
45
|
+
|
46
|
+
def user_registration(user)
|
47
|
+
# this is a regular ActionMailer email method
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def log_params
|
53
|
+
MailerLogger.info "[CREATE] #{params_inspector}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def log_success
|
57
|
+
MailerLogger.info "[DELIVERED] #{params_inspector}"
|
58
|
+
end
|
59
|
+
|
60
|
+
def rescue_from_errors
|
61
|
+
begin
|
62
|
+
yield
|
63
|
+
rescue
|
64
|
+
puts 'An error occured!'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def params_inspector
|
69
|
+
@params.present? && @params.inject([]) do |lines, param|
|
70
|
+
message = begin
|
71
|
+
if param.is_a?(ActiveRecord::Base)
|
72
|
+
"#{param.class.name.downcase}.id = #{param.id}"
|
73
|
+
else
|
74
|
+
param
|
75
|
+
end
|
76
|
+
end
|
77
|
+
lines << message
|
78
|
+
end.join(', ')
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
desc "Run those specs"
|
5
|
+
task :spec do
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
7
|
+
t.rspec_opts = %w{--colour --format progress}
|
8
|
+
t.pattern = 'spec/**/*_spec.rb'
|
9
|
+
t.rspec_path = 'bundle exec rspec'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => :spec
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require 'actionmailer/callbacks/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "actionmailer-callbacks"
|
7
|
+
s.version = Actionmailer::Callbacks::VERSION
|
8
|
+
s.authors = ["andrea longhi"]
|
9
|
+
s.email = ["andrea@spaghetticode.it"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{add callbacks to actionmailer 2.3.11}
|
12
|
+
s.description = %q{add callbacks to actionmailer 2.3.11}
|
13
|
+
|
14
|
+
# s.rubyforge_project = 'actionmailer-callbacks'
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ['lib']
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency 'rspec'
|
23
|
+
s.add_development_dependency 'rake'
|
24
|
+
s.add_runtime_dependency 'activesupport', '~> 2.3.11'
|
25
|
+
s.add_runtime_dependency 'actionmailer', '~> 2.3.11'
|
26
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_support'
|
3
|
+
require 'action_mailer'
|
4
|
+
|
5
|
+
module ActionMailer
|
6
|
+
module Callbacks
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'actionmailer/callbacks/version'
|
11
|
+
require 'actionmailer/callbacks/callback'
|
12
|
+
require 'actionmailer/callbacks/methods'
|
13
|
+
|
14
|
+
ActionMailer::Base.send :include, ActionMailer::Callbacks::Methods
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActionMailer
|
2
|
+
module Callbacks
|
3
|
+
# this object stores the callback data
|
4
|
+
class Callback
|
5
|
+
attr_accessor :name, :except, :only
|
6
|
+
|
7
|
+
def initialize(callback_name, opts={})
|
8
|
+
@name = callback_name
|
9
|
+
@only = Array.wrap(opts[:only]).map(&:to_s)
|
10
|
+
@except = Array.wrap(opts[:except]).map(&:to_s)
|
11
|
+
end
|
12
|
+
|
13
|
+
def should_run?(mailer_method)
|
14
|
+
if @only.present?
|
15
|
+
@only.include?(mailer_method.to_s)
|
16
|
+
else
|
17
|
+
!@except.include?(mailer_method.to_s)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# this implements those class methods to be used in your mailer model:
|
2
|
+
# after_create :do_something
|
3
|
+
# before_deliver :log_data
|
4
|
+
# after_deliver :log_data, :only => :notify_customer
|
5
|
+
# after_deliver :log_data, :except => [:notify_customer, :send_xmas_regards]
|
6
|
+
|
7
|
+
module ActionMailer
|
8
|
+
module Callbacks
|
9
|
+
module Methods
|
10
|
+
CALLBACK_TYPES = %w[before_create after_create before_deliver after_deliver]
|
11
|
+
AROUND_METHODS = %w[around_create around_deliver]
|
12
|
+
|
13
|
+
def self.included(base)
|
14
|
+
base.extend ClassMethods
|
15
|
+
base.alias_method_chain :create!, :create_callbacks
|
16
|
+
base.alias_method_chain :deliver!, :deliver_callbacks
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
# this creates a series of methods like these:
|
21
|
+
# def after_create(*args)
|
22
|
+
# after_create_callbacks << Callback.new(*args)
|
23
|
+
# end
|
24
|
+
|
25
|
+
# def after_create_callbacks
|
26
|
+
# @after_create_callbacks ||= []
|
27
|
+
# end
|
28
|
+
CALLBACK_TYPES.each do |type|
|
29
|
+
define_method type do |*args|
|
30
|
+
send("#{type}_callbacks") << Callback.new(*args)
|
31
|
+
end
|
32
|
+
|
33
|
+
name = "#{type}_callbacks"
|
34
|
+
define_method name do
|
35
|
+
instance_variable_get("@#{name}") || instance_variable_set("@#{name}", [])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def clear_callbacks
|
40
|
+
CALLBACK_TYPES.each do |type|
|
41
|
+
instance_variable_set "@#{type}_callbacks", []
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def around_create(*args)
|
46
|
+
around_create_methods << Callback.new(*args)
|
47
|
+
end
|
48
|
+
|
49
|
+
def around_create_methods
|
50
|
+
@around_create_methods ||= []
|
51
|
+
end
|
52
|
+
|
53
|
+
def around_deliver(*args)
|
54
|
+
around_deliver_methods << Callback.new(*args)
|
55
|
+
end
|
56
|
+
|
57
|
+
def around_deliver_methods
|
58
|
+
@around_deliver_methods ||= []
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def create_with_create_callbacks!(*args)
|
63
|
+
@params = args
|
64
|
+
run_around_create_method do
|
65
|
+
run_before_create_callbacks
|
66
|
+
create_without_create_callbacks!(*args)
|
67
|
+
run_after_create_callbacks
|
68
|
+
@mail
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def deliver_with_deliver_callbacks!(*args)
|
73
|
+
run_around_deliver_method do
|
74
|
+
run_before_deliver_callbacks
|
75
|
+
deliver_without_deliver_callbacks!(*args)
|
76
|
+
run_after_deliver_callbacks
|
77
|
+
@mail
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# this creates a series of methods similar to this:
|
82
|
+
# def run_after_create_callbacks
|
83
|
+
# self.class.send('after_create_callbacks').each do |callback|
|
84
|
+
# send callback.name if callback.should_run?(@params.first)
|
85
|
+
# end
|
86
|
+
# end
|
87
|
+
CALLBACK_TYPES.each do |type|
|
88
|
+
define_method "run_#{type}_callbacks" do
|
89
|
+
self.class.send("#{type}_callbacks").each do |callback|
|
90
|
+
send callback.name if callback.should_run?(@params.first)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def run_around_create_method(&block)
|
96
|
+
self.class.send('around_create_methods').each do |callback|
|
97
|
+
if callback.should_run?(@params.first)
|
98
|
+
send callback.name, &block
|
99
|
+
return
|
100
|
+
end
|
101
|
+
end
|
102
|
+
block.call
|
103
|
+
end
|
104
|
+
|
105
|
+
def run_around_deliver_method(&block)
|
106
|
+
self.class.send('around_deliver_methods').each do |callback|
|
107
|
+
if callback.should_run?(@params.first)
|
108
|
+
send callback.name, &block
|
109
|
+
return
|
110
|
+
end
|
111
|
+
end
|
112
|
+
block.call
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActionMailer::Callbacks::Callback do
|
4
|
+
describe 'a new callback' do
|
5
|
+
let(:callback) { ActionMailer::Callbacks::Callback.new('name', :except => ['except', :notthis], :only => :yesthis) }
|
6
|
+
|
7
|
+
it 'should have "name", "except", "only" accessors' do
|
8
|
+
%w[name except only].each do |attribute|
|
9
|
+
callback.send "#{attribute}=", 'asd'
|
10
|
+
callback.send(attribute).should == 'asd'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should stringify symbols in "except" and "only" arrays' do
|
15
|
+
callback.instance_eval do
|
16
|
+
@except.should == ['except', 'notthis']
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'should_run?' do
|
22
|
+
context 'when "except" is set' do
|
23
|
+
let(:callback) { ActionMailer::Callbacks::Callback.new('name', :except => ['except']) }
|
24
|
+
it 'should be false for except' do
|
25
|
+
callback.should_run?('except').should be_false
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'be true for anything else' do
|
29
|
+
callback.should_run?('other').should be_true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when "only" is set' do
|
34
|
+
let(:callback) { ActionMailer::Callbacks::Callback.new('name', :only => :this_method_name)}
|
35
|
+
|
36
|
+
it 'should be be true for this_method_name' do
|
37
|
+
callback.should_run?('this_method_name').should be_true
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'should be be false for anything else' do
|
41
|
+
callback.should_run?('another_method_name').should be_false
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'when neither "except" or "only" are set' do
|
46
|
+
let(:callback) { ActionMailer::Callbacks::Callback.new('name') }
|
47
|
+
|
48
|
+
it 'should be true for any method' do
|
49
|
+
callback.should_run?('any_method_name').should be_true
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class MailerWithCallbacks < ActionMailer::Base
|
2
|
+
before_create :set_before_create_flag, :except => :no_callback
|
3
|
+
after_create :set_after_create_flag, :except => [:no_callback]
|
4
|
+
before_deliver :set_before_deliver_flag, :only => :test_email
|
5
|
+
after_deliver :set_after_deliver_flag
|
6
|
+
around_create :create_wrapper_flag
|
7
|
+
around_deliver :deliver_wrapper_flag
|
8
|
+
|
9
|
+
def test_email(recipient)
|
10
|
+
recipients recipient
|
11
|
+
from 'test@test.com'
|
12
|
+
subject 'test email'
|
13
|
+
body 'a test email'
|
14
|
+
end
|
15
|
+
|
16
|
+
def no_callback(recipient)
|
17
|
+
test_email(recipient)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def create_wrapper_flag
|
23
|
+
Flag.create_before_block_call = true
|
24
|
+
yield
|
25
|
+
Flag.create_after_block_call = true
|
26
|
+
end
|
27
|
+
|
28
|
+
def deliver_wrapper_flag
|
29
|
+
Flag.deliver_before_block_call = true
|
30
|
+
yield
|
31
|
+
Flag.deliver_after_block_call = true
|
32
|
+
end
|
33
|
+
|
34
|
+
def set_before_create_flag
|
35
|
+
Flag.before_create = true
|
36
|
+
end
|
37
|
+
|
38
|
+
def set_after_create_flag
|
39
|
+
Flag.after_create = true
|
40
|
+
end
|
41
|
+
|
42
|
+
def set_before_deliver_flag
|
43
|
+
Flag.before_deliver = true
|
44
|
+
end
|
45
|
+
|
46
|
+
def set_after_deliver_flag
|
47
|
+
Flag.after_deliver = true
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MailerWithCallbacks do
|
4
|
+
before { Flag.reset }
|
5
|
+
|
6
|
+
context 'when delivering a mail via deliver_* class method' do
|
7
|
+
context 'when the mailer method has callbacks' do
|
8
|
+
before { MailerWithCallbacks.deliver_test_email('asd@asd.it') }
|
9
|
+
|
10
|
+
it 'should run before_create callback' do
|
11
|
+
Flag.before_create.should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should run after_create callback' do
|
15
|
+
Flag.after_create.should be_true
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should run before_deliver callback' do
|
19
|
+
Flag.before_deliver.should be_true
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should run after_deliver callback' do
|
23
|
+
Flag.after_deliver.should be_true
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should run around_create method' do
|
27
|
+
Flag.create_before_block_call.should be_true
|
28
|
+
Flag.create_after_block_call.should be_true
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should run around_deliver method' do
|
32
|
+
Flag.deliver_before_block_call.should be_true
|
33
|
+
Flag.deliver_after_block_call.should be_true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when the mailer method is included in only/except options' do
|
38
|
+
before { MailerWithCallbacks.deliver_no_callback('asd@asd.it') }
|
39
|
+
|
40
|
+
it 'should not run the callback when in except' do
|
41
|
+
Flag.after_create.should_not be_true
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should not run the callback when not in only' do
|
45
|
+
Flag.before_deliver.should_not be_true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'when the mailer method is not included in any restricting options' do
|
50
|
+
before { MailerWithCallbacks.deliver_test_email('asd@asd.it') }
|
51
|
+
|
52
|
+
it 'should run after_deliver callback' do
|
53
|
+
Flag.after_deliver.should be_true
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'when error is raised on delivery' do
|
58
|
+
before do
|
59
|
+
MailerWithCallbacks.any_instance.should_receive(:deliver_without_deliver_callbacks!).and_raise(ArgumentError)
|
60
|
+
MailerWithCallbacks.deliver_test_email('asd@asd.it') rescue nil
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should run before_create callback' do
|
64
|
+
Flag.before_create.should be_true
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should run after_create callback' do
|
68
|
+
Flag.after_create.should be_true
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'should run before_deliver callback' do
|
72
|
+
Flag.before_deliver.should be_true
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should not run after_deliver callback' do
|
76
|
+
Flag.after_deliver.should_not be_true
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'when error is raised on creation' do
|
81
|
+
context 'when error is raised on delivery' do
|
82
|
+
before do
|
83
|
+
MailerWithCallbacks.any_instance.should_receive(:create_without_create_callbacks!).and_raise(ArgumentError)
|
84
|
+
MailerWithCallbacks.create_test_email('asd@asd.it') rescue nil
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'should run before_create callback' do
|
88
|
+
Flag.before_create.should be_true
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'should not run after_create callback' do
|
92
|
+
Flag.after_create.should_not be_true
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'should not run before_deliver callback' do
|
96
|
+
Flag.before_deliver.should_not be_true
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'should not run after_deliver callback' do
|
100
|
+
Flag.after_deliver.should_not be_true
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'when creating a mail via create_* class method' do
|
107
|
+
before { MailerWithCallbacks.create_test_email('asd@asd.it') }
|
108
|
+
|
109
|
+
it 'should run before_create callback' do
|
110
|
+
Flag.before_create.should be_true
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'should run after_create callback' do
|
114
|
+
Flag.after_create.should be_true
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'should not run before_deliver callback' do
|
118
|
+
Flag.before_deliver.should_not be_true
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'should not run after_deliver callback' do
|
122
|
+
Flag.after_deliver.should_not be_true
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'should run around_create method' do
|
126
|
+
Flag.create_before_block_call.should be_true
|
127
|
+
Flag.create_after_block_call.should be_true
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'should not run around_deliver method' do
|
131
|
+
Flag.deliver_before_block_call.should_not be_true
|
132
|
+
Flag.deliver_after_block_call.should_not be_true
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActionMailer::Callbacks::Methods do
|
4
|
+
def callback_lists
|
5
|
+
%w[before_create_callbacks after_create_callbacks before_deliver_callbacks after_deliver_callbacks]
|
6
|
+
end
|
7
|
+
|
8
|
+
before { MailerWithoutCallbacks.clear_callbacks }
|
9
|
+
|
10
|
+
it 'callbacks lists should be empty' do
|
11
|
+
callback_lists.each do |list|
|
12
|
+
MailerWithoutCallbacks.send(list).should be_empty
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when adding a after create callback' do
|
17
|
+
before do
|
18
|
+
MailerWithoutCallbacks.before_create('name')
|
19
|
+
MailerWithoutCallbacks.after_create('name')
|
20
|
+
MailerWithoutCallbacks.before_deliver('name')
|
21
|
+
MailerWithoutCallbacks.after_deliver('name')
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should add the callbacks to the list' do
|
25
|
+
callback_lists.each do |list|
|
26
|
+
MailerWithoutCallbacks.send(list).should have(1).item
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'clear_callbacks should clear all lists' do
|
31
|
+
MailerWithoutCallbacks.clear_callbacks
|
32
|
+
callback_lists.each do |list|
|
33
|
+
MailerWithoutCallbacks.send(list).should be_empty
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'when delivering a mail via method missing' do
|
39
|
+
it 'should call before_create callback handing method' do
|
40
|
+
MailerWithoutCallbacks.any_instance.should_receive(:run_before_create_callbacks)
|
41
|
+
MailerWithoutCallbacks.deliver_test_email('asd@asd.it')
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'should call after_create callback handing method' do
|
45
|
+
MailerWithoutCallbacks.any_instance.should_receive(:run_after_create_callbacks)
|
46
|
+
MailerWithoutCallbacks.deliver_test_email('asd@asd.it')
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should call before_deliver callback handing method' do
|
50
|
+
MailerWithoutCallbacks.any_instance.should_receive(:run_before_deliver_callbacks)
|
51
|
+
MailerWithoutCallbacks.deliver_test_email('asd@asd.it')
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should call after_deliver callback handing method' do
|
55
|
+
MailerWithoutCallbacks.any_instance.should_receive(:run_after_deliver_callbacks)
|
56
|
+
MailerWithoutCallbacks.deliver_test_email('asd@asd.it')
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
describe 'when creating a mail via method_missing' do
|
62
|
+
it 'should call before_create callback handing method' do
|
63
|
+
MailerWithoutCallbacks.any_instance.should_receive(:run_before_create_callbacks)
|
64
|
+
MailerWithoutCallbacks.create_test_email('asd@asd.it')
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should call after_create callback handing method' do
|
68
|
+
MailerWithoutCallbacks.any_instance.should_receive(:run_after_create_callbacks)
|
69
|
+
MailerWithoutCallbacks.create_test_email('asd@asd.it')
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'should not call before_deliver callback handing method' do
|
73
|
+
MailerWithoutCallbacks.any_instance.should_not_receive(:run_before_deliver_callbacks)
|
74
|
+
MailerWithoutCallbacks.create_test_email('asd@asd.it')
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'should not call after_deliver callback handing method' do
|
78
|
+
MailerWithoutCallbacks.any_instance.should_not_receive(:run_after_deliver_callbacks)
|
79
|
+
MailerWithoutCallbacks.create_test_email('asd@asd.it')
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# load required files
|
2
|
+
current_dir = File.dirname(__FILE__)
|
3
|
+
require File.join(current_dir, '../lib/actionmailer-callbacks')
|
4
|
+
Dir[File.expand_path(File.join(current_dir, 'support', '**', '*.rb'))].each {|f| require f}
|
5
|
+
Dir[File.expand_path(File.join(current_dir, 'fixtures', '**', '*.rb'))].each {|f| require f}
|
6
|
+
|
7
|
+
# don't raise email sending erros
|
8
|
+
ActionMailer::Base.raise_delivery_errors = false
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Flag
|
2
|
+
ACCESSORS = :before_create, :after_create, :before_deliver, :after_deliver,
|
3
|
+
:create_before_block_call, :create_after_block_call,
|
4
|
+
:deliver_before_block_call, :deliver_after_block_call
|
5
|
+
|
6
|
+
mattr_accessor *ACCESSORS
|
7
|
+
|
8
|
+
def self.reset
|
9
|
+
ACCESSORS.each do |accessor|
|
10
|
+
send "#{accessor}=", nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.included
|
15
|
+
raise 'Not to be included anywhere!'
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.extended
|
19
|
+
included
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: actionmailer-callbacks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- andrea longhi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
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
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: activesupport
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.3.11
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.3.11
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: actionmailer
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.3.11
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 2.3.11
|
78
|
+
description: add callbacks to actionmailer 2.3.11
|
79
|
+
email:
|
80
|
+
- andrea@spaghetticode.it
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- README.md
|
88
|
+
- Rakefile
|
89
|
+
- actionmailer-callbacks.gemspec
|
90
|
+
- lib/actionmailer-callbacks.rb
|
91
|
+
- lib/actionmailer/callbacks/callback.rb
|
92
|
+
- lib/actionmailer/callbacks/methods.rb
|
93
|
+
- lib/actionmailer/callbacks/version.rb
|
94
|
+
- spec/callback_spec.rb
|
95
|
+
- spec/fixtures/mailer_with_callbacks.rb
|
96
|
+
- spec/fixtures/mailer_without_callbacks.rb
|
97
|
+
- spec/integration/action_mailer_integration_spec.rb
|
98
|
+
- spec/methods_spec.rb
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
- spec/support/flag.rb
|
101
|
+
homepage: ''
|
102
|
+
licenses: []
|
103
|
+
post_install_message:
|
104
|
+
rdoc_options: []
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ! '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 1.8.24
|
122
|
+
signing_key:
|
123
|
+
specification_version: 3
|
124
|
+
summary: add callbacks to actionmailer 2.3.11
|
125
|
+
test_files:
|
126
|
+
- spec/callback_spec.rb
|
127
|
+
- spec/fixtures/mailer_with_callbacks.rb
|
128
|
+
- spec/fixtures/mailer_without_callbacks.rb
|
129
|
+
- spec/integration/action_mailer_integration_spec.rb
|
130
|
+
- spec/methods_spec.rb
|
131
|
+
- spec/spec_helper.rb
|
132
|
+
- spec/support/flag.rb
|