Sha256: d9ea067a038a9f0c14f5cd380830468b277671c6ac43d43759c0621681b51ae1

Contents?: true

Size: 1.52 KB

Versions: 15

Compression:

Stored size: 1.52 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?
        
        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)
        config = ThinkingSphinx::Configuration.instance
        config.client.update index_name, attribute_names, {
          sphinx_document_id => attribute_values
        } if self.class.search_for_id(sphinx_document_id, index_name)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 6 rubygems

Version Path
thinking-sphinx-1.4.1 lib/thinking_sphinx/active_record/attribute_updates.rb
joshcutler-thinking-sphinx-1.3.18 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.4.0 lib/thinking_sphinx/active_record/attribute_updates.rb
thorsson_thinking-sphinx-2.0 lib/thinking_sphinx/active_record/attribute_updates.rb
angelf-thinking-sphinx-1.3.18 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.20 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.19 lib/thinking_sphinx/active_record/attribute_updates.rb
thorsson_thinking-sphinx-1.3.18 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-allen-1.3.18.4 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-allen-1.3.18.3 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-allen-1.3.18.2 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-allen-1.3.18.1 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-allen-1.3.18 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.18 lib/thinking_sphinx/active_record/attribute_updates.rb
josh_cutler-thinking-sphinx-1.3.17 lib/thinking_sphinx/active_record/attribute_updates.rb