Last active
March 5, 2021 09:37
-
-
Save dorkalev/276d6fe6b8cdae2045a65c514e1e59b2 to your computer and use it in GitHub Desktop.
My last ApplicationJob ever.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JustLaterJob < ApplicationJob | |
queue_as :default | |
def perform(e, method, *args) | |
e.send(method, *args) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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