Sha256: 7c69ff05e4b1eb1035877558c1e5b3f206a507becd7deb07a7c74b358a55b569

Contents?: true

Size: 1.01 KB

Versions: 34

Compression:

Stored size: 1.01 KB

Contents

module Workarea
  class IndexCategoryChanges
    include Sidekiq::Worker
    include Sidekiq::CallbacksWorker

    sidekiq_options(
      enqueue_on: { Catalog::Category => :save, with: -> { [changes] } },
      ignore_if: -> { changes['product_ids'].blank? },
      lock: :until_executing
    )

    def perform(changes)
      return unless changes['product_ids'].present?

      ids = require_index_ids(*changes['product_ids'])

      if ids.size > max_count
        ids.each_slice(max_count) do |ids|
          BulkIndexProducts.perform_async(ids)
        end
      else
        BulkIndexProducts.perform(ids)
      end
    end

    def require_index_ids(previous_ids, new_ids)
      previous_ids = Array.wrap(previous_ids)
      new_ids = Array.wrap(new_ids)

      new_ids.reject do |id|
        previous_ids.index(id).present? &&
          previous_ids.index(id) == new_ids.index(id)
      end + (previous_ids - new_ids)
    end

    def max_count
      Workarea.config.category_inline_index_product_max_count
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
workarea-core-3.4.45 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.44 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.43 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.42 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.41 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.40 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.39 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.38 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.37 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.36 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.35 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.34 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.33 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.32 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.31 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.30 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.29 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.28 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.27 app/workers/workarea/index_category_changes.rb
workarea-core-3.4.26 app/workers/workarea/index_category_changes.rb