pcreux/csv-importer

Possible to bypass existing callbacks

Closed this issue · 2 comments

Hi there!

Is it possible to bypass existing callbacks on a model when performing the import?

My use case is I have a few actions I perform in a after_save_commit callback - setting prices, broadcasting updates to channels. It would be useful to skip these when performing the import.

Many thanks for the gem!

Hey @andregoldstein ,

Ideally, you'd want to tell the model to skip ActiveRecord callbacks in the after_build callback. Ex:

   after_build do |product|
      product.skip_callbacks!
  end

It doesn't seem that Active Record offers a way to skip callbacks that would work well with csv-importer, so you'd want to implement your own logic:

Ex:

class Product < ApplicationRecord
  after_save_commit :set_prices, unless: :skip_callbacks?
  after_save_commit :broadcast_updates, unless: :skip_callbacks?

  def skip_callbacks!
    @skip_callbacks = true
  end

  def skip_callbacks?
    @skip_callbacks == true
  end

I hope that helps!

Thanks for your reply, I'll try and implement that!