Sha256: ed36b2e8e0a36b480799479810af3d956f6a80966fb08610b7ccd54bc3ce84fa

Contents?: true

Size: 1.73 KB

Versions: 14

Compression:

Stored size: 1.73 KB

Contents

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

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

    def perform(changes, for_release = false)
      return unless changes['product_ids'].present?

      ids = if for_release
        # This is a shortcut because if you're resorting products within a release,
        # the `changes` hash doesn't reflect the repositioning within the release,
        # only the difference between what's live and what's in the release.
        #
        # Reindexing all of them is a shortcut to having to manually build a diff
        # between the changesets in the possible affected releases.
        changes['product_ids'].flatten.uniq
      else
        require_index_ids(*changes['product_ids'])
      end

      if ids.size > max_count
        ids.each { |id| IndexProduct.perform_async(id) }
      else
        Catalog::Product.in(id: ids).each do |product|
          begin
            IndexProduct.perform(product)
          rescue
            IndexProduct.perform_async(product.id)
          end
        end
      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

14 entries across 14 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.26 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.25 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.23 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.22 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.21 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.20 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.19 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.18 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.17 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.16 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.15 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.14 app/workers/workarea/index_category_changes.rb
workarea-core-3.5.13 app/workers/workarea/index_category_changes.rb