Sha256: a88145f753688d712f428d68f61ec801e133330806d3adf236a0a3ae6a8d4820

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require "delayed_after_commit/version"

module DelayedAfterCommit
  extend ActiveSupport::Concern

  class_methods do
    def enqueue_delayed_method(method, id)
      # this is used as a generic method that is created to run asyncronously from within sidekiq
      # it finds the object, and runs the deferred method
      if obj = self.find_by_id(id)
        obj.send(method)
      end
    end

    def delayed_after_create(*args, &block)
      args[1] ||= {}
      args[1][:on] = :create
      delayed_after_commit(*args, &block)
    end

    def delayed_after_update(*args, &block)
      args[1] ||= {}
      args[1][:on] = :update
      delayed_after_commit(*args, &block)
    end

    protected
    def delayed_after_commit(*args, &block)
      # this creates a method that runs `enqueue_delayed_method`
      # it then adds that method to the after_commit callback
      opts = args.extract_options!
      method = args.first
      delayed_method_name = "delayed_after_#{opts[:on]}_#{method}"
      define_method(delayed_method_name) do |m = method|
        self.class.delay.enqueue_delayed_method(m, self.id)
      end
      self.after_commit(delayed_method_name.to_sym, opts, &block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
delayed_after_commit-0.1.6 lib/delayed_after_commit.rb