Sha256: 9a624568c219e62fc720f9f60ad85171b586b6c5ce5fabf6edc59ff56a12eaa6

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module Datamapper4rails
  module RestfulTransactions

    class Rollback < StandardError
    end
    
    module Base
      def self.included(base)
        base.prepend_around_filter(TransactionFilter)
      end
    end
  
    class TransactionFilter
      def self.filter(controller)
        case controller.request.method
        when :post, :put, :delete then
          begin
            DataMapper::Transaction.new(DataMapper.repository(:default)) do |*block_args|
              if block_given? 
                yield (*block_args)
                # added rollback for all actions which just render
                # a page with validation errors and do not redirect to new idem potent 
                # page (http-method get is idem potent within the
                # restful paradigma
                unless controller.response.redirected_to
                  raise Datamapper4rails::RestfulTransactions::Rollback      
                end
              end
            end
          rescue Datamapper4rails::RestfulTransactions::Rollback 
            # ignore, 
            # this is just needed to trigger the rollback on the transaction
          end
        else
          yield if block_given?
        end
      end
    end
  end
end

::ActionController::Base.send(:include, Datamapper4rails::RestfulTransactions::Base)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datamapper4rail-0.1.0 lib/datamapper4rails/restful_transactions.rb