module ThinkingSphinx::ActiveRecord::AttributeUpdates

Public Class Methods

included(base) click to toggle source
# File lib/thinking_sphinx/active_record/attribute_updates.rb, line 4
def self.included(base)
  base.class_eval do
    after_save :update_attribute_values
  end
end

Private Instance Methods

attribute_values_for_index(index) click to toggle source
# File lib/thinking_sphinx/active_record/attribute_updates.rb, line 35
def attribute_values_for_index(index)
  updatable_attributes(index).inject({}) { |hash, attrib|
    hash[attrib.unique_name.to_s] = attrib.live_value self
    hash
  }
end
updatable_attributes(index) click to toggle source
# File lib/thinking_sphinx/active_record/attribute_updates.rb, line 31
def updatable_attributes(index)
  index.attributes.select { |attrib| attrib.updatable? }
end
update_attribute_values() click to toggle source
# File lib/thinking_sphinx/active_record/attribute_updates.rb, line 12
def update_attribute_values
  return true unless ThinkingSphinx.updates_enabled? &&
    ThinkingSphinx.sphinx_running?

  self.class.sphinx_indexes.each do |index|
    attribute_pairs  = attribute_values_for_index(index)
    attribute_names  = attribute_pairs.keys
    attribute_values = attribute_names.collect { |key|
      attribute_pairs[key]
    }

    update_index index.core_name, attribute_names, attribute_values
    next unless index.delta?
    update_index index.delta_name, attribute_names, attribute_values
  end

  true
end
update_index(index_name, attribute_names, attribute_values) click to toggle source
# File lib/thinking_sphinx/active_record/attribute_updates.rb, line 42
def update_index(index_name, attribute_names, attribute_values)
  config = ThinkingSphinx::Configuration.instance
  config.client.update index_name, attribute_names, {
    sphinx_document_id => attribute_values
  }
rescue Riddle::ConnectionError, Riddle::ResponseError,
  ThinkingSphinx::SphinxError, Errno::ETIMEDOUT
  # Not the end of the world if Sphinx isn't running.
end