Async ActiveRecord Callbacks

I do a lot of things like this:

class Post < ApplicationRecord
  after_create_commit :sync_with_something!

  private 

  def sync_with_something! 
    SomeWorker.perform_async(self)
  end 
end

And I really wanted to DRY this up. So I thought:

class Post < ApplicationRecord
  async_after_create_commit :sync_with_something! 
end 

And I could write this like:

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true 

  def self.async_after_create_commit(*args) 
    options = args.extract_options!
    method_or_class = args[0] 
    after_create_commit options do
      if method_or_class.is_a?(Class) 
        method_or_class.perform_async(self)
      else
        self.delay.send(method_or_class)
      end
    end 
  end 
end 

I’ve written worse things.