Sha256: 98c0bc448369979703e3e682f251ed192c5480c21f29de65d85e128917e85afa

Contents?: true

Size: 790 Bytes

Versions: 23

Compression:

Stored size: 790 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
            # TODO: Support Sequel as well
            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

23 entries across 23 versions & 1 rubygems

Version Path
praxis-2.0.pre.33 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.32 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.31 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.30 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.29 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.28 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.27 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.26 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.25 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.24 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.23 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.22 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.21 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.20 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.19 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.18 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.17 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.16 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.15 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb
praxis-2.0.pre.14 tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb