Sha256: 90e12c8b706999bc6267f820687e3e525de8d5fcfb9df79e8a15d8fba28382ae

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

module ThinkingSphinx
  module ActiveRecord
    module AttributeUpdates
      def self.included(base)
        base.class_eval do
          after_save :update_attribute_values
        end
      end

      private

      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

      def updatable_attributes(index)
        index.attributes.select { |attrib| attrib.updatable? }
      end

      def attribute_values_for_index(index)
        updatable_attributes(index).inject({}) { |hash, attrib|
          hash[attrib.unique_name.to_s] = attrib.live_value self
          hash
        }
      end

      def update_index(index_name, attribute_names, attribute_values)
        ThinkingSphinx::Connection.take do |client|
          client.update index_name, attribute_names, {
            sphinx_document_id => attribute_values
          }
        end
      rescue Riddle::ConnectionError, Riddle::ResponseError,
        ThinkingSphinx::SphinxError, Errno::ETIMEDOUT
        # Not the end of the world if Sphinx isn't running.
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
friendlyfashion-thinking-sphinx-2.0.14.4 lib/thinking_sphinx/active_record/attribute_updates.rb
friendlyfashion-thinking-sphinx-2.0.14.3 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-2.1.0 lib/thinking_sphinx/active_record/attribute_updates.rb
friendlyfashion-thinking-sphinx-2.0.14.2 lib/thinking_sphinx/active_record/attribute_updates.rb
friendlyfashion-thinking-sphinx-2.0.14.1 lib/thinking_sphinx/active_record/attribute_updates.rb