class ActiverecordReindex::Reindexer

Public Instance Methods

call(record, reflection:, skip_record:) click to toggle source

reindex records associated with given record on given association if association is collection(has_many, has_many_through, has_and_belongs_to_many)

get all associated recrods and reindex them

else

reindex given record associted one
# File lib/activerecord_reindex/reindexer.rb, line 24
def call(record, reflection:, skip_record:)
  if reflection.collection?
    _reindex_collection(reflection, record, skip_record)
  else
    _reindex_single(reflection, record, skip_record)
  end
end
with_strategy(strategy) click to toggle source

chain strategy before actual executing strategy can be either sync or async corresponding to type of reindexing additional strategies can be defined and specified by user

# File lib/activerecord_reindex/reindexer.rb, line 12
def with_strategy(strategy)
  @strategy = strategy
  self
end

Private Instance Methods

_check_strategy() click to toggle source

raise if strategy was not specified or doesn't respond to call which is required for strategy

# File lib/activerecord_reindex/reindexer.rb, line 37
def _check_strategy
  raise ArgumentError, 'No strategy specified.' unless @strategy
  raise ArgumentError, "Strategy specified incorrect. Check if #{@strategy} responds to :call." unless @strategy.respond_to? :call
end
_clear_strategy() click to toggle source

clear strategy to not mess up future reindexing

# File lib/activerecord_reindex/reindexer.rb, line 43
def _clear_strategy
  @strategy = nil
end
_reindex_collection(reflection, record, skip_record) click to toggle source
# File lib/activerecord_reindex/reindexer.rb, line 60
def _reindex_collection(reflection, record, skip_record)
  _check_strategy

  collection = record.public_send(reflection.name)
  collection -= [skip_record] if reflection.klass == skip_record.class

  @strategy.call(record, records: collection)

  _clear_strategy
end
_reindex_single(reflection, record, skip_record) click to toggle source

TODO: got clearing collection bug, write test for it

# File lib/activerecord_reindex/reindexer.rb, line 50
def _reindex_single(reflection, record, skip_record)
  _check_strategy

  associated_record = record.public_send(reflection.name)
  return if associated_record == skip_record
  @strategy.call(record, record: associated_record)

  _clear_strategy
end