Module: ActiveRecord::Transactions
- Defined in:
- lib/active_record/transactions.rb
Overview
See ActiveRecord::Transactions::ClassMethods for documentation.
Defined Under Namespace
Modules: ClassMethods Classes: TransactionError
Class Method Summary collapse
Instance Method Summary collapse
-
#destroy_with_transactions ⇒ Object
:nodoc:.
-
#rollback_active_record_state! ⇒ Object
Reset id and @new_record if the transaction rolls back.
-
#save_with_transactions(perform_validation = true) ⇒ Object
:nodoc:.
-
#save_with_transactions! ⇒ Object
:nodoc:.
-
#transaction(&block) ⇒ Object
See ActiveRecord::Transactions::ClassMethods for detailed documentation.
-
#with_transaction_returning_status(method, *args) ⇒ Object
Executes
method
within a transaction and captures its return value as a status flag.
Class Method Details
.included(base) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/active_record/transactions.rb', line 9 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_transactions ⇒ Object
:nodoc:
191 192 193 |
# File 'lib/active_record/transactions.rb', line 191 def destroy_with_transactions #:nodoc: with_transaction_returning_status(:destroy_without_transactions) end |
#rollback_active_record_state! ⇒ Object
Reset id and @new_record if the transaction rolls back.
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/active_record/transactions.rb', line 204 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:
195 196 197 |
# File 'lib/active_record/transactions.rb', line 195 def save_with_transactions(perform_validation = true) #:nodoc: rollback_active_record_state! { with_transaction_returning_status(:save_without_transactions, perform_validation) } end |
#save_with_transactions! ⇒ Object
:nodoc:
199 200 201 |
# File 'lib/active_record/transactions.rb', line 199 def save_with_transactions! #:nodoc: rollback_active_record_state! { self.class.transaction { save_without_transactions! } } end |
#transaction(&block) ⇒ Object
See ActiveRecord::Transactions::ClassMethods for detailed documentation.
187 188 189 |
# File 'lib/active_record/transactions.rb', line 187 def transaction(&block) self.class.transaction(&block) end |
#with_transaction_returning_status(method, *args) ⇒ Object
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.
226 227 228 229 230 231 232 233 |
# File 'lib/active_record/transactions.rb', line 226 def with_transaction_returning_status(method, *args) status = nil self.class.transaction do status = send(method, *args) raise ActiveRecord::Rollback unless status end status end |