From 95c0d0a0133da1bb2dfaaae8f7e108bc499b332b Mon Sep 17 00:00:00 2001 From: Jan Sandbrink Date: Mon, 29 Jun 2015 15:54:45 +0200 Subject: [PATCH] pull recipient & author up into MailNotificationJob --- .../deliver_work_package_created_job.rb | 15 ++--------- .../deliver_work_package_updated_job.rb | 15 ++--------- app/workers/mail_notification_job.rb | 27 +++++++++++++++---- 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/app/workers/deliver_work_package_created_job.rb b/app/workers/deliver_work_package_created_job.rb index 486b9c6228..0368cad08f 100644 --- a/app/workers/deliver_work_package_created_job.rb +++ b/app/workers/deliver_work_package_created_job.rb @@ -27,13 +27,10 @@ # See doc/COPYRIGHT.rdoc for more details. #++ -class DeliverWorkPackageCreatedJob - include MailNotificationJob - +class DeliverWorkPackageCreatedJob < MailNotificationJob def initialize(recipient_id, work_package_id, author_id) - @recipient_id = recipient_id + super(recipient_id, author_id) @work_package_id = work_package_id - @author_id = author_id end private @@ -42,15 +39,7 @@ class DeliverWorkPackageCreatedJob @notification_mail ||= UserMailer.work_package_added(recipient, work_package, author) end - def recipient - @recipient ||= Principal.find(@recipient_id) - end - def work_package @work_package ||= WorkPackage.find(@work_package_id) end - - def author - @author ||= Principal.find(@author_id) - end end diff --git a/app/workers/deliver_work_package_updated_job.rb b/app/workers/deliver_work_package_updated_job.rb index df1cda33b2..4bac8b14dd 100644 --- a/app/workers/deliver_work_package_updated_job.rb +++ b/app/workers/deliver_work_package_updated_job.rb @@ -27,13 +27,10 @@ # See doc/COPYRIGHT.rdoc for more details. #++ -class DeliverWorkPackageUpdatedJob - include MailNotificationJob - +class DeliverWorkPackageUpdatedJob < MailNotificationJob def initialize(recipient_id, journal_id, author_id) - @recipient_id = recipient_id + super(recipient_id, author_id) @journal_id = journal_id - @author_id = author_id end private @@ -42,15 +39,7 @@ class DeliverWorkPackageUpdatedJob @notification_mail ||= UserMailer.work_package_updated(recipient, journal, author) end - def recipient - @recipient ||= Principal.find(@recipient_id) - end - def journal @journal ||= Journal.find(@journal_id) end - - def author - @author ||= Principal.find(@author_id) - end end diff --git a/app/workers/mail_notification_job.rb b/app/workers/mail_notification_job.rb index 00a58a23b8..f798a7b763 100644 --- a/app/workers/mail_notification_job.rb +++ b/app/workers/mail_notification_job.rb @@ -29,13 +29,34 @@ ## # Requires including class to implement #notification_mail. -module MailNotificationJob +class MailNotificationJob mattr_accessor :raise_exceptions + def initialize(recipient_id, author_id) + @recipient_id = recipient_id + @author_id = author_id + end + def perform notify end + def error(_job, e) + Rails.logger.error "notification failed (#{self.inspect}): #{e}" + end + + protected + + def recipient + @recipient ||= Principal.find(@recipient_id) + end + + def author + @author ||= Principal.find(@author_id) + end + + private + def notify notification_mail.deliver rescue ActiveRecord::RecordNotFound => e @@ -44,8 +65,4 @@ module MailNotificationJob as required record was not found: #{e}".squish raise e if raise_exceptions end - - def error(_job, e) - Rails.logger.error "notification failed (#{self.inspect}): #{e}" - end end