Skip to content

Instantly share code, notes, and snippets.

@lazaronixon
Last active April 3, 2023 18:33
Show Gist options
  • Save lazaronixon/fbde4ef1a9eebf995d7fae29b02b2599 to your computer and use it in GitHub Desktop.
Save lazaronixon/fbde4ef1a9eebf995d7fae29b02b2599 to your computer and use it in GitHub Desktop.
Action deliver
class Deliveries::AppointmentCancelationDelivery < Deliveries::Delivery
protected
def notification
AppointmentNotifier.canceled(recipient)
end
def mail
AppointmentMailer.canceled(deliverable, recipient)
end
end
class Deliveries::Delivery
extend Suppressible
def initialize(deliverable, recipient)
@deliverable, @recipient = deliverable, recipient
end
def self.deliver(deliverable, recipient)
new(deliverable, recipient).deliver
end
def deliver
return if suppressed?
notification.try :notify_later
mail.try :deliver_later
end
private
attr_reader :deliverable, :recipient
def suppressed?
Deliveries::Delivery.suppressed?
end
def notification
# Must be implemented in concrete subclass
end
def mail
# Must be implemented in concrete subclass
end
end
gem "abstract_notifier"
module Suppressible
def self.extended(base)
base.thread_mattr_accessor :suppressed, instance_accessor: false
base.delegate :suppressed?, to: "self.class"
end
def suppress(&block)
original, self.suppressed = self.suppressed, true
yield
ensure
self.suppressed = original
end
def suppressed?
suppressed
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment