Sha256: f932f2819eef860c61bc65b62c5cb960e9152898d155f2242aa4bd83b00bdcb9

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

module ThinkingSphinx
  module ActiveRecord
    module AttributeUpdates
      def self.included(base)
        base.class_eval do
          after_commit :update_attribute_values
        end
      end
      
      private
            
      def update_attribute_values
        return true unless ThinkingSphinx.updates_enabled? &&
          ThinkingSphinx.sphinx_running?
        
        config = ThinkingSphinx::Configuration.instance
        client = config.client
        
        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]
          }
          
          client.update "#{index.core_name}", attribute_names, {
            sphinx_document_id => attribute_values
          } if self.class.search_for_id(sphinx_document_id, index.core_name)
        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|
          if attrib.type == :datetime
            hash[attrib.unique_name.to_s] = attrib.live_value(self).to_time.to_i
          else
            hash[attrib.unique_name.to_s] = attrib.live_value self
          end
          
          hash
        }
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
thinking-sphinx-1.3.8 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.7 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.6 lib/thinking_sphinx/active_record/attribute_updates.rb