Sha256: 3fe4b270fb368e4688fad8541c1375cb0e832a1026dca5813a658578ac4c5254

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'

module Revertable
  extend ActiveSupport::Concern

  def revert!
    undo_all

    import.reverted!
  rescue StandardError => e
    import.result_message = "Exception: #{e.message}"
    Rails.logger.error "Importo exception: #{e.message} backtrace #{e.backtrace.join(';')}"
    import.failure!
  end

  private

  def undo_all
    revertable_results = import.results.select { |result| result['state'] == 'success' }

    revertable_results.each do |revertable_result|
      next unless revertable_result['state'] == 'success'

      begin
        undo(revertable_result['class'], revertable_result['id'], cells_from_row(revertable_result['row']))
        revertable_result['state'] = 'reverted'
        revertable_result.delete('message')
        revertable_result.delete('errors')
      rescue StandardError => e
        result['message'] = "Not reverted: #{e.message}"
      end
    end
  end

  def undo_row(klass, id, _row)
    klass.constantize.find(id).destroy
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
importo-2.0.5 app/importers/concerns/revertable.rb
importo-2.0.4 app/importers/concerns/revertable.rb