diff options
author | Abdelkader Boudih <terminale@gmail.com> | 2014-08-13 21:50:38 +0000 |
---|---|---|
committer | Abdelkader Boudih <terminale@gmail.com> | 2014-08-14 11:05:35 +0000 |
commit | 18303f6e82258c1d6175f1faf1becd260397fb57 (patch) | |
tree | 798d8ab01720f33f7141728d3294980e3372138f /actionmailer/lib/action_mailer | |
parent | b937ea893e5ad22f53a244ac17d085c1d40fdcc2 (diff) | |
download | rails-18303f6e82258c1d6175f1faf1becd260397fb57.tar.gz rails-18303f6e82258c1d6175f1faf1becd260397fb57.tar.bz2 rails-18303f6e82258c1d6175f1faf1becd260397fb57.zip |
Refactor DeliverLater into MessageDelivery
Diffstat (limited to 'actionmailer/lib/action_mailer')
6 files changed, 60 insertions, 72 deletions
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb index df0c945434..3c144c8e5c 100644 --- a/actionmailer/lib/action_mailer/base.rb +++ b/actionmailer/lib/action_mailer/base.rb @@ -548,12 +548,8 @@ module ActionMailer end def method_missing(method_name, *args) # :nodoc: - if respond_to?(method_name) - if defined?(::ActiveJob) && action_methods.include?(method_name.to_s) - DeliverLater::MailMessageWrapper.new(self, method_name, *args) - else - new(method_name, *args).message - end + if action_methods.include?(method_name.to_s) + MessageDelivery.new(self, method_name, *args) else super end diff --git a/actionmailer/lib/action_mailer/delayed_delivery_job.rb b/actionmailer/lib/action_mailer/delayed_delivery_job.rb new file mode 100644 index 0000000000..159198829f --- /dev/null +++ b/actionmailer/lib/action_mailer/delayed_delivery_job.rb @@ -0,0 +1,11 @@ +require 'active_job' + +module ActionMailer + class DelayedDeliveryJob < ActiveJob::Base + queue_as :mailers + + def perform(mailer, mail_method, delivery_method, *args) + mailer.constantize.send(mail_method, *args).send(delivery_method) + end + end +end diff --git a/actionmailer/lib/action_mailer/deliver_later.rb b/actionmailer/lib/action_mailer/deliver_later.rb deleted file mode 100644 index 5609e35d01..0000000000 --- a/actionmailer/lib/action_mailer/deliver_later.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'active_job' - -module ActionMailer - module DeliverLater - extend ActiveSupport::Autoload - autoload :Job - autoload :MailMessageWrapper - end -end
\ No newline at end of file diff --git a/actionmailer/lib/action_mailer/deliver_later/job.rb b/actionmailer/lib/action_mailer/deliver_later/job.rb deleted file mode 100644 index fda3b626b2..0000000000 --- a/actionmailer/lib/action_mailer/deliver_later/job.rb +++ /dev/null @@ -1,11 +0,0 @@ -module ActionMailer - module DeliverLater - class Job < ActiveJob::Base - queue_as :mailers - - def perform(mailer, mail_method, delivery_method, *args) - mailer.constantize.send(mail_method, *args).send(delivery_method) - end - end - end -end diff --git a/actionmailer/lib/action_mailer/deliver_later/mail_message_wrapper.rb b/actionmailer/lib/action_mailer/deliver_later/mail_message_wrapper.rb deleted file mode 100644 index 5c0a5304d4..0000000000 --- a/actionmailer/lib/action_mailer/deliver_later/mail_message_wrapper.rb +++ /dev/null @@ -1,46 +0,0 @@ -module ActionMailer - module DeliverLater - class MailMessageWrapper < Delegator - def initialize(mailer, mail_method, *args) - @mailer = mailer - @mail_method = mail_method - @args = args - __getobj__ - end - - def __getobj__ - @obj ||= @mailer.send(:new, @mail_method, *@args).message - end - - def __setobj__(obj) - @obj = obj - end - - def deliver_later!(options={}) - enqueue_delivery :deliver!, options - end - - def deliver_later(options={}) - enqueue_delivery :deliver, options - end - - def method_missing(m, *args, &block) - __getobj__.__send__(m, *args, &block) - end - - private - def enqueue_delivery(delivery_method, options={}) - args = @mailer.name, @mail_method.to_s, delivery_method.to_s, *@args - enqueue_method = :enqueue - if options[:at] - enqueue_method = :enqueue_at - args.unshift options[:at] - elsif options[:in] - enqueue_method = :enqueue_in - args.unshift options[:in] - end - ActionMailer::DeliverLater::Job.send enqueue_method, *args - end - end - end -end diff --git a/actionmailer/lib/action_mailer/message_delivery.rb b/actionmailer/lib/action_mailer/message_delivery.rb new file mode 100644 index 0000000000..fa0aa43a2d --- /dev/null +++ b/actionmailer/lib/action_mailer/message_delivery.rb @@ -0,0 +1,47 @@ +module ActionMailer + class MessageDelivery < Delegator + def initialize(mailer, mail_method, *args) + @mailer = mailer + @mail_method = mail_method + @args = args + end + + def __getobj__ + @obj ||= @mailer.send(:new, @mail_method, *@args).message + end + + def __setobj__(obj) + @obj = obj + end + + def message #:nodoc: + __getobj__ + end + + def deliver_later!(options={}) + enqueue_delivery :deliver!, options + end + + def deliver_later(options={}) + enqueue_delivery :deliver, options + end + + def method_missing(m, *args, &block) + __getobj__.__send__(m, *args, &block) + end + + private + def enqueue_delivery(delivery_method, options={}) + args = @mailer.name, @mail_method.to_s, delivery_method.to_s, *@args + enqueue_method = :enqueue + if options[:at] + enqueue_method = :enqueue_at + args.unshift options[:at] + elsif options[:in] + enqueue_method = :enqueue_in + args.unshift options[:in] + end + ActionMailer::DelayedDeliveryJob.send enqueue_method, *args + end + end +end |