Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix after_commit_on_* callbacks

  • Loading branch information...
commit 3c562d72e58dfe88a6806093aec69e8cc9e01fc1 1 parent 34832ee
@xaviershay xaviershay authored
View
1  .gitignore
@@ -0,0 +1 @@
+test.sqlite3
View
6 lib/after_commit/active_record.rb
@@ -24,17 +24,17 @@ def after_commit(*callbacks, &block)
def after_commit_on_create(*callbacks, &block)
callbacks << block if block_given?
- write_inheritable_array(:after_commit_on_create_callback, callbacks)
+ write_inheritable_array(:after_commit_on_create, callbacks)
end
def after_commit_on_update(*callbacks, &block)
callbacks << block if block_given?
- write_inheritable_array(:after_commit_on_update_callback, callbacks)
+ write_inheritable_array(:after_commit_on_update, callbacks)
end
def after_commit_on_destroy(*callbacks, &block)
callbacks << block if block_given?
- write_inheritable_array(:after_commit_on_destroy_callback, callbacks)
+ write_inheritable_array(:after_commit_on_destroy, callbacks)
end
end
end
View
51 test/after_commit_test.rb
@@ -1,8 +1,53 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
require 'test/unit'
+require 'rubygems'
+require 'activerecord'
+require 'after_commit'
+require 'after_commit/active_record'
+require 'after_commit/connection_adapters'
+
+ActiveRecord::Base.establish_connection({"adapter" => "sqlite3", "database" => 'test.sqlite3'})
+begin
+ ActiveRecord::Base.connection.execute("drop table mock_records");
+rescue
+end
+ActiveRecord::Base.connection.execute("create table mock_records(id int)");
+
+require File.dirname(__FILE__) + '/../init.rb'
+
+class MockRecord < ActiveRecord::Base
+ attr_accessor :after_commit_on_create_called
+ attr_accessor :after_commit_on_update_called
+ attr_accessor :after_commit_on_destroy_called
+
+ after_commit_on_create :do_create
+ def do_create
+ self.after_commit_on_create_called = true
+ end
+
+ after_commit_on_update :do_update
+ def do_update
+ self.after_commit_on_update_called = true
+ end
+
+ after_commit_on_create :do_destroy
+ def do_destroy
+ self.after_commit_on_destroy_called = true
+ end
+end
class AfterCommitTest < Test::Unit::TestCase
- # Replace this with your real tests.
- def test_this_plugin
- flunk
+ def test_after_commit_on_create_is_called
+ assert_equal true, MockRecord.create!.after_commit_on_create_called
+ end
+
+ def test_after_commit_on_update_is_called
+ record = MockRecord.create!
+ record.save
+ assert_equal true, record.after_commit_on_update_called
+ end
+
+ def test_after_commit_on_destroy_is_called
+ assert_equal true, MockRecord.create!.destroy.after_commit_on_destroy_called
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.