Sha256: 6a5a522c229b36c656101c53427ec7670d45460fbbb61c303ea6b607b5494a6f

Contents?: true

Size: 1.31 KB

Versions: 18

Compression:

Stored size: 1.31 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|
          hash[attrib.unique_name.to_s] = attrib.live_value self
          hash
        }
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 4 rubygems

Version Path
hawkerb-thinking-sphinx-1.3.18 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.17 lib/thinking_sphinx/active_record/attribute_updates.rb
warp-thinking-sphinx-1.3.16 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.16 lib/thinking_sphinx/active_record/attribute_updates.rb
hawkerb-thinking-sphinx-1.3.17 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.15 lib/thinking_sphinx/active_record/attribute_updates.rb
hawkerb-thinking-sphinx-1.3.16 lib/thinking_sphinx/active_record/attribute_updates.rb
hawkerb-thinking-sphinx-1.3.15 lib/thinking_sphinx/active_record/attribute_updates.rb
hawkerb-thinking-sphinx-1.3.14 lib/thinking_sphinx/active_record/attribute_updates.rb
zipme-thinking-sphinx-1.3.14 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.14 lib/thinking_sphinx/active_record/attribute_updates.rb
warp-thinking-sphinx-1.3.13 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.13 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.12 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.11 lib/thinking_sphinx/active_record/attribute_updates.rb
warp-thinking-sphinx-1.3.11 lib/thinking_sphinx/active_record/attribute_updates.rb
warp-thinking-sphinx-1.3.10 lib/thinking_sphinx/active_record/attribute_updates.rb
thinking-sphinx-1.3.10 lib/thinking_sphinx/active_record/attribute_updates.rb