actionmailer-callbacks 0.0.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,22 +0,0 @@
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
@@ -1,116 +0,0 @@
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
@@ -1,53 +0,0 @@
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
@@ -1,49 +0,0 @@
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
@@ -1,8 +0,0 @@
1
- class MailerWithoutCallbacks < ActionMailer::Base
2
- def test_email(recipient)
3
- recipients recipient
4
- from 'test@test.com'
5
- subject 'test email'
6
- body 'a test email'
7
- end
8
- end
@@ -1,135 +0,0 @@
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
@@ -1,82 +0,0 @@
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