Permalink
Browse files

convert to rails plugin structure (remove old files)

  • Loading branch information...
1 parent 5650248 commit bf16d31683180e2304da654fd00a0ff89c59d3dd @AnthonyCaliendo committed Jan 21, 2009
Showing with 0 additions and 261 deletions.
  1. +0 −79 action_mailer_callbacks.rb
  2. +0 −182 action_mailer_callbacks_test.rb
@@ -1,79 +0,0 @@
-class ActionMailer::Base
- cattr_accessor :callback_chain_halted
-
- def self.before_deliver(*args, &block)
- delivery_callback :before, *args, &block
- end
-
- def self.after_deliver(*args, &block)
- delivery_callback :after, *args, &block
- end
-
- def self.delivery_callback(type, *args, &block)
- args = args.dup
-
- method = args.first
- method = nil if method.is_a?(Hash)
-
- options = args.pop
- options = {} unless options.is_a?(Hash)
-
- 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
- class_variable_set("@@#{type}_deliver_callbacks", callbacks)
- end
-
- private
- def create_mail_with_method_stored
- mail = create_mail_without_method_stored
- mail.instance_variable_set '@method_name', @method_name
- mail
- end
- alias_method_chain :create_mail, :method_stored
-
- def create_with_method_stored!(method_name, *parameters)
- @method_name = method_name
- create_without_method_stored!(method_name, *parameters)
- end
- alias_method_chain :create!, :method_stored
-
- def deliver_with_callbacks!(mail = @mail)
- self.class.callback_chain_halted = false
- if invoke_delivery_callbacks(:before, mail)
- deliver_without_callbacks!
- invoke_delivery_callbacks(:after, mail)
- end
- end
- alias_method_chain :deliver!, :callbacks
-
- 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'
-
- 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
- end
-
- return !callback_chain_halted
- end
-
- def self.halt_callback_chain
- self.callback_chain_halted = true
- end
-
-end
@@ -1,182 +0,0 @@
-require File.dirname(__FILE__) + '/../test_helper'
-
-class ActionMailerCallbacksTest < Test::Unit::TestCase
- class TestMailer < ActionMailer::Base
- cattr_accessor :befores, :afters
- cattr_accessor :before_deliveries, :after_deliveries
-
- def always; end #no-op
- before_deliver do |mail|
- TestMailer.befores << :before_block
- end
- before_deliver 'before_from_string'
- def before_from_string(mail)
- TestMailer.befores << :before_string
- end
- before_deliver :before_from_symbol
- def before_from_symbol(mail)
- TestMailer.befores << :before_symbol
- end
- after_deliver do |mail|
- TestMailer.afters << :after_block
- end
- after_deliver 'after_from_string'
- def after_from_string(mail)
- TestMailer.afters << :after_string
- end
- after_deliver :after_from_symbol
- def after_from_symbol(mail)
- TestMailer.afters << :after_symbol
- end
-
- def in_except; end # no-op
- def not_in_except; end #no-op
- before_deliver :except => :in_except do |mail|
- TestMailer.befores << :before_except
- end
- after_deliver :except => :in_except do |mail|
- TestMailer.afters << :after_except
- end
-
- def in_only; end #no-op
- def not_in_only; end #no-op
- before_deliver :only => :in_only do |mail|
- TestMailer.befores << :before_only
- end
- after_deliver :only => :in_only do |mail|
- TestMailer.afters << :after_only
- end
-
- def halts; end
- before_deliver :only => :halts do |mail|
- TestMailer.befores << :halts_before
- halt_callback_chain
- end
-
- after_deliver :only => :halts do |mail|
- TestMailer.afters << :halts_after
- raise 'this should never happen if the chain is halted...'
- end
-
- # we can test this by checking the deliveries in the before and after callbacks
- def ensure_before_and_after; end
- before_deliver :only => :ensure_before_and_after do |mail|
- TestMailer.befores << :ensure_before
- TestMailer.before_deliveries = ActionMailer::Base.deliveries.dup
- end
- after_deliver :only => :ensure_before_and_after do |mail|
- TestMailer.afters << :ensure_before
- TestMailer.after_deliveries = ActionMailer::Base.deliveries.dup
- end
-
- def template_path
- "#{RAILS_ROOT}/test/fixtures/test_mailer"
- end
-
- def render(opts)
- # no-op
- end
- end
-
- setup do
- TestMailer.befores = []
- TestMailer.afters = []
- TestMailer.before_deliveries = []
- TestMailer.after_deliveries = []
- ActionMailer::Base.deliveries = []
- end
-
- def test_invoke_before_with_block
- TestMailer.deliver_always
- assert TestMailer.befores.include?(:before_block)
- end
-
- def test_invoke_before_with_symbol
- TestMailer.deliver_always
- assert TestMailer.befores.include?(:before_symbol)
- end
-
- def test_invoke_before_with_string
- TestMailer.deliver_always
- assert TestMailer.befores.include?(:before_string)
- end
-
- def test_invoke_before_when_in_only_option
- TestMailer.deliver_in_only
- assert TestMailer.befores.include?(:before_only)
- end
-
- def test_invoke_before_when_not_in_except_option
- TestMailer.deliver_not_in_except
- assert TestMailer.befores.include?(:before_except)
- end
-
- def test_not_invoke_before_when_not_in_only_option
- TestMailer.deliver_not_in_only
- assert !TestMailer.befores.include?(:before_only)
- end
-
- def not_invoke_before_when_in_except_option
- TestMailer.deliver_in_except
- assert !TestMailer.befores.include?(:before_except)
- end
-
- def test_invoke_after_with_block
- TestMailer.deliver_always
- assert TestMailer.afters.include?(:after_block)
- end
-
- def test_invoke_after_with_symbol
- TestMailer.deliver_always
- assert TestMailer.afters.include?(:after_symbol)
- end
-
- def test_invoke_after_with_string
- TestMailer.deliver_always
- assert TestMailer.afters.include?(:after_string)
- end
-
- def test_invoke_after_when_in_only_option
- TestMailer.deliver_in_only
- assert TestMailer.afters.include?(:after_only)
- end
-
- def test_invoke_after_when_not_in_except_option
- TestMailer.deliver_not_in_except
- assert TestMailer.afters.include?(:after_except)
- end
-
- def test_not_invoke_after_when_not_in_only_option
- TestMailer.deliver_not_in_only
- assert !TestMailer.afters.include?(:after_only)
- end
-
- def test_not_invoke_after_when_in_except_option
- TestMailer.deliver_in_except
- assert !TestMailer.afters.include?(:after_except)
- end
-
- def test_should_halt_chain_when_callback_halts
- TestMailer.deliver_halts
- assert TestMailer.befores.include?(:halts_before)
- assert !TestMailer.afters.include?(:halts_after)
- end
-
- def test_should_not_halt_chain_when_previous_chain_halted
- TestMailer.deliver_halts
- assert !TestMailer.afters.include?(:after_block)
-
- TestMailer.deliver_always
- assert TestMailer.afters.include?(:after_block)
- end
-
- def test_ensure_before_filter_called_before_method
- TestMailer.deliver_ensure_before_and_after
- assert TestMailer.before_deliveries.empty?
- end
-
- def test_ensure_after_filter_called_after_method
- TestMailer.deliver_ensure_before_and_after
- assert !TestMailer.after_deliveries.empty?
- end
-end

0 comments on commit bf16d31

Please sign in to comment.