Module: TrxExt::ObjectExt
- Defined in:
- lib/trx_ext/object_ext.rb
Instance Method Summary collapse
-
#trx ⇒ Object
A shorthand version of
ActiveRecord::Base.transaction. -
#wrap_in_trx(method, class_name = nil) ⇒ Symbol
Wraps specified method in an
ActiveRecordtransaction.
Instance Method Details
#trx ⇒ Object
A shorthand version of ActiveRecord::Base.transaction
42 43 44 45 46 47 48 |
# File 'lib/trx_ext/object_ext.rb', line 42 def trx(...) # If trx method is called over AR model - we should take this into account. Otherwise - call it over # ActiveRecord::Base class. This ensures proper connection picking in sharded environment. return transaction(...) if respond_to?(:transaction) ActiveRecord::Base.transaction(...) end |
#wrap_in_trx(method, class_name = nil) ⇒ Symbol
Wraps specified method in an ActiveRecord transaction.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/trx_ext/object_ext.rb', line 28 def wrap_in_trx(method, class_name = nil) module_to_prepend = Module.new do define_method(method) do |*args, **kwargs, &blk| context = class_name&.constantize || self context.trx do super(*args, **kwargs, &blk) end end end prepend module_to_prepend method end |