Skip to content
Browse files

refactor: encapsulate chain/callback logic into separate classes exte…

…nding ActiveSupport::Callbacks
  • Loading branch information...
1 parent e1747db commit fb475678119b63c27124430c4aa296bdaef70a66 @AnthonyCaliendo committed Jan 26, 2009
View
26 README
@@ -10,11 +10,15 @@ If your rails version supports it, you can do:
script/plugin install git://github.com/AnthonyCaliendo/action_mailer_callbacks.git
+== Compatibility
+
+This plugin is currently compatible with Rails 2.1 (untested with 2.2).
+
== How Do I Use It?
-There are 2 main ways to define a callback. In each case, the callback method/block is passed the mail object as the only argument.
+There are 3 ways to define a callback. In each case, the callback method/block is passed the mail object as the only argument.
You may define a callback using a block:
class FooMailer < ActionMailer::Base
@@ -23,6 +27,7 @@ class FooMailer < ActionMailer::Base
end
end
+
You may also define a callback using a symbol/string for a method name:
class FooMailer < ActionMailer::Base
before_deliver :append_advertisement
@@ -32,6 +37,19 @@ class FooMailer < ActionMailer::Base
end
end
+
+You can also pass a Method object directly in:
+class FooMailer < ActionMailer::Base
+ class Handler
+ def self.bar
+ ...
+ end
+ end
+
+ after_deliver Handler.method(:bar)
+end
+
+
Callbacks take options which can be used to define which mail types (i.e. methods) they will be applied to.
These options take the format of *only* and *except*.
- An *only* callback will only be run for methods which match the passed method names.
@@ -77,5 +95,9 @@ Any bug reports or features requests are welcomed.
+=== Contact
+Feel free to email me at:
+action.mailer.callbacks@gmail.com
+
-Copyright (c) 2009 Anthony Caliendo, released under the MIT license
+Copyright (c) 2009 Anthony Caliendo, released under the MIT license
View
1 lib/action_mailer_callbacks.rb
@@ -1,4 +1,5 @@
module ActionMailerCallbacks; end
+require 'action_mailer_callbacks/callbacks'
require 'action_mailer_callbacks/class_methods'
require 'action_mailer_callbacks/instance_methods'
View
46 lib/action_mailer_callbacks/callbacks.rb
@@ -0,0 +1,46 @@
+module ActionMailerCallbacks::Callbacks
+
+ class CallbackChain < ActiveSupport::Callbacks::CallbackChain; end
+
+ class Callback < ActiveSupport::Callbacks::Callback
+ def initialize(callable, options = {})
+ @method, @options = callable, options.symbolize_keys
+
+ normalize_option :only
+ normalize_option :except
+
+ @method = @method.to_sym if @method.is_a?(String) # treat a string like a symbol, so the eval String logic from ActiveSupport::Callbacks::Callback is not used
+ end
+
+ private
+ def should_run_callback?(*arguments)
+ mailer_method_name = arguments.first[:mailer_method_name]
+ return false if options[:only] && !options[:only].include?(mailer_method_name.to_sym)
+ return false if options[:except] && options[:except].include?(mailer_method_name.to_sym)
+ return true
+ end
+
+ def evaluate_method(method, *arguments)
+ target = arguments.first[:target]
+ mail = arguments.first[:mail]
+
+ case method
+ when Symbol
+ target.send(method, mail)
+ when Proc, Method
+ method.call(mail)
+ end
+ end
+
+ def normalize_option(option_type)
+ return unless options[option_type]
+
+ # build a an array of all method names as symbols
+ options[option_type] = [].push(*options[option_type]).inject([]) do |method_names, method_name|
+ method_names << method_name.to_sym unless method_name.blank?
+ method_names
+ end
+ end
+ end
+
+end
View
4 lib/action_mailer_callbacks/class_methods.rb
@@ -29,8 +29,8 @@ def delivery_callback(type, *args, &block)
callable = block || method
raise 'Specify a block or method to invoke in "before_deliver"' unless callable
- callbacks = class_variable_defined?("@@#{type}_deliver_callbacks") ? class_variable_get("@@#{type}_deliver_callbacks") : ActiveSupport::OrderedHash.new
- callbacks[callable] = options.symbolize_keys
+ callbacks = class_variable_defined?("@@#{type}_deliver_callbacks") ? class_variable_get("@@#{type}_deliver_callbacks") : ActionMailerCallbacks::Callbacks::CallbackChain.new
+ callbacks << ActionMailerCallbacks::Callbacks::Callback.new(callable, options)
class_variable_set("@@#{type}_deliver_callbacks", callbacks)
end
View
20 lib/action_mailer_callbacks/instance_methods.rb
@@ -27,23 +27,11 @@ def deliver_with_callbacks!(mail = @mail)
private
def invoke_delivery_callbacks(type, mail)
- callbacks = self.class.class_variable_defined?("@@#{type}_deliver_callbacks") ? self.class.send(:class_variable_get, "@@#{type}_deliver_callbacks") : {}
- method = mail.instance_variable_get '@method_name'
+ callback_chain = self.class.class_variable_defined?("@@#{type}_deliver_callbacks") ? self.class.send(:class_variable_get, "@@#{type}_deliver_callbacks") : ActiveSupport::Callbacks::CallbackChain.new
+ method_name = mail.instance_variable_get '@method_name'
- callbacks.each do |callable, options|
- break if callback_chain_halted
-
- only = [].push(*options[:only]) if options[:only] && method
- next if only && !(only.include?(method.to_s) || only.include?(method.to_sym))
-
- except = [].push(*options[:except]) if options[:except] && method
- next if except && (except.include?(method.to_s) || except.include?(method.to_sym))
-
- if callable.is_a? Proc
- callable.call(mail)
- else
- send(callable, mail)
- end
+ callback_chain.run(:mailer_method_name => method_name, :mail => mail, :target => self) do |result, object|
+ callback_chain_halted
end
return !callback_chain_halted
View
1 test/action_mailer_callbacks_test.rb
@@ -3,6 +3,7 @@
class ActionMailerCallbacksTest < Test::Unit::TestCase
+ # this'd be easier with flexmock... but trying to keep this independent from anything outside of core rails
class TestMailer < ActionMailer::Base
cattr_accessor :befores, :afters
cattr_accessor :before_deliveries, :after_deliveries
View
691 test/callback_test.rb
@@ -0,0 +1,691 @@
+require File.expand_path(File.join(File.dirname(__FILE__), '../../../../test/test_helper'))
+require 'test/unit'
+
+class ActionMailerCallbacks::CallbackTest < Test::Unit::TestCase
+
+ # remove shoulda dependency
+ def assert_contains(collection, item)
+ assert collection.include?(item)
+ end
+
+ def assert_does_not_contain(collection, item)
+ assert !collection.include?(item)
+ end
+
+ # this'd be easier with flexmock... but trying to keep this independent from anything outside of core rails
+ class MockObject
+ attr_accessor :invocations, :mail
+
+ def initialize
+ self.invocations = []
+ end
+
+ def from_string(mail)
+ self.mail = mail
+ invocations << :from_string
+ end
+
+ def from_symbol(mail)
+ self.mail = mail
+ invocations << :from_symbol
+ end
+
+ def from_method(mail)
+ self.mail = mail
+ invocations << :from_method
+ end
+
+ def some_method; end
+ end
+
+ def setup
+ super
+ @mail = TMail::Mail.new
+ end
+
+ def test_should_invoke_proc_in_callback
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc).call(:mailer_method_name => 'some_method', :mail => @mail, :target => MockObject.new)
+
+ assert hit_proc
+ assert_equal @mail, passed_mail
+ end
+
+ def test_should_invoke_method_in_callback
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method)).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_method
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_string_in_callback
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string').call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_string
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_symbol_in_callback
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_symbol
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_proc_in_callback_when_method_is_in_only_option_and_single_option_as_symbol
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, :only => :some_method).call(:mailer_method_name => 'some_method', :mail => @mail, :target => MockObject.new)
+
+ assert hit_proc
+ assert_equal @mail, passed_mail
+ end
+
+ def test_should_invoke_method_in_callback_when_method_is_in_only_option_and_single_option_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), :only => :some_method).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_method
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_string_in_callback_when_method_is_in_only_option_and_single_option_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', :only => :some_method).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_string
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_symbol_in_callback_when_method_is_in_only_option_and_single_option_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, :only => :some_method).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_symbol
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_proc_in_callback_when_method_is_in_only_option_and_single_option_as_string
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, 'only' => 'some_method').call(:mailer_method_name => 'some_method', :mail => @mail, :target => MockObject.new)
+
+ assert hit_proc
+ assert_equal @mail, passed_mail
+ end
+
+ def test_should_invoke_method_in_callback_when_method_is_in_only_option_and_single_option_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), 'only' => 'some_method').call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_method
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_string_in_callback_when_method_is_in_only_option_and_single_option_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', 'only' => 'some_method').call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_string
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_symbol_in_callback_when_method_is_in_only_option_and_single_option_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, 'only' => 'some_method').call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_symbol
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_proc_in_callback_when_method_is_in_only_option_and_multiple_options_as_symbol
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, :only => [:another_method, :some_method]).call(:mailer_method_name => 'some_method', :mail => @mail, :target => MockObject.new)
+
+ assert hit_proc
+ assert_equal @mail, passed_mail
+ end
+
+ def test_should_invoke_method_in_callback_when_method_is_in_only_option_and_multiple_options_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), :only => [:another_method, :some_method]).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_method
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_string_in_callback_when_method_is_in_only_option_and_multiple_options_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', :only => [:another_method, :some_method]).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_string
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_symbol_in_callback_when_method_is_in_only_option_and_multiple_options_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, :only => [:another_method, :some_method]).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_symbol
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_proc_in_callback_when_method_is_in_only_option_and_multiple_options_as_string
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, 'only' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_method', :mail => @mail, :target => MockObject.new)
+
+ assert hit_proc
+ assert_equal @mail, passed_mail
+ end
+
+ def test_should_invoke_method_in_callback_when_method_is_in_only_option_and_multiple_options_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), 'only' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_method
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_string_in_callback_when_method_is_in_only_option_and_multiple_options_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', 'only' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_string
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_symbol_in_callback_when_method_is_in_only_option_and_multiple_options_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, 'only' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_symbol
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_not_invoke_proc_in_callback_when_method_is_not_in_only_option_and_single_option_as_symbol
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, :only => :some_method).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => MockObject.new)
+
+ assert !hit_proc
+ assert_nil passed_mail
+ end
+
+ def test_should_not_invoke_method_in_callback_when_method_is_not_in_only_option_and_single_option_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), :only => :some_method).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_method
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_string_in_callback_when_method_is_not_in_only_option_and_single_option_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', :only => :some_method).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_string
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_symbol_in_callback_when_method_is_not_in_only_option_and_single_option_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, :only => :some_method).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_symbol
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_proc_in_callback_when_method_is_not_in_only_option_and_single_option_as_string
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, 'only' => 'some_method').call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => MockObject.new)
+
+ assert !hit_proc
+ assert_nil passed_mail
+ end
+
+ def test_should_not_invoke_method_in_callback_when_method_is_not_in_only_option_and_single_option_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), 'only' => 'some_method').call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_method
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_string_in_callback_when_method_is_not_in_only_option_and_single_option_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', 'only' => 'some_method').call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_string
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_symbol_in_callback_when_method_is_not_in_only_option_and_single_option_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, 'only' => 'some_method').call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_symbol
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_proc_in_callback_when_method_is_not_in_only_option_and_multiple_options_as_symbol
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, :only => [:another_method, :some_method]).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => MockObject.new)
+
+ assert !hit_proc
+ assert_nil passed_mail
+ end
+
+ def test_should_not_invoke_method_in_callback_when_method_is_not_in_only_option_and_multiple_options_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), :only => [:another_method, :some_method]).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_method
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_string_in_callback_when_method_is_not_in_only_option_and_multiple_options_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', :only => [:another_method, :some_method]).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_string
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_symbol_in_callback_when_method_is_not_in_only_option_and_multiple_options_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, :only => [:another_method, :some_method]).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_symbol
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_proc_in_callback_when_method_is_not_in_only_option_and_multiple_options_as_string
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, 'only' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => MockObject.new)
+
+ assert !hit_proc
+ assert_nil passed_mail
+ end
+
+ def test_should_not_invoke_method_in_callback_when_method_is_not_in_only_option_and_multiple_options_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), 'only' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_method
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_string_in_callback_when_method_is_not_in_only_option_and_multiple_options_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', 'only' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_string
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_symbol_in_callback_when_method_is_not_in_only_option_and_multiple_options_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, 'only' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_symbol
+ assert_nil target.mail
+ end
+
+ def test_should_invoke_proc_in_callback_when_method_is_not_in_except_option_and_single_option_as_symbol
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, :except => :some_method).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => MockObject.new)
+
+ assert hit_proc
+ assert_equal @mail, passed_mail
+ end
+
+ def test_should_invoke_method_in_callback_when_method_is_not_in_except_option_and_single_option_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), :except => :some_method).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_method
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_string_in_callback_when_method_is_not_in_except_option_and_single_option_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', :except => :some_method).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_string
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_symbol_in_callback_when_method_is_not_in_except_option_and_single_option_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, :except => :some_method).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_symbol
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_proc_in_callback_when_method_is_not_in_except_option_and_single_option_as_string
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, 'except' => 'some_method').call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => MockObject.new)
+
+ assert hit_proc
+ assert_equal @mail, passed_mail
+ end
+
+ def test_should_invoke_method_in_callback_when_method_is_not_in_except_option_and_single_option_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), 'except' => 'some_method').call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_method
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_string_in_callback_when_method_is_not_in_except_option_and_single_option_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', 'except' => 'some_method').call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_string
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_symbol_in_callback_when_method_is_not_in_except_option_and_single_option_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, 'except' => 'some_method').call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_symbol
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_proc_in_callback_when_method_is_not_in_except_option_and_multiple_options_as_symbol
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, :except => [:another_method, :some_method]).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => MockObject.new)
+
+ assert hit_proc
+ assert_equal @mail, passed_mail
+ end
+
+ def test_should_invoke_method_in_callback_when_method_is_not_in_except_option_and_multiple_options_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), :except => [:another_method, :some_method]).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_method
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_string_in_callback_when_method_is_not_in_except_option_and_multiple_options_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', :except => [:another_method, :some_method]).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_string
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_symbol_in_callback_when_method_is_not_in_except_option_and_multiple_options_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, :except => [:another_method, :some_method]).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_symbol
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_proc_in_callback_when_method_is_not_in_except_option_and_multiple_options_as_string
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, 'except' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => MockObject.new)
+
+ assert hit_proc
+ assert_equal @mail, passed_mail
+ end
+
+ def test_should_invoke_method_in_callback_when_method_is_not_in_except_option_and_multiple_options_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), 'except' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_method
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_string_in_callback_when_method_is_not_in_except_option_and_multiple_options_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', 'except' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_string
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_invoke_symbol_in_callback_when_method_is_not_in_except_option_and_multiple_options_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, 'except' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_other_method', :mail => @mail, :target => target)
+
+ assert_contains target.invocations, :from_symbol
+ assert_equal @mail, target.mail
+ end
+
+ def test_should_not_invoke_proc_in_callback_when_method_is_in_except_option_and_single_option_as_symbol
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, :except => :some_method).call(:mailer_method_name => 'some_method', :mail => @mail, :target => MockObject.new)
+
+ assert !hit_proc
+ assert_nil passed_mail
+ end
+
+ def test_should_not_invoke_method_in_callback_when_method_is_in_except_option_and_single_option_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), :except => :some_method).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_method
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_string_in_callback_when_method_is_in_except_option_and_single_option_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', :except => :some_method).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_string
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_symbol_in_callback_when_method_is_in_except_option_and_single_option_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, :except => :some_method).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_symbol
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_proc_in_callback_when_method_is_in_except_option_and_single_option_as_string
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, 'except' => 'some_method').call(:mailer_method_name => 'some_method', :mail => @mail, :target => MockObject.new)
+
+ assert !hit_proc
+ assert_nil passed_mail
+ end
+
+ def test_should_not_invoke_method_in_callback_when_method_is_in_except_option_and_single_option_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), 'except' => 'some_method').call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_method
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_string_in_callback_when_method_is_in_except_option_and_single_option_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', 'except' => 'some_method').call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_string
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_symbol_in_callback_when_method_is_in_except_option_and_single_option_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, 'except' => 'some_method').call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_symbol
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_proc_in_callback_when_method_is_in_except_option_and_multiple_options_as_symbol
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, :except => [:another_method, :some_method]).call(:mailer_method_name => 'some_method', :mail => @mail, :target => MockObject.new)
+
+ assert !hit_proc
+ assert_nil passed_mail
+ end
+
+ def test_should_not_invoke_method_in_callback_when_method_is_in_except_option_and_multiple_options_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), :except => [:another_method, :some_method]).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_method
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_string_in_callback_when_method_is_in_except_option_and_multiple_options_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', :except => [:another_method, :some_method]).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_string
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_symbol_in_callback_when_method_is_in_except_option_and_multiple_options_as_symbol
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, :except => [:another_method, :some_method]).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_symbol
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_proc_in_callback_when_method_is_in_except_option_and_multiple_options_as_string
+ hit_proc = false
+ passed_mail = nil
+ proc = lambda {|mail|hit_proc = true, passed_mail = mail}
+
+ ActionMailerCallbacks::Callbacks::Callback.new(proc, 'except' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_method', :mail => @mail, :target => MockObject.new)
+
+ assert !hit_proc
+ assert_nil passed_mail
+ end
+
+ def test_should_not_invoke_method_in_callback_when_method_is_in_except_option_and_multiple_options_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(target.method(:from_method), 'except' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_method
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_string_in_callback_when_method_is_in_except_option_and_multiple_options_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new('from_string', 'except' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_string
+ assert_nil target.mail
+ end
+
+ def test_should_not_invoke_symbol_in_callback_when_method_is_in_except_option_and_multiple_options_as_string
+ target = MockObject.new
+
+ ActionMailerCallbacks::Callbacks::Callback.new(:from_symbol, 'except' => ['another_method', 'some_method']).call(:mailer_method_name => 'some_method', :mail => @mail, :target => target)
+
+ assert_does_not_contain target.invocations, :from_symbol
+ assert_nil target.mail
+ end
+end

0 comments on commit fb47567

Please sign in to comment.
Something went wrong with that request. Please try again.