Module: ActiveRecord::Transactions

Defined in:
lib/active_record/transactions.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods Classes: TransactionError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/active_record/transactions.rb', line 8

def self.included(base)
  base.extend(ClassMethods)

  base.class_eval do
    [:destroy, :save, :save!].each do |method|
      alias_method_chain method, :transactions
    end
  end
end

Instance Method Details

#destroy_with_transactionsObject

:nodoc:



101
102
103
# File 'lib/active_record/transactions.rb', line 101

def destroy_with_transactions #:nodoc:
  transaction { destroy_without_transactions }
end

#rollback_active_record_state!Object

Reset id and @new_record if the transaction rolls back.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/active_record/transactions.rb', line 114

def rollback_active_record_state!
  id_present = has_attribute?(self.class.primary_key)
  previous_id = id
  previous_new_record = new_record?
  yield
rescue Exception
  @new_record = previous_new_record
  if id_present
    self.id = previous_id
  else
    @attributes.delete(self.class.primary_key)
    @attributes_cache.delete(self.class.primary_key)
  end
  raise
end

#save_with_transactions(perform_validation = true) ⇒ Object

:nodoc:



105
106
107
# File 'lib/active_record/transactions.rb', line 105

def save_with_transactions(perform_validation = true) #:nodoc:
  rollback_active_record_state! { transaction { save_without_transactions(perform_validation) } }
end

#save_with_transactions!Object

:nodoc:



109
110
111
# File 'lib/active_record/transactions.rb', line 109

def save_with_transactions! #:nodoc:
  rollback_active_record_state! { transaction { save_without_transactions! } }
end

#transaction(&block) ⇒ Object



97
98
99
# File 'lib/active_record/transactions.rb', line 97

def transaction(&block)
  self.class.transaction(&block)
end