actionmailer-callbacks 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +1,9 @@
1
- require 'rubygems'
2
- require 'active_support'
3
1
  require 'action_mailer'
4
2
 
5
- module ActionMailer
6
- module Callbacks
7
- end
8
- end
3
+ lib_dir = File.expand_path('..', __FILE__)
4
+ require File.join(lib_dir, 'actionmailer-callbacks/version')
5
+ require File.join(lib_dir, 'actionmailer-callbacks/callback')
6
+ require File.join(lib_dir, 'actionmailer-callbacks/callbackable')
7
+ require File.join(lib_dir, 'actionmailer-callbacks/extensions')
9
8
 
10
- require 'actionmailer/callbacks/version'
11
- require 'actionmailer/callbacks/callback'
12
- require 'actionmailer/callbacks/methods'
13
-
14
- ActionMailer::Base.send :include, ActionMailer::Callbacks::Methods
9
+ ActionMailer::Base.extend ActionMailer::Callbacks::Extensions
@@ -0,0 +1,25 @@
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
@@ -0,0 +1,44 @@
1
+ module ActionMailer
2
+ module Callbacks
3
+ module Callbackable
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def before_create_callbacks
8
+ @before_create_callbacks ||= Set.new
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
22
+ end
23
+
24
+ def initialize(method, *args)
25
+ result = nil
26
+ around_create_callback = self.class.around_create_callback
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
39
+ super
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,24 @@
1
+ module ActionMailer
2
+ module Callbacks
3
+ module Extensions
4
+ def before_create(*args)
5
+ include_callbackable
6
+ before_create_callbacks << Callback.new(*args)
7
+ end
8
+
9
+ def around_create(*args)
10
+ include_callbackable
11
+ @around_create_callback = Callback.new(*args)
12
+ end
13
+
14
+ private
15
+
16
+ def include_callbackable
17
+ callbackable = ActionMailer::Callbacks::Callbackable
18
+ unless ancestors.include?(callbackable)
19
+ include callbackable
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,5 @@
1
1
  module Actionmailer
2
2
  module Callbacks
3
- VERSION = '0.0.2'
3
+ VERSION = '1.0.0'
4
4
  end
5
5
  end
@@ -0,0 +1,49 @@
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
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+
3
+ module ActionMailer
4
+ module Callbacks
5
+ describe Callbackable do
6
+ let(:sample_class) {Class.new {include Callbackable}}
7
+ let(:callback) {double(name: :callback_method)}
8
+
9
+ subject {sample_class}
10
+
11
+ before {subject.reset_callbacks}
12
+
13
+ context '#initialize' do
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
53
+
54
+ context 'when the callback should not run' do
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
91
+
92
+ describe 'reset_callbacks' do
93
+ it 'empties the before_create_callbacks list' do
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
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ module ActionMailer
4
+ module Callbacks
5
+ describe Extensions do
6
+ subject {Class.new {extend Extensions}}
7
+
8
+ context 'class methods' do
9
+ it 'responds to before_create' do
10
+ subject.should respond_to(:before_create)
11
+ end
12
+
13
+ it 'responds to around_create' do
14
+ subject.should respond_to(:around_create)
15
+ end
16
+
17
+ describe 'before_create' do
18
+ it 'adds Callbackable to the ancestors' do
19
+ Callback.stub(:new)
20
+ subject.before_create
21
+ subject.ancestors.should include(ActionMailer::Callbacks::Callbackable)
22
+ end
23
+ end
24
+
25
+ describe 'around_create' do
26
+ it 'adds Callbackable to the ancestors' do
27
+ Callback.stub(:new)
28
+ subject.around_create
29
+ subject.ancestors.should include(ActionMailer::Callbacks::Callbackable)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,8 +1,2 @@
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
1
+ require 'support/custom_matchers'
2
+ require File.expand_path('../../lib/actionmailer-callbacks', __FILE__)
@@ -0,0 +1,5 @@
1
+ RSpec::Matchers.define :run do |method|
2
+ match do |callback|
3
+ callback.run?(method) == true
4
+ end
5
+ 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: 0.0.2
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -12,14 +12,30 @@ cert_chain: []
12
12
  date: 2012-05-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rspec
15
+ name: actionmailer
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
16
32
  requirement: !ruby/object:Gem::Requirement
17
33
  none: false
18
34
  requirements:
19
35
  - - ! '>='
20
36
  - !ruby/object:Gem::Version
21
37
  version: '0'
22
- type: :development
38
+ type: :runtime
23
39
  prerelease: false
24
40
  version_requirements: !ruby/object:Gem::Requirement
25
41
  none: false
@@ -44,38 +60,70 @@ dependencies:
44
60
  - !ruby/object:Gem::Version
45
61
  version: '0'
46
62
  - !ruby/object:Gem::Dependency
47
- name: activesupport
63
+ name: rspec
48
64
  requirement: !ruby/object:Gem::Requirement
49
65
  none: false
50
66
  requirements:
51
67
  - - ~>
52
68
  - !ruby/object:Gem::Version
53
- version: 2.3.11
54
- type: :runtime
69
+ version: '2.0'
70
+ type: :development
55
71
  prerelease: false
56
72
  version_requirements: !ruby/object:Gem::Requirement
57
73
  none: false
58
74
  requirements:
59
75
  - - ~>
60
76
  - !ruby/object:Gem::Version
61
- version: 2.3.11
77
+ version: '2.0'
62
78
  - !ruby/object:Gem::Dependency
63
- name: actionmailer
79
+ name: cucumber
64
80
  requirement: !ruby/object:Gem::Requirement
65
81
  none: false
66
82
  requirements:
67
83
  - - ~>
68
84
  - !ruby/object:Gem::Version
69
- version: 2.3.11
70
- type: :runtime
85
+ version: '1.0'
86
+ type: :development
71
87
  prerelease: false
72
88
  version_requirements: !ruby/object:Gem::Requirement
73
89
  none: false
74
90
  requirements:
75
91
  - - ~>
76
92
  - !ruby/object:Gem::Version
77
- version: 2.3.11
78
- description: add callbacks to actionmailer 2.3.11
93
+ version: '1.0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: guard-rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: guard-cucumber
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: adds before_create, around_create callbacks to action mailer 3
79
127
  email:
80
128
  - andrea@spaghetticode.it
81
129
  executables: []
@@ -83,23 +131,31 @@ extensions: []
83
131
  extra_rdoc_files: []
84
132
  files:
85
133
  - .gitignore
134
+ - .rspec
86
135
  - .travis
87
136
  - Gemfile
137
+ - Guardfile
138
+ - LICENSE
88
139
  - README.md
89
140
  - Rakefile
90
141
  - actionmailer-callbacks.gemspec
142
+ - features/around_create_callback.feature
143
+ - features/before_create_callback.feature
144
+ - features/step_definitions/before_create_callback_steps.rb
145
+ - features/support/env.rb
146
+ - features/support/hooks.rb
147
+ - features/support/world_extensions.rb
91
148
  - lib/actionmailer-callbacks.rb
92
- - lib/actionmailer/callbacks/callback.rb
93
- - lib/actionmailer/callbacks/methods.rb
94
- - lib/actionmailer/callbacks/version.rb
95
- - spec/callback_spec.rb
96
- - spec/fixtures/mailer_with_callbacks.rb
97
- - spec/fixtures/mailer_without_callbacks.rb
98
- - spec/integration/action_mailer_integration_spec.rb
99
- - spec/methods_spec.rb
149
+ - lib/actionmailer-callbacks/callback.rb
150
+ - lib/actionmailer-callbacks/callbackable.rb
151
+ - lib/actionmailer-callbacks/extensions.rb
152
+ - lib/actionmailer-callbacks/version.rb
153
+ - spec/lib/actionmailer-callbacks/callback_spec.rb
154
+ - spec/lib/actionmailer-callbacks/callbackable_spec.rb
155
+ - spec/lib/actionmailer-callbacks/extensions_spec.rb
100
156
  - spec/spec_helper.rb
101
- - spec/support/flag.rb
102
- homepage: ''
157
+ - spec/support/custom_matchers.rb
158
+ homepage: http://github.com/spaghetticode/actionmailer-callbacks
103
159
  licenses: []
104
160
  post_install_message:
105
161
  rdoc_options: []
@@ -111,23 +167,33 @@ required_ruby_version: !ruby/object:Gem::Requirement
111
167
  - - ! '>='
112
168
  - !ruby/object:Gem::Version
113
169
  version: '0'
170
+ segments:
171
+ - 0
172
+ hash: 1517825137690287905
114
173
  required_rubygems_version: !ruby/object:Gem::Requirement
115
174
  none: false
116
175
  requirements:
117
176
  - - ! '>='
118
177
  - !ruby/object:Gem::Version
119
178
  version: '0'
179
+ segments:
180
+ - 0
181
+ hash: 1517825137690287905
120
182
  requirements: []
121
183
  rubyforge_project:
122
184
  rubygems_version: 1.8.24
123
185
  signing_key:
124
186
  specification_version: 3
125
- summary: add callbacks to actionmailer 2.3.11
187
+ summary: adds before_create, around_create callbacks to action mailer 3
126
188
  test_files:
127
- - spec/callback_spec.rb
128
- - spec/fixtures/mailer_with_callbacks.rb
129
- - spec/fixtures/mailer_without_callbacks.rb
130
- - spec/integration/action_mailer_integration_spec.rb
131
- - spec/methods_spec.rb
189
+ - features/around_create_callback.feature
190
+ - features/before_create_callback.feature
191
+ - features/step_definitions/before_create_callback_steps.rb
192
+ - features/support/env.rb
193
+ - features/support/hooks.rb
194
+ - features/support/world_extensions.rb
195
+ - spec/lib/actionmailer-callbacks/callback_spec.rb
196
+ - spec/lib/actionmailer-callbacks/callbackable_spec.rb
197
+ - spec/lib/actionmailer-callbacks/extensions_spec.rb
132
198
  - spec/spec_helper.rb
133
- - spec/support/flag.rb
199
+ - spec/support/custom_matchers.rb