Class: Dry::Operation::Extensions::ActiveRecord::Builder Private

Inherits:
Module
  • Object
show all
Defined in:
lib/dry/operation/extensions/active_record.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Constructor Details

#initialize(connection, **options) ⇒ Builder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Builder.



93
94
95
96
97
# File 'lib/dry/operation/extensions/active_record.rb', line 93

def initialize(connection, **options)
  super()
  @connection = connection
  @options = options
end

Instance Method Details

#included(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/dry/operation/extensions/active_record.rb', line 99

def included(klass)
  class_exec(@connection, @options) do |default_connection, options|
    # @!method transaction(connection = ActiveRecord::Base, **options, &steps)
    #   Wrap the given steps in an ActiveRecord transaction.
    #
    #   If any of the steps returns a `Dry::Monads::Result::Failure`, the
    #   transaction will be rolled back and `:halt` will be thrown with the
    #   failure as its value.
    #
    #   @param connection [#transaction] The class/object to use
    #   @param options [Hash] Additional options for the ActiveRecord transaction
    #   @yieldreturn [Object] the result of the block
    #   @see Dry::Operation#steps
    #   @see https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#method-i-transaction
    klass.define_method(:transaction) do |connection = default_connection, **opts, &steps|
      intercepting_failure do
        result = nil
        connection.transaction(**options.merge(opts)) do
          intercepting_failure(->(failure) {
                                 result = failure
                                 raise ::ActiveRecord::Rollback
                               }) do
            result = steps.()
          end
        end
        result
      end
    end
  end
end