Skip to content
Browse files

Use Delegator for a cleaner QueuedMessage class

Credit goes to *Nicolás Sanguinetti* (foca) for this suggestion
  • Loading branch information...
1 parent d1d6c36 commit 5337149caf28b9d8bfe907584511453044890ebd @bcardarella bcardarella committed
Showing with 8 additions and 13 deletions.
  1. +8 −13 actionmailer/lib/action_mailer/async.rb
View
21 actionmailer/lib/action_mailer/async.rb
@@ -1,3 +1,5 @@
+require 'delegate'
+
module ActionMailer::Async
def self.included(base)
base.extend(ClassMethods)
@@ -13,17 +15,19 @@ def method_missing(method_name, *args)
end
end
- class QueuedMessage
- delegate :to_s, :to => :actual_message
-
+ class QueuedMessage < ::Delegator
def initialize(mailer_class, method_name, *args)
@mailer_class = mailer_class
@method_name = method_name
*@args = *args
end
+ def __getobj__
+ @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message
+ end
+
def run
- actual_message.deliver
+ __getobj__.deliver
end
# Will push the message onto the Queue to be processed
@@ -36,14 +40,5 @@ def deliver(force = false)
Rails.queue << self
end
end
-
- # The original ActionMailer message
- def actual_message
- @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message
- end
-
- def method_missing(method_name, *args)
- actual_message.send(method_name, *args)
- end
end
end

0 comments on commit 5337149

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