class ActiveRecord::Base

Public Class Methods

inherited(child) click to toggle source
Calls superclass method
# File lib/activerecord_reindex/base.rb, line 13
def self.inherited(child)
  super
  class << child

    attr_accessor :reindexer, :async_adapter, :sync_adapter, :sync_reindexable_reflections,
                  :async_reindexable_reflections, :reindex_attr_blacklist, :reindex_attr_whitelist

  end

  # Init default values to prevent undefined method for nilClass error
  child.sync_reindexable_reflections = []
  child.async_reindexable_reflections = []

  child.reindexer = ActiverecordReindex::Reindexer.new
  # TODO: provide config for changing adapters
  # For now can set adapter through writers inside class
  if ActiverecordReindex.config.async_reindex_only_in_production? && !Rails.env.production?
    child.async_adapter = ActiverecordReindex::SyncAdapter
  else
    child.async_adapter = ActiverecordReindex::AsyncAdapter
  end

  child.sync_adapter = ActiverecordReindex::SyncAdapter
end

Public Instance Methods

reindex_async(reflection, skip_record: nil) click to toggle source
# File lib/activerecord_reindex/base.rb, line 38
def reindex_async(reflection, skip_record: nil)
  _reindex(reflection, strategy: self.class.async_adapter, skip_record: skip_record)
end
reindex_sync(reflection, skip_record: nil) click to toggle source
# File lib/activerecord_reindex/base.rb, line 42
def reindex_sync(reflection, skip_record: nil)
  _reindex(reflection, strategy: self.class.sync_adapter, skip_record: skip_record)
end

Private Instance Methods

_reindex(reflection, strategy:, skip_record:) click to toggle source
# File lib/activerecord_reindex/base.rb, line 48
def _reindex(reflection, strategy:, skip_record:)
  self.class.reindexer
      .with_strategy(strategy)
      .call(self, reflection: reflection, skip_record: skip_record)
end
changed_index_relevant_attributes?() click to toggle source
# File lib/activerecord_reindex/base.rb, line 54
def changed_index_relevant_attributes?
  return true unless self.class.reindex_attr_blacklist || self.class.reindex_attr_whitelist
  changed = changed_attributes.keys
  wl = self.class.reindex_attr_whitelist&.map(&:to_sym)
  bl = self.class.reindex_attr_blacklist&.map(&:to_sym)

  if wl
    whitelisted = wl & changed
  else
    whitelisted = changed
  end

  if bl
    blacklisted = changed - bl
  else
    blacklisted = []
  end

  !(whitelisted - blacklisted).empty?
end