Module: ActiveRecord::Transactions

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
activerecord/lib/active_record/transactions.rb

Overview

See ActiveRecord::Transactions::ClassMethods for documentation.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

ACTIONS =
[:create, :destroy, :update]

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, extended, included

Instance Method Details

#add_to_transactionObject

Add the record to the current transaction so that the after_rollback and after_commit callbacks can be called.



312
313
314
315
316
# File 'activerecord/lib/active_record/transactions.rb', line 312

def add_to_transaction
  if self.class.connection.add_transaction_record(self)
    remember_transaction_record_state
  end
end

#committed!Object

Call the after_commit callbacks.

Ensure that it is not called if the object was never persisted (failed create), but call it after the commit of a destroyed object.



295
296
297
298
299
# File 'activerecord/lib/active_record/transactions.rb', line 295

def committed! #:nodoc:
  run_callbacks :commit if destroyed? || persisted?
ensure
  @_start_transaction_state.clear
end

#destroyObject

:nodoc:



262
263
264
# File 'activerecord/lib/active_record/transactions.rb', line 262

def destroy #:nodoc:
  with_transaction_returning_status { super }
end

#rollback_active_record_state!Object

Reset id and @new_record if the transaction rolls back.



281
282
283
284
285
286
287
288
289
# File 'activerecord/lib/active_record/transactions.rb', line 281

def rollback_active_record_state!
  remember_transaction_record_state
  yield
rescue Exception
  restore_transaction_record_state
  raise
ensure
  clear_transaction_record_state
end

#rolledback!(force_restore_state = false) ⇒ Object

Call the after_rollback callbacks. The force_restore_state argument indicates if the record state should be rolled back to the beginning or just to the last savepoint.



303
304
305
306
307
308
# File 'activerecord/lib/active_record/transactions.rb', line 303

def rolledback!(force_restore_state = false) #:nodoc:
  run_callbacks :rollback
ensure
  restore_transaction_record_state(force_restore_state)
  clear_transaction_record_state
end

#saveObject

:nodoc:



266
267
268
269
270
# File 'activerecord/lib/active_record/transactions.rb', line 266

def save(*) #:nodoc:
  rollback_active_record_state! do
    with_transaction_returning_status { super }
  end
end

#save!Object

:nodoc:



272
273
274
# File 'activerecord/lib/active_record/transactions.rb', line 272

def save!(*) #:nodoc:
  with_transaction_returning_status { super }
end

#touchObject

:nodoc:



276
277
278
# File 'activerecord/lib/active_record/transactions.rb', line 276

def touch(*) #:nodoc:
  with_transaction_returning_status { super }
end

#transaction(options = {}, &block) ⇒ Object

See ActiveRecord::Transactions::ClassMethods for detailed documentation.



258
259
260
# File 'activerecord/lib/active_record/transactions.rb', line 258

def transaction(options = {}, &block)
  self.class.transaction(options, &block)
end

#with_transaction_returning_statusObject

Executes method within a transaction and captures its return value as a status flag. If the status is true the transaction is committed, otherwise a ROLLBACK is issued. In any case the status flag is returned.

This method is available within the context of an ActiveRecord::Base instance.



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'activerecord/lib/active_record/transactions.rb', line 324

def with_transaction_returning_status
  status = nil
  self.class.transaction do
    add_to_transaction
    begin
      status = yield
    rescue ActiveRecord::Rollback
      @_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1
      status = nil
    end

    raise ActiveRecord::Rollback unless status
  end
  status
end