Sha256: eec60afcef003acab423e072a776830386bd01dfc2ceb350cbb9b70ae6ffb379

Contents?: true

Size: 1.25 KB

Versions: 17

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

class ThinkingSphinx::RealTime::Callbacks::RealTimeCallbacks
  def initialize(reference, path = [], &block)
    @reference, @path, @block = reference, path, block
  end

  def after_commit(instance)
    persist_changes instance
  end

  def after_save(instance)
    persist_changes instance
  end

  private

  attr_reader :reference, :path, :block

  def callbacks_enabled?
    setting = configuration.settings['real_time_callbacks']
    setting.nil? || setting
  end

  def configuration
    ThinkingSphinx::Configuration.instance
  end

  def indices
    configuration.indices_for_references reference
  end

  def objects_for(instance)
    if block
      results = block.call instance
    else
      results = path.inject(instance) { |object, method| object.send method }
    end

    Array results
  end

  def persist_changes(instance)
    return unless real_time_indices? && callbacks_enabled?

    real_time_indices.each do |index|
      objects_for(instance).each do |object|
        ThinkingSphinx::RealTime::Transcriber.new(index).copy object
      end
    end
  end

  def real_time_indices?
    real_time_indices.any?
  end

  def real_time_indices
    indices.select { |index| index.is_a? ThinkingSphinx::RealTime::Index }
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
thinking-sphinx-5.6.0 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-5.5.1 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-5.5.0 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-5.4.0 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-5.3.0 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-5.2.1 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-5.2.0 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-5.1.0 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-5.0.0 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-4.4.1 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-4.4.0 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-4.3.2 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-4.3.1 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-4.3.0 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-4.2.0 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-4.1.0 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb
thinking-sphinx-4.0.0 lib/thinking_sphinx/real_time/callbacks/real_time_callbacks.rb