Skip to content

Instantly share code, notes, and snippets.

@dorkalev
Last active March 5, 2021 09:37
Show Gist options
  • Save dorkalev/276d6fe6b8cdae2045a65c514e1e59b2 to your computer and use it in GitHub Desktop.
Save dorkalev/276d6fe6b8cdae2045a65c514e1e59b2 to your computer and use it in GitHub Desktop.
My last ApplicationJob ever.
# capture every XXXX_just_later call and create a JustLaterJob
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def method_missing(method, *args, &block)
ms = method.to_s
if ms.end_with?('_just_later')
a = ms[0...-11]
if respond_to?(a)
JustLaterJob.perform_later(self, a, *args)
end
else
super(method, *args, &block)
end
end
end
class JustLaterJob < ApplicationJob
queue_as :default
def perform(e, method, *args)
e.send(method, *args)
end
end
# that's how you use it
class Message < ApplicationRecord
belongs_to :user, optional: true
validates :content, presence: true, allow_blank: false
after_commit :notify_just_later
def notify
SMS.send(user.phone, "got a new message: #{content}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment