Skip to content

Instantly share code, notes, and snippets.

@fractaledmind
Created March 1, 2021 09:09
Show Gist options
  • Save fractaledmind/f553b9b47c89ae09ff7d528d817f0abe to your computer and use it in GitHub Desktop.
Save fractaledmind/f553b9b47c89ae09ff7d528d817f0abe to your computer and use it in GitHub Desktop.
This concern allows model instances to run async jobs that depend on them. Calling #async_method_name will trigger the MethodNameJob
module Jobbable
extend ActiveSupport::Concern
# This concern allows model instances to run async jobs that depend on them.
# Calling #async_method_name will trigger the MethodNameJob
PREFIX = "async_"
included do
def method_missing(method_name)
if is_async_method?(method_name)
job_name = extract_event_name(method_name).camelize + 'Job'
job_name.constantize.perform_later(self)
else
super
end
end
def respond_to_missing?(method_name, include_private = false)
is_async_method?(method_name)
end
end
private
def is_async_method?(method_name)
method_name.to_s.start_with?(PREFIX)
end
def extract_event_name(method_name)
method_name.to_s.remove(PREFIX)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment