Skip to content

Instantly share code, notes, and snippets.

@poshboytl
Forked from maccman/after_commit.rb
Created August 12, 2010 09:52
Show Gist options
  • Save poshboytl/520653 to your computer and use it in GitHub Desktop.
Save poshboytl/520653 to your computer and use it in GitHub Desktop.
module AfterCommit
def self.included(base)
base.class_eval do
[:save, :save!].each do |method|
alias_method_chain method, :after_commit
end
end
base.define_callbacks :after_commit, :after_commit_on_create
end
def save_with_after_commit(*args)
previous_new_record = new_record?
if result = save_without_after_commit(*args)
callback(:after_commit)
callback(:after_commit_on_create) if previous_new_record
end
result
end
def save_with_after_commit!(*args)
previous_new_record = new_record?
if result = save_without_after_commit!(*args)
callback(:after_commit)
callback(:after_commit_on_create) if previous_new_record
end
result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment