class Datamapper4rails::RestfulTransactions::TransactionFilter

Public Class Methods

filter(controller) { |*block_args| ... } click to toggle source
# File lib/datamapper4rails/restful_transactions.rb, line 15
def self.filter(controller)
  case controller.request.method
  when :post, :put, :delete then
    begin
      # TODO remove the :default repository and make it more general
      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
          # another page
          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