Class: Chewy::Strategy::Atomic

Inherits:
Base
  • Object
show all
Defined in:
lib/chewy/strategy/atomic.rb

Overview

This strategy accumulates all the objects prepared for indexing and fires index process when strategy is popped from the strategies stack.

Chewy.strategy(:atomic) do User.all.map(&:save) # Does nothing here Post.all.map(&:save) # And here # It imports all the changed users and posts right here # before block leaving with bulk ES API, kinda optimization end

Direct Known Subclasses

ActiveJob, AtomicNoRefresh, Sidekiq

Instance Method Summary collapse

Methods inherited from Base

#name, #update_chewy_indices

Constructor Details

#initializeAtomic

Returns a new instance of Atomic.



15
16
17
# File 'lib/chewy/strategy/atomic.rb', line 15

def initialize
  @stash = {}
end

Instance Method Details

#leaveObject



24
25
26
# File 'lib/chewy/strategy/atomic.rb', line 24

def leave
  @stash.all? { |type, ids| type.import!(ids) }
end

#update(type, objects, _options = {}) ⇒ Object



19
20
21
22
# File 'lib/chewy/strategy/atomic.rb', line 19

def update(type, objects, _options = {})
  @stash[type] ||= []
  @stash[type] |= type.root.id ? Array.wrap(objects) : type.adapter.identify(objects)
end