Skip to content

Instantly share code, notes, and snippets.

@Kimtaro
Created November 22, 2012 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kimtaro/4129327 to your computer and use it in GitHub Desktop.
Save Kimtaro/4129327 to your computer and use it in GitHub Desktop.
module DelayedCallbacks
def self.included(base)
methods = base.instance_methods(false) + base.private_instance_methods(false)
[:save, :update, :create].each do |time|
if methods.include?("after_#{time}_job".to_sym)
klass = Object.const_get("GenericAfter#{time.capitalize}Worker")
performer = -> { klass.perform_async(self.id, self.class) }
base.send(:define_method, "delay_after_#{time}".to_sym, performer)
base.send("after_#{time}".to_sym, "delay_after_#{time}".to_sym)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment