Sha256: 1fe111f6a63f05961b12456fd4943b43b15d06f026d5fde402602a704e182bd7

Contents?: true

Size: 747 Bytes

Versions: 8

Compression:

Stored size: 747 Bytes

Contents

module V1
  module Concerns
    module ControllerBase
      extend ActiveSupport::Concern
      # Controller concen that wraps an API with a transaction, and automatically rolls it back
      # for non-2xx (or 3xx) responses
      included do
        around :action do |controller, callee|
          begin
            ActiveRecord::Base.transaction do
              callee.call
              res = controller.response
              # Force a rollback for non 2xx or 3xx responses
              raise ActiveRecord::Rollback unless res.status >= 200 && res.status < 400
            end
          rescue ActiveRecord::Rollback
            # No need to do anything, let the responses flow normally
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
praxis-2.0.0 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.40 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.39 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.38 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.37 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.36 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.35 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.34 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb