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.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source "http://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in actionmailer-callbacks.gemspec
4
4
  gemspec
@@ -0,0 +1,17 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
10
+ end
11
+
12
+
13
+ guard 'cucumber' do
14
+ watch(%r{^features/.+\.feature$})
15
+ watch(%r{^features/support/.+$}) { 'features' }
16
+ watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
17
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Andrea Longhi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -2,46 +2,53 @@
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/spaghetticode/actionmailer-callbacks.png)](http://travis-ci.org/spaghetticode/actionmailer-callbacks)
4
4
 
5
- This gem adds the following methods to ActionMailer, similar to ActionController
6
- before/after filters:
5
+ This gem adds before_create and around_create to ActionMailer::Base to make it
6
+ work similarly to ActionController before/around filters and ActiveRecord::Base
7
+ callbacks:
7
8
 
8
9
  ```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
10
+ before_create :log_params, except: :test_email
11
+ around_create :benchmark, only: :test_email
15
12
  ```
16
13
 
14
+ *except* and *only* options are optional and has same functionality as in
15
+ ActionController.
16
+
17
+
17
18
  ## Requirements
18
19
 
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. Some features of this version are
23
- available also on the action mailer v3.x version. Checkout the 1.0 release.
20
+ The master branch now works only with Actionmailer 3.x, if you need to add
21
+ callbacks to older versions please refer to the 0.x release.
22
+
23
+
24
+ ## Installation
25
+
26
+ Add the gem to the Gemfile:
27
+
28
+ ```ruby
29
+ gem 'actionmailer-callbacks'
30
+ ```
31
+
32
+ And then run ```bundle```
33
+
34
+ >>>>>>> actionmailer3
24
35
 
25
36
  ## Notes
26
37
 
27
- *before_create* is executed even if the mail instantiation process fails due to
28
- some error.
29
- Since no mail has been created at this point, you can't do that much here,
30
- basically it's useful to inspect or log params. You can access the params
31
- anywhere via *@params* instance variable.
38
+ If you need something like before/after deliver callbacks ActionMailer 3.x comes
39
+ ready for that: you can use an *observer* or an *instrumentation* for that.
40
+
41
+ *around_create* wrap the mail method execution (and all before_create callbacks).
42
+ You can use them for rescuing from errors or for benchmarking, for example.
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.
32
45
 
33
- *around_create* and *around_deliver* wrap the mail method execution (and all
34
- callbacks). You can use them for rescuing from errors or for benchmarking, for
35
- example.
36
- There can be only 1 around_create or around_deliver method for each email method,
37
- if there are more than 1 only the first will be executed.
38
46
 
39
47
  ## Example
40
48
 
41
49
  ```ruby
42
50
  class UserMailer < ActionMailer::Base
43
51
  before_create :log_params
44
- after_deliver :log_success
45
52
  around_create :rescue_from_errors
46
53
 
47
54
  def user_registration(user)
@@ -50,12 +57,8 @@ if there are more than 1 only the first will be executed.
50
57
 
51
58
  private
52
59
 
53
- def log_params
54
- MailerLogger.info "[CREATE] #{params_inspector}"
55
- end
56
-
57
- def log_success
58
- MailerLogger.info "[DELIVERED] #{params_inspector}"
60
+ def log_params(args)
61
+ MailerLogger.info "[CREATE] #{args.inspect}"
59
62
  end
60
63
 
61
64
  def rescue_from_errors
@@ -65,18 +68,15 @@ if there are more than 1 only the first will be executed.
65
68
  puts 'An error occured!'
66
69
  end
67
70
  end
68
-
69
- def params_inspector
70
- @params.present? && @params.inject([]) do |lines, param|
71
- message = begin
72
- if param.is_a?(ActiveRecord::Base)
73
- "#{param.class.name.downcase}.id = #{param.id}"
74
- else
75
- param
76
- end
77
- end
78
- lines << message
79
- end.join(', ')
80
- end
81
71
  end
82
72
  ```
73
+
74
+
75
+ ## Contributing
76
+
77
+ 1. Fork it
78
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
79
+ 3. Add your feature tests to the rspec/cucumber test suite
80
+ 4. Commit your changes (`git commit -am 'Added some feature'`)
81
+ 5. Push to the branch (`git push origin my-new-feature`)
82
+ 6. Create new Pull Request
data/Rakefile CHANGED
@@ -1,11 +1,12 @@
1
- require "bundler/gem_tasks"
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler/gem_tasks'
2
4
  require 'rspec/core/rake_task'
3
5
 
4
- desc "Run those specs"
6
+ desc 'Run those specs'
5
7
  task :spec do
6
8
  RSpec::Core::RakeTask.new(:spec) do |t|
7
- t.rspec_opts = %w{--colour --format progress}
8
- t.pattern = 'spec/**/*_spec.rb'
9
+ t.pattern = 'spec/**/*_spec.rb'
9
10
  t.rspec_path = 'bundle exec rspec'
10
11
  end
11
12
  end
@@ -1,26 +1,25 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require 'actionmailer/callbacks/version'
2
+ require File.expand_path('../lib/actionmailer-callbacks/version', __FILE__)
4
3
 
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}
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['andrea longhi']
6
+ gem.email = ['andrea@spaghetticode.it']
7
+ gem.description = %q{adds before_create, around_create callbacks to action mailer 3}
8
+ gem.summary = %q{adds before_create, around_create callbacks to action mailer 3}
9
+ gem.homepage = 'http://github.com/spaghetticode/actionmailer-callbacks'
13
10
 
14
- # s.rubyforge_project = 'actionmailer-callbacks'
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = 'actionmailer-callbacks'
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Actionmailer::Callbacks::VERSION
15
17
 
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'
18
+ gem.add_dependency 'actionmailer', '~> 3.0'
19
+ gem.add_dependency 'activesupport'
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec', '~> 2.0'
22
+ gem.add_development_dependency 'cucumber', '~> 1.0'
23
+ gem.add_development_dependency 'guard-rspec'
24
+ gem.add_development_dependency 'guard-cucumber'
26
25
  end
@@ -0,0 +1,84 @@
1
+ Feature: around_create callback
2
+ As an actionmailer gem user
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
+
6
+ The "around_create" callback allows to wrap the email creation
7
+ process around another method definition.
8
+ The macro accepts the callback name as first arguments and an
9
+ optional hash with "only" and/or "except" keys that are
10
+ functionally equivalent to the ones of ActionController
11
+ before/after/around create filters: for example in order
12
+ to run a callback only for the "test" action you should
13
+ specify one of the following:
14
+ around_create :test_callback, only: :test
15
+ around_create :test_callback, only: [:test]
16
+ @focus
17
+ Scenario: successful around_create calling
18
+ Given the following mailer class with an around_create callback:
19
+ """
20
+ class TestMailer < ::ActionMailer::Base
21
+ around_create :log_args
22
+
23
+ def self.logger
24
+ @logger ||= Array.new
25
+ end
26
+
27
+ def test(recipient)
28
+ mail(
29
+ to: recipient,
30
+ from: 'sender@test.com',
31
+ subject: 'Test Email'
32
+ )
33
+ end
34
+
35
+ private
36
+
37
+ def log_args(*args)
38
+ self.class.logger << "Test email now being called with #{args.flatten.inspect}"
39
+ yield
40
+ self.class.logger << "Test email was successfully created"
41
+ end
42
+ end
43
+ """
44
+ When I run the code "TestMailer.test('recipient@test.com')"
45
+ Then an email should have been created
46
+ And the logger for the class "TestMailer" should contain:
47
+ """
48
+ Test email now being called with ["recipient@test.com"]
49
+ """
50
+ And the logger for the class "TestMailer" should contain:
51
+ """
52
+ Test email was successfully created
53
+ """
54
+
55
+ Scenario: around_create callback skipped because not included in "only" directive
56
+ Given the following mailer class with an around_create callback:
57
+ """
58
+ class TestMailer < ::ActionMailer::Base
59
+ around_create :log_args, only: :only_method
60
+
61
+ def self.logger
62
+ @logger ||= Array.new
63
+ end
64
+
65
+ def test(recipient)
66
+ mail(
67
+ to: recipient,
68
+ from: 'sender@test.com',
69
+ subject: 'Test Email'
70
+ )
71
+ end
72
+
73
+ private
74
+
75
+ def log_args(*args)
76
+ self.class.logger << "Test email now being called"
77
+ yield
78
+ self.class.logger << "Test email was successfully created"
79
+ end
80
+ end
81
+ """
82
+ When I run the code "TestMailer.test('recipient@test.com')"
83
+ Then an email should have been created
84
+ And the logger for the class "TestMailer" should be empty
@@ -0,0 +1,76 @@
1
+ Feature: before_create callback
2
+ As an actionmailer gem user
3
+ I want to be able to use the before_create callback
4
+ So that I can to execute code before creating an email
5
+
6
+ The "before_create" callback allows to execute a callback
7
+ before the email creation process. The macro accepts
8
+ the callback name as first arguments and an
9
+ optional hash with "only" and/or "except" keys that are
10
+ functionally equivalent to the ones of ActionController
11
+ before/after/around create filters: for example in order
12
+ to run a callback only for the "test" action you should
13
+ specify one of the following:
14
+ before_create :test_callback, only: :test
15
+ before_create :test_callback, only: [:test]
16
+
17
+ Scenario: successful before_create callback calling
18
+ Given the following mailer class with a before_create callback:
19
+ """
20
+ class TestMailer < ::ActionMailer::Base
21
+ before_create :log_args
22
+
23
+ def self.logger
24
+ @logger ||= Array.new
25
+ end
26
+
27
+ def test(recipient)
28
+ mail(
29
+ to: recipient,
30
+ from: 'sender@test.com',
31
+ subject: 'Test Email'
32
+ )
33
+ end
34
+
35
+ private
36
+
37
+ def log_args(args)
38
+ self.class.logger << "Test was called with #{args.inspect}"
39
+ end
40
+ end
41
+ """
42
+ When I run the code "TestMailer.test('recipient@test.com').deliver"
43
+ Then an email should have been sent
44
+ And the logger for the class "TestMailer" should contain:
45
+ """
46
+ Test was called with "recipient@test.com"
47
+ """
48
+
49
+ Scenario: before_create callback skipped because not included in "only" directive
50
+ Given the following mailer class with a before_create callback:
51
+ """
52
+ class TestMailer < ::ActionMailer::Base
53
+ before_create :log_args, only: :only_method
54
+
55
+ def self.logger
56
+ @logger ||= Array.new
57
+ end
58
+
59
+ def test(recipient)
60
+ mail(
61
+ to: recipient,
62
+ from: 'sender@test.com',
63
+ subject: 'Test Email'
64
+ )
65
+ end
66
+
67
+ private
68
+
69
+ def log_args(*args)
70
+ self.class.logger << "Test was called with #{args.inspect}"
71
+ end
72
+ end
73
+ """
74
+ When I run the code "TestMailer.test('recipient@test.com').deliver"
75
+ Then an email should have been sent
76
+ And the logger for the class "TestMailer" should be empty
@@ -0,0 +1,25 @@
1
+ Given /^the following mailer class with an? (around_create callback|before_create callback):$/ do |_, class_definition|
2
+ context_module.class_eval class_definition
3
+ end
4
+
5
+ When /^I run the code "(.*?)"$/ do |code|
6
+ self.code_result = context_module.class_eval code
7
+ end
8
+
9
+ Then /^the logger for the class "(.*?)" should contain:$/ do |class_name, log|
10
+ klass = context_module.const_get(class_name)
11
+ klass.logger.should include(log)
12
+ end
13
+
14
+ Then /^an email should have been sent$/ do
15
+ ActionMailer::Base.deliveries.size.should == 1
16
+ end
17
+
18
+ Then /^the logger for the class "(.*?)" should be empty$/ do |class_name|
19
+ klass = context_module.const_get(class_name)
20
+ klass.logger.should == []
21
+ end
22
+
23
+ Then /^an email should have been created$/ do
24
+ code_result.should be_a(Mail::Message)
25
+ end
@@ -0,0 +1,4 @@
1
+ require 'action_mailer'
2
+ require File.expand_path('../../../lib/actionmailer-callbacks', __FILE__)
3
+ ActionMailer::Base.delivery_method = :test
4
+
@@ -0,0 +1,3 @@
1
+ Before do
2
+ ActionMailer::Base.deliveries.clear
3
+ end
@@ -0,0 +1,9 @@
1
+ module WorldExtensions
2
+ attr_accessor :code_result
3
+
4
+ def context_module
5
+ @context_module ||= Module.new
6
+ end
7
+ end
8
+
9
+ World(WorldExtensions)