actionmailer-callbacks 1.0.0 → 1.1.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.
- data/{.travis → .travis.yml} +3 -0
- data/README.md +22 -15
- data/Rakefile +24 -1
- data/actionmailer-callbacks.gemspec +1 -1
- data/features/around_create_callback.feature +46 -9
- data/features/before_create_callback.feature +43 -10
- data/features/readme.md +89 -0
- data/lib/actionmailer-callbacks.rb +9 -5
- data/lib/actionmailer-callbacks/callbackable.rb +10 -31
- data/lib/actionmailer-callbacks/extensions.rb +18 -9
- data/lib/actionmailer-callbacks/version.rb +2 -2
- data/spec/lib/actionmailer-callbacks/callbackable_spec.rb +7 -87
- data/spec/lib/actionmailer-callbacks/extensions_spec.rb +19 -4
- metadata +6 -12
- data/lib/actionmailer-callbacks/callback.rb +0 -25
- data/spec/lib/actionmailer-callbacks/callback_spec.rb +0 -49
data/{.travis → .travis.yml}
RENAMED
data/README.md
CHANGED
@@ -2,22 +2,24 @@
|
|
2
2
|
|
3
3
|
[](http://travis-ci.org/spaghetticode/actionmailer-callbacks)
|
4
4
|
|
5
|
-
This gem adds before_create and around_create to ActionMailer::Base
|
6
|
-
work similarly to ActionController before/around filters and
|
7
|
-
callbacks:
|
5
|
+
This gem adds ```before_create``` and ```around_create``` to ActionMailer::Base
|
6
|
+
to make it work similarly to ActionController before/around filters and
|
7
|
+
ActiveRecord::Base callbacks:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
before_create
|
11
|
-
around_create
|
10
|
+
before_create :log_params, except: :test_email
|
11
|
+
around_create :benchmark, only: :test_email
|
12
12
|
```
|
13
13
|
|
14
|
-
|
14
|
+
```except``` and ```only``` options are optional and has same functionality as in
|
15
15
|
ActionController.
|
16
16
|
|
17
|
+
Callbacks functionality is implemented using the ActiveSupport::Callbacks module.
|
18
|
+
|
17
19
|
|
18
20
|
## Requirements
|
19
21
|
|
20
|
-
The master branch now works only with
|
22
|
+
The master branch now works only with ActionMailer 3.x, if you need to add
|
21
23
|
callbacks to older versions please refer to the 0.x release.
|
22
24
|
|
23
25
|
|
@@ -31,24 +33,29 @@ Add the gem to the Gemfile:
|
|
31
33
|
|
32
34
|
And then run ```bundle```
|
33
35
|
|
34
|
-
|
36
|
+
|
37
|
+
## Documentation
|
38
|
+
|
39
|
+
You can find some more documentation on the workings of the gem on relish:
|
40
|
+
https://www.relishapp.com/spaghetticode/actionmailer-callbacks/docs
|
41
|
+
|
35
42
|
|
36
43
|
## Notes
|
37
44
|
|
38
45
|
If you need something like before/after deliver callbacks ActionMailer 3.x comes
|
39
|
-
ready for that: you can use an
|
46
|
+
ready for that: you can use an **observer** or an **instrumentation** for that.
|
40
47
|
|
41
|
-
|
42
|
-
|
43
|
-
There can be only one *around_create* method for each email method, if you
|
44
|
-
register more than one only the first will be executed.
|
48
|
+
The **around_create** callback wraps the entire mail method execution. You can
|
49
|
+
use them for rescuing from errors or for benchmarking, for example.
|
45
50
|
|
51
|
+
The gem adds the ```args``` attribute accessor to the mail object in order to
|
52
|
+
be able to access the arguments used for its initialization.
|
46
53
|
|
47
54
|
## Example
|
48
55
|
|
49
56
|
```ruby
|
50
57
|
class UserMailer < ActionMailer::Base
|
51
|
-
before_create :
|
58
|
+
before_create :log_args
|
52
59
|
around_create :rescue_from_errors
|
53
60
|
|
54
61
|
def user_registration(user)
|
@@ -57,7 +64,7 @@ register more than one only the first will be executed.
|
|
57
64
|
|
58
65
|
private
|
59
66
|
|
60
|
-
def
|
67
|
+
def log_args
|
61
68
|
MailerLogger.info "[CREATE] #{args.inspect}"
|
62
69
|
end
|
63
70
|
|
data/Rakefile
CHANGED
@@ -11,4 +11,27 @@ task :spec do
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
|
14
|
+
desc 'Run cucumber features'
|
15
|
+
task :cucumber do
|
16
|
+
require 'cucumber/rake/task'
|
17
|
+
|
18
|
+
Cucumber::Rake::Task.new do |t|
|
19
|
+
t.rcov = false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
desc 'Runs tests on Travis CI'
|
24
|
+
task :travis do
|
25
|
+
["rspec spec", "rake cucumber"].each do |cmd|
|
26
|
+
puts "Starting to run #{cmd}..."
|
27
|
+
system("export DISPLAY=:99.0 && bundle exec #{cmd}")
|
28
|
+
raise "#{cmd} failed!" unless $?.exitstatus == 0
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
if ENV['TRAVIS']
|
33
|
+
task :default => :travis
|
34
|
+
else
|
35
|
+
task :default => [:spec, :cucumber]
|
36
|
+
end
|
37
|
+
|
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
|
|
13
13
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
14
|
gem.name = 'actionmailer-callbacks'
|
15
15
|
gem.require_paths = ["lib"]
|
16
|
-
gem.version =
|
16
|
+
gem.version = ActionMailer::Callbacks::VERSION
|
17
17
|
|
18
18
|
gem.add_dependency 'actionmailer', '~> 3.0'
|
19
19
|
gem.add_dependency 'activesupport'
|
@@ -1,19 +1,22 @@
|
|
1
1
|
Feature: around_create callback
|
2
2
|
As an actionmailer gem user
|
3
|
-
I want to be able to use the around_create callback
|
4
|
-
|
3
|
+
I want to be able to use the *around_create* callback
|
4
|
+
so that I can wrap the email creation process with my callback.
|
5
5
|
|
6
|
-
The
|
6
|
+
The *around_create* callback allows to wrap the email creation
|
7
7
|
process around another method definition.
|
8
|
+
|
8
9
|
The macro accepts the callback name as first arguments and an
|
9
|
-
optional hash with
|
10
|
-
functionally equivalent to the ones of
|
10
|
+
optional hash with *only* and/or *except* keys that are
|
11
|
+
functionally equivalent to the ones of ApplicationController
|
11
12
|
before/after/around create filters: for example in order
|
12
13
|
to run a callback only for the "test" action you should
|
13
14
|
specify one of the following:
|
15
|
+
|
14
16
|
around_create :test_callback, only: :test
|
15
17
|
around_create :test_callback, only: [:test]
|
16
|
-
|
18
|
+
|
19
|
+
|
17
20
|
Scenario: successful around_create calling
|
18
21
|
Given the following mailer class with an around_create callback:
|
19
22
|
"""
|
@@ -34,8 +37,9 @@ Scenario: successful around_create calling
|
|
34
37
|
|
35
38
|
private
|
36
39
|
|
37
|
-
def log_args
|
38
|
-
|
40
|
+
def log_args
|
41
|
+
params = args.flatten.to_sentence
|
42
|
+
self.class.logger << "Test email now being called with #{params}"
|
39
43
|
yield
|
40
44
|
self.class.logger << "Test email was successfully created"
|
41
45
|
end
|
@@ -45,13 +49,14 @@ Scenario: successful around_create calling
|
|
45
49
|
Then an email should have been created
|
46
50
|
And the logger for the class "TestMailer" should contain:
|
47
51
|
"""
|
48
|
-
Test email now being called with
|
52
|
+
Test email now being called with test and recipient@test.com
|
49
53
|
"""
|
50
54
|
And the logger for the class "TestMailer" should contain:
|
51
55
|
"""
|
52
56
|
Test email was successfully created
|
53
57
|
"""
|
54
58
|
|
59
|
+
|
55
60
|
Scenario: around_create callback skipped because not included in "only" directive
|
56
61
|
Given the following mailer class with an around_create callback:
|
57
62
|
"""
|
@@ -82,3 +87,35 @@ Scenario: successful around_create calling
|
|
82
87
|
When I run the code "TestMailer.test('recipient@test.com')"
|
83
88
|
Then an email should have been created
|
84
89
|
And the logger for the class "TestMailer" should be empty
|
90
|
+
|
91
|
+
|
92
|
+
Scenario: around_create callback skipped because included in "except" directive
|
93
|
+
Given the following mailer class with an around_create callback:
|
94
|
+
"""
|
95
|
+
class TestMailer < ::ActionMailer::Base
|
96
|
+
around_create :log_args, except: :test
|
97
|
+
|
98
|
+
def self.logger
|
99
|
+
@logger ||= Array.new
|
100
|
+
end
|
101
|
+
|
102
|
+
def test(recipient)
|
103
|
+
mail(
|
104
|
+
to: recipient,
|
105
|
+
from: 'sender@test.com',
|
106
|
+
subject: 'Test Email'
|
107
|
+
)
|
108
|
+
end
|
109
|
+
|
110
|
+
private
|
111
|
+
|
112
|
+
def log_args(*args)
|
113
|
+
self.class.logger << "Test email now being called"
|
114
|
+
yield
|
115
|
+
self.class.logger << "Test email was successfully created"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
"""
|
119
|
+
When I run the code "TestMailer.test('recipient@test.com')"
|
120
|
+
Then an email should have been created
|
121
|
+
And the logger for the class "TestMailer" should be empty
|
@@ -1,19 +1,21 @@
|
|
1
1
|
Feature: before_create callback
|
2
2
|
As an actionmailer gem user
|
3
3
|
I want to be able to use the before_create callback
|
4
|
-
|
4
|
+
so that I can to execute code before creating an email
|
5
5
|
|
6
|
-
The
|
6
|
+
The *before_create* callback allows to execute a callback
|
7
7
|
before the email creation process. The macro accepts
|
8
8
|
the callback name as first arguments and an
|
9
|
-
optional hash with
|
10
|
-
functionally equivalent to the ones of
|
9
|
+
optional hash with *only* and/or *except* keys that are
|
10
|
+
functionally equivalent to the ones of ApplicationController
|
11
11
|
before/after/around create filters: for example in order
|
12
|
-
to run a callback only for the
|
12
|
+
to run a callback only for the *test* action you should
|
13
13
|
specify one of the following:
|
14
|
+
|
14
15
|
before_create :test_callback, only: :test
|
15
16
|
before_create :test_callback, only: [:test]
|
16
17
|
|
18
|
+
|
17
19
|
Scenario: successful before_create callback calling
|
18
20
|
Given the following mailer class with a before_create callback:
|
19
21
|
"""
|
@@ -34,8 +36,8 @@ Scenario: successful before_create callback calling
|
|
34
36
|
|
35
37
|
private
|
36
38
|
|
37
|
-
def log_args
|
38
|
-
self.class.logger << "Test was called with #{args.
|
39
|
+
def log_args
|
40
|
+
self.class.logger << "Test was called with #{args.to_sentence}"
|
39
41
|
end
|
40
42
|
end
|
41
43
|
"""
|
@@ -43,9 +45,10 @@ Scenario: successful before_create callback calling
|
|
43
45
|
Then an email should have been sent
|
44
46
|
And the logger for the class "TestMailer" should contain:
|
45
47
|
"""
|
46
|
-
Test was called with
|
48
|
+
Test was called with test and recipient@test.com
|
47
49
|
"""
|
48
50
|
|
51
|
+
|
49
52
|
Scenario: before_create callback skipped because not included in "only" directive
|
50
53
|
Given the following mailer class with a before_create callback:
|
51
54
|
"""
|
@@ -66,8 +69,38 @@ Scenario: successful before_create callback calling
|
|
66
69
|
|
67
70
|
private
|
68
71
|
|
69
|
-
def log_args
|
70
|
-
self.class.logger << "Test was called with #{args.
|
72
|
+
def log_args
|
73
|
+
self.class.logger << "Test was called with #{args.to_sentence}"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
"""
|
77
|
+
When I run the code "TestMailer.test('recipient@test.com').deliver"
|
78
|
+
Then an email should have been sent
|
79
|
+
And the logger for the class "TestMailer" should be empty
|
80
|
+
|
81
|
+
|
82
|
+
Scenario: before_create callback skipped because included in "except" directive
|
83
|
+
Given the following mailer class with a before_create callback:
|
84
|
+
"""
|
85
|
+
class TestMailer < ::ActionMailer::Base
|
86
|
+
before_create :log_args, except: :test
|
87
|
+
|
88
|
+
def self.logger
|
89
|
+
@logger ||= Array.new
|
90
|
+
end
|
91
|
+
|
92
|
+
def test(recipient)
|
93
|
+
mail(
|
94
|
+
to: recipient,
|
95
|
+
from: 'sender@test.com',
|
96
|
+
subject: 'Test Email'
|
97
|
+
)
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
def log_args
|
103
|
+
self.class.logger << "Test was called with #{args.to_sentence}"
|
71
104
|
end
|
72
105
|
end
|
73
106
|
"""
|
data/features/readme.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# ActionMailer Callbacks
|
2
|
+
|
3
|
+
[](http://travis-ci.org/spaghetticode/actionmailer-callbacks)
|
4
|
+
|
5
|
+
This gem adds ```before_create``` and ```around_create``` to ActionMailer::Base
|
6
|
+
to make it work similarly to ActionController before/around filters and
|
7
|
+
ActiveRecord::Base callbacks:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
before_create :log_params, except: :test_email
|
11
|
+
around_create :benchmark, only: :test_email
|
12
|
+
```
|
13
|
+
|
14
|
+
```except``` and ```only``` options are optional and has same functionality as in
|
15
|
+
ActionController.
|
16
|
+
|
17
|
+
Callbacks functionality is implemented using the ActiveSupport::Callbacks module.
|
18
|
+
|
19
|
+
|
20
|
+
## Requirements
|
21
|
+
|
22
|
+
The master branch now works only with ActionMailer 3.x, if you need to add
|
23
|
+
callbacks to older versions please refer to the 0.x release.
|
24
|
+
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
|
28
|
+
Add the gem to the Gemfile:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
gem 'actionmailer-callbacks'
|
32
|
+
```
|
33
|
+
|
34
|
+
And then run ```bundle```
|
35
|
+
|
36
|
+
|
37
|
+
## Documentation
|
38
|
+
|
39
|
+
You can find some more documentation on the workings of the gem on relish:
|
40
|
+
https://www.relishapp.com/spaghetticode/actionmailer-callbacks/docs
|
41
|
+
|
42
|
+
|
43
|
+
## Notes
|
44
|
+
|
45
|
+
If you need something like before/after deliver callbacks ActionMailer 3.x comes
|
46
|
+
ready for that: you can use an **observer** or an **instrumentation** for that.
|
47
|
+
|
48
|
+
The **around_create** callback wraps the entire mail method execution. You can
|
49
|
+
use them for rescuing from errors or for benchmarking, for example.
|
50
|
+
|
51
|
+
The gem adds the ```args``` attribute accessor to the mail object in order to
|
52
|
+
be able to access the arguments used for its initialization.
|
53
|
+
|
54
|
+
## Example
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
class UserMailer < ActionMailer::Base
|
58
|
+
before_create :log_args
|
59
|
+
around_create :rescue_from_errors
|
60
|
+
|
61
|
+
def user_registration(user)
|
62
|
+
# this is a regular ActionMailer email method
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def log_args
|
68
|
+
MailerLogger.info "[CREATE] #{args.inspect}"
|
69
|
+
end
|
70
|
+
|
71
|
+
def rescue_from_errors
|
72
|
+
begin
|
73
|
+
yield
|
74
|
+
rescue
|
75
|
+
puts 'An error occured!'
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
1. Fork it
|
85
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
86
|
+
3. Add your feature tests to the rspec/cucumber test suite
|
87
|
+
4. Commit your changes (`git commit -am 'Added some feature'`)
|
88
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
89
|
+
6. Create new Pull Request
|
@@ -1,9 +1,13 @@
|
|
1
1
|
require 'action_mailer'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
module ActionMailer
|
4
|
+
module Callbacks
|
5
|
+
autoload :Callbackable, 'actionmailer-callbacks/callbackable'
|
6
|
+
autoload :Extensions, 'actionmailer-callbacks/extensions'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
require File.join(File.expand_path('..', __FILE__), 'actionmailer-callbacks/version')
|
11
|
+
|
8
12
|
|
9
13
|
ActionMailer::Base.extend ActionMailer::Callbacks::Extensions
|
@@ -1,41 +1,20 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'active_support/callbacks'
|
3
|
+
|
1
4
|
module ActionMailer
|
2
5
|
module Callbacks
|
3
6
|
module Callbackable
|
4
7
|
extend ActiveSupport::Concern
|
8
|
+
include ActiveSupport::Callbacks
|
5
9
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
def add_before_create_callback(callback)
|
12
|
-
@before_create_callbacks << callback
|
13
|
-
end
|
14
|
-
|
15
|
-
def around_create_callback
|
16
|
-
@around_create_callback
|
17
|
-
end
|
18
|
-
|
19
|
-
def reset_callbacks
|
20
|
-
@before_create_callbacks = Set.new
|
21
|
-
end
|
10
|
+
included do
|
11
|
+
attr_internal_accessor :args
|
12
|
+
define_callbacks :initialize
|
22
13
|
end
|
23
14
|
|
24
|
-
def initialize(
|
25
|
-
|
26
|
-
|
27
|
-
if around_create_callback and around_create_callback.run?(method)
|
28
|
-
send self.class.around_create_callback.name, *args do
|
29
|
-
self.class.before_create_callbacks.each do |callback|
|
30
|
-
send callback.name, *args if callback.run?(method)
|
31
|
-
end
|
32
|
-
result = super
|
33
|
-
end
|
34
|
-
result
|
35
|
-
else
|
36
|
-
self.class.before_create_callbacks.each do |callback|
|
37
|
-
send callback.name, *args if callback.run?(method)
|
38
|
-
end
|
15
|
+
def initialize(*args)
|
16
|
+
self.args = args
|
17
|
+
run_callbacks :initialize do
|
39
18
|
super
|
40
19
|
end
|
41
20
|
end
|
@@ -1,23 +1,32 @@
|
|
1
1
|
module ActionMailer
|
2
2
|
module Callbacks
|
3
3
|
module Extensions
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
%w[before around].each do |method|
|
5
|
+
define_method "#{method}_create" do |*args| # def before_create(*args)
|
6
|
+
include_callbackable # include_callbackable
|
7
|
+
opts = args.extract_options! # opts = args.extract_options!
|
8
|
+
args.each do |arg| # args.each do |arg|
|
9
|
+
set_callback :initialize, method.to_sym, arg, _conditions(opts) # set_callback :initialize, :before, arg, _conditions(opts)
|
10
|
+
end # end
|
11
|
+
end # end
|
7
12
|
end
|
8
13
|
|
9
|
-
def
|
10
|
-
|
11
|
-
@around_create_callback = Callback.new(*args)
|
14
|
+
def _conditions(opts)
|
15
|
+
_condition(:if, opts[:only]) or _condition(:unless, opts[:except]) or {}
|
12
16
|
end
|
13
17
|
|
14
18
|
private
|
15
19
|
|
20
|
+
def _condition(type, actions)
|
21
|
+
if actions.present?
|
22
|
+
actions = Array.wrap(actions)
|
23
|
+
{type => Proc.new {|mail_obj| actions.include? mail_obj.args.first }}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
16
27
|
def include_callbackable
|
17
28
|
callbackable = ActionMailer::Callbacks::Callbackable
|
18
|
-
unless ancestors.include?(callbackable)
|
19
|
-
include callbackable
|
20
|
-
end
|
29
|
+
include callbackable unless ancestors.include?(callbackable)
|
21
30
|
end
|
22
31
|
end
|
23
32
|
end
|
@@ -4,98 +4,18 @@ module ActionMailer
|
|
4
4
|
module Callbacks
|
5
5
|
describe Callbackable do
|
6
6
|
let(:sample_class) {Class.new {include Callbackable}}
|
7
|
-
let(:callback) {double(name: :callback_method)}
|
8
7
|
|
9
|
-
subject {sample_class}
|
8
|
+
subject {sample_class.new}
|
10
9
|
|
11
|
-
|
10
|
+
describe 'args accessor' do
|
11
|
+
let(:args) {[:some, :args]}
|
12
12
|
|
13
|
-
|
14
|
-
it 'checks for the around_create callback' do
|
15
|
-
sample_class.should_receive(:around_create_callback)
|
16
|
-
sample_class.new(:mailer_method)
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'does not run the around_create callback if present but should not run' do
|
20
|
-
callback.stub(run?: false)
|
21
|
-
sample_class.stub(around_create_callback: callback)
|
22
|
-
sample_class.any_instance.should_not_receive(:callback_method)
|
23
|
-
sample_class.new(:mailer_method)
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'runs the around_create callback if present and should run' do
|
27
|
-
callback.stub(run?: true)
|
28
|
-
sample_class.stub(around_create_callback: callback)
|
29
|
-
sample_class.any_instance.should_receive(:callback_method)
|
30
|
-
sample_class.new(:mailer_method)
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'cycles on before_create_callbacks' do
|
34
|
-
sample_class.should_receive(:before_create_callbacks).and_return([])
|
35
|
-
sample_class.new(:mailer_method)
|
36
|
-
end
|
37
|
-
|
38
|
-
context 'when the callback should run' do
|
39
|
-
before {callback.stub run?: true}
|
40
|
-
|
41
|
-
it 'runs the callback included in the list' do
|
42
|
-
sample_class.stub(before_create_callbacks: [callback])
|
43
|
-
sample_class.any_instance.should_receive(:callback_method)
|
44
|
-
sample_class.new(:mailer_method)
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'pass all the params to the callback' do
|
48
|
-
sample_class.stub(before_create_callbacks: [callback])
|
49
|
-
sample_class.any_instance.should_receive(:callback_method).with(:params)
|
50
|
-
sample_class.new(:mailer_method, :params)
|
51
|
-
end
|
52
|
-
end
|
13
|
+
before {subject.args = args}
|
53
14
|
|
54
|
-
|
55
|
-
before {callback.stub run?: false}
|
56
|
-
|
57
|
-
it 'does not run the callback included in the list' do
|
58
|
-
sample_class.stub(before_create_callbacks: [callback])
|
59
|
-
sample_class.any_instance.should_not_receive(:callback_method)
|
60
|
-
sample_class.new(:mailer_method)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
context 'class methods' do
|
66
|
-
describe 'before_create_callbacks' do
|
67
|
-
it 'responds to before_create_callbacks' do
|
68
|
-
subject.should respond_to(:before_create_callbacks)
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'before_create_callbacks should be empty at start' do
|
72
|
-
subject.before_create_callbacks.should be_empty
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
describe 'add_before_create_callback' do
|
77
|
-
it 'responds to add_before_create_callback' do
|
78
|
-
subject.should respond_to(:add_before_create_callback)
|
79
|
-
end
|
80
|
-
|
81
|
-
it 'adds a callback to the before_create_callbacks list' do
|
82
|
-
subject.add_before_create_callback callback
|
83
|
-
subject.before_create_callbacks.should include(callback)
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'does not add the same callback twice' do
|
87
|
-
2.times {subject.add_before_create_callback callback}
|
88
|
-
subject.before_create_callbacks.should have(1).callback
|
89
|
-
end
|
90
|
-
end
|
15
|
+
it {subject.args.should == args}
|
91
16
|
|
92
|
-
|
93
|
-
|
94
|
-
subject.add_before_create_callback callback
|
95
|
-
lambda do
|
96
|
-
subject.reset_callbacks
|
97
|
-
end.should change(subject, :before_create_callbacks).to(Set.new)
|
98
|
-
end
|
17
|
+
it 'is an internal accessor' do
|
18
|
+
subject.instance_eval {@_args}.should == args
|
99
19
|
end
|
100
20
|
end
|
101
21
|
end
|
@@ -15,20 +15,35 @@ module ActionMailer
|
|
15
15
|
end
|
16
16
|
|
17
17
|
describe 'before_create' do
|
18
|
-
it 'adds Callbackable to the ancestors' do
|
19
|
-
Callback.stub(:new)
|
18
|
+
it 'adds ActionMailer::Callbacks::Callbackable to the ancestors' do
|
20
19
|
subject.before_create
|
21
20
|
subject.ancestors.should include(ActionMailer::Callbacks::Callbackable)
|
22
21
|
end
|
23
22
|
end
|
24
23
|
|
25
24
|
describe 'around_create' do
|
26
|
-
it 'adds Callbackable to the ancestors' do
|
27
|
-
Callback.stub(:new)
|
25
|
+
it 'adds ActionMailer::Callbacks::Callbackable to the ancestors' do
|
28
26
|
subject.around_create
|
29
27
|
subject.ancestors.should include(ActionMailer::Callbacks::Callbackable)
|
30
28
|
end
|
31
29
|
end
|
30
|
+
|
31
|
+
describe '_conditions' do
|
32
|
+
it 'returns empty hash when no "except" or "only" options are provided' do
|
33
|
+
opts = {}
|
34
|
+
subject._conditions(opts).should == {}
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'returns a hash with :if key when "only" option is provided' do
|
38
|
+
opts = {:only => :index}
|
39
|
+
subject._conditions(opts).keys.should == [:if]
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'returns a hash with :unless key when "except" option is provided' do
|
43
|
+
opts = {:except => :index}
|
44
|
+
subject._conditions(opts).keys.should == [:unless]
|
45
|
+
end
|
46
|
+
end
|
32
47
|
end
|
33
48
|
end
|
34
49
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: actionmailer-callbacks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-08-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionmailer
|
@@ -132,7 +132,7 @@ extra_rdoc_files: []
|
|
132
132
|
files:
|
133
133
|
- .gitignore
|
134
134
|
- .rspec
|
135
|
-
- .travis
|
135
|
+
- .travis.yml
|
136
136
|
- Gemfile
|
137
137
|
- Guardfile
|
138
138
|
- LICENSE
|
@@ -141,16 +141,15 @@ files:
|
|
141
141
|
- actionmailer-callbacks.gemspec
|
142
142
|
- features/around_create_callback.feature
|
143
143
|
- features/before_create_callback.feature
|
144
|
+
- features/readme.md
|
144
145
|
- features/step_definitions/before_create_callback_steps.rb
|
145
146
|
- features/support/env.rb
|
146
147
|
- features/support/hooks.rb
|
147
148
|
- features/support/world_extensions.rb
|
148
149
|
- lib/actionmailer-callbacks.rb
|
149
|
-
- lib/actionmailer-callbacks/callback.rb
|
150
150
|
- lib/actionmailer-callbacks/callbackable.rb
|
151
151
|
- lib/actionmailer-callbacks/extensions.rb
|
152
152
|
- lib/actionmailer-callbacks/version.rb
|
153
|
-
- spec/lib/actionmailer-callbacks/callback_spec.rb
|
154
153
|
- spec/lib/actionmailer-callbacks/callbackable_spec.rb
|
155
154
|
- spec/lib/actionmailer-callbacks/extensions_spec.rb
|
156
155
|
- spec/spec_helper.rb
|
@@ -167,18 +166,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
167
166
|
- - ! '>='
|
168
167
|
- !ruby/object:Gem::Version
|
169
168
|
version: '0'
|
170
|
-
segments:
|
171
|
-
- 0
|
172
|
-
hash: 1517825137690287905
|
173
169
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
170
|
none: false
|
175
171
|
requirements:
|
176
172
|
- - ! '>='
|
177
173
|
- !ruby/object:Gem::Version
|
178
174
|
version: '0'
|
179
|
-
segments:
|
180
|
-
- 0
|
181
|
-
hash: 1517825137690287905
|
182
175
|
requirements: []
|
183
176
|
rubyforge_project:
|
184
177
|
rubygems_version: 1.8.24
|
@@ -188,12 +181,13 @@ summary: adds before_create, around_create callbacks to action mailer 3
|
|
188
181
|
test_files:
|
189
182
|
- features/around_create_callback.feature
|
190
183
|
- features/before_create_callback.feature
|
184
|
+
- features/readme.md
|
191
185
|
- features/step_definitions/before_create_callback_steps.rb
|
192
186
|
- features/support/env.rb
|
193
187
|
- features/support/hooks.rb
|
194
188
|
- features/support/world_extensions.rb
|
195
|
-
- spec/lib/actionmailer-callbacks/callback_spec.rb
|
196
189
|
- spec/lib/actionmailer-callbacks/callbackable_spec.rb
|
197
190
|
- spec/lib/actionmailer-callbacks/extensions_spec.rb
|
198
191
|
- spec/spec_helper.rb
|
199
192
|
- spec/support/custom_matchers.rb
|
193
|
+
has_rdoc:
|
@@ -1,25 +0,0 @@
|
|
1
|
-
module ActionMailer
|
2
|
-
module Callbacks
|
3
|
-
class Callback
|
4
|
-
attr_reader :name, :only, :except
|
5
|
-
|
6
|
-
def initialize(name, opts={})
|
7
|
-
@name = name
|
8
|
-
@only = Array.wrap(opts[:only])
|
9
|
-
@except = Array.wrap(opts[:except])
|
10
|
-
end
|
11
|
-
|
12
|
-
def run?(callback_method)
|
13
|
-
if only.include?(callback_method)
|
14
|
-
true
|
15
|
-
else
|
16
|
-
if except.include?(callback_method)
|
17
|
-
false
|
18
|
-
else
|
19
|
-
only.empty? ? true : false
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module ActionMailer
|
4
|
-
module Callbacks
|
5
|
-
describe Callback do
|
6
|
-
context 'creating an instance' do
|
7
|
-
it 'requires name' do
|
8
|
-
expect {Callback.new}.to raise_error(ArgumentError)
|
9
|
-
end
|
10
|
-
|
11
|
-
context 'when name but no option is passed' do
|
12
|
-
subject {Callback.new(:name)}
|
13
|
-
|
14
|
-
it 'runs all callback methods' do
|
15
|
-
subject.should run(:any_method)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
context 'when name and all options are passed' do
|
20
|
-
subject {Callback.new(:name, :only => :only_method, :except => [:except_method])}
|
21
|
-
|
22
|
-
it 'sets "name" attribute' do
|
23
|
-
subject.name.should == :name
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'sets "only" reader' do
|
27
|
-
subject.only.should == [:only_method]
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'sets "except" reader' do
|
31
|
-
subject.except.should == [:except_method]
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'runs the methods included in the "only" list' do
|
35
|
-
subject.should run(:only_method)
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'skips the methods not included in the "only" list' do
|
39
|
-
subject.should_not run(:other_method)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'skips the methods included in the "except" list' do
|
43
|
-
subject.should_not run(:except_method)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|