Permalink
Browse files

Merge pull request #9006 from wangjohn/activerecord_transaction_state

Created state for a transaction and added tests.
  • Loading branch information...
2 parents 5503796 + bbcebb6 commit a1c0e51a2c99ec88fba59824b59f32f0d34a0aae @tenderlove tenderlove committed Jan 20, 2013
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
+* Added a state instance variable to each transaction. Will allow other objects
+ to know whether a transaction has been committed or rolled back.
+
+ *John Wang*
+
* Collection associations `#empty?` always respects builded records.
Fix #8879.
@@ -5,7 +5,17 @@ class Transaction #:nodoc:
def initialize(connection)
@connection = connection
+ @state = nil
end
+
+ def committed?
+ @state == :commit
+ end
+
+ def rolledback?
+ @state == :rollback
+ end
+
end
class ClosedTransaction < Transaction #:nodoc:
@@ -91,6 +101,7 @@ def add_record(record)
end
def rollback_records
+ @state = :rollback
records.uniq.each do |record|
begin
record.rolledback!(parent.closed?)
@@ -101,6 +112,7 @@ def rollback_records
end
def commit_records
+ @state = :commit
records.uniq.each do |record|
begin
record.committed!
@@ -365,17 +365,18 @@ def init_internals
pk = self.class.primary_key
@attributes[pk] = nil unless @attributes.key?(pk)
- @aggregation_cache = {}
- @association_cache = {}
- @attributes_cache = {}
- @previously_changed = {}
- @changed_attributes = {}
- @readonly = false
- @destroyed = false
- @marked_for_destruction = false
- @new_record = true
- @txn = nil
- @_start_transaction_state = {}
+ @aggregation_cache = {}
+ @association_cache = {}
+ @attributes_cache = {}
+ @previously_changed = {}
+ @changed_attributes = {}
+ @readonly = false
+ @destroyed = false
+ @marked_for_destruction = false
+ @new_record = true
+ @txn = nil
+ @_start_transaction_state = {}
+ @transaction = nil
end
end
end
@@ -451,6 +451,26 @@ def test_sqlite_add_column_in_transaction
end
end
+ def test_transactions_state_from_rollback
+ connection = Topic.connection
+ transaction = ActiveRecord::ConnectionAdapters::ClosedTransaction.new(connection).begin
+
+ assert transaction.open?
+ transaction.perform_rollback
+
+ assert transaction.rolledback?
+ end
+
+ def test_transactions_state_from_commit
+ connection = Topic.connection
+ transaction = ActiveRecord::ConnectionAdapters::ClosedTransaction.new(connection).begin
+
+ assert transaction.open?
+ transaction.perform_commit
+
+ assert transaction.committed?
+ end
+
private
%w(validation save destroy).each do |filter|

0 comments on commit a1c0e51

Please sign in to comment.