Class: Workarea::IndexCategoryChanges

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::CallbacksWorker, Sidekiq::Worker
Defined in:
app/workers/workarea/index_category_changes.rb

Instance Method Summary collapse

Instance Method Details

#max_countObject



54
55
56
# File 'app/workers/workarea/index_category_changes.rb', line 54

def max_count
  Workarea.config.category_inline_index_product_max_count
end

#perform(changes, for_release = false) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/workers/workarea/index_category_changes.rb', line 16

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

#require_index_ids(previous_ids, new_ids) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'app/workers/workarea/index_category_changes.rb', line 44

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