Sha256: b8691139eccc19ec103bc74f5bf5ebacb97c9cc7fe295d0099e29a27c9b0b179

Contents?: true

Size: 1.41 KB

Versions: 62

Compression:

Stored size: 1.41 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 unless ThinkingSphinx.updates_enabled? && ThinkingSphinx.sphinx_running?
        
        config = ThinkingSphinx::Configuration.instance
        client = Riddle::Client.new config.address, config.port
        
        self.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.name}_core", attribute_names, {
            sphinx_document_id => attribute_values
          } if in_core_index?
        end
      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

62 entries across 62 versions & 13 rubygems

Version Path
DrMark-thinking-sphinx-1.1.14 lib/thinking_sphinx/active_record/attribute_updates.rb
DrMark-thinking-sphinx-1.1.15 lib/thinking_sphinx/active_record/attribute_updates.rb
DrMark-thinking-sphinx-1.2.5 lib/thinking_sphinx/active_record/attribute_updates.rb
DrMark-thinking-sphinx-1.2.6 lib/thinking_sphinx/active_record/attribute_updates.rb
bterlson-thinking-sphinx-1.2.8 lib/thinking_sphinx/active_record/attribute_updates.rb
dpickett-thinking-sphinx-1.1.12 lib/thinking_sphinx/active_record/attribute_updates.rb
dpickett-thinking-sphinx-1.1.23 lib/thinking_sphinx/active_record/attribute_updates.rb
ebeigarts-thinking-sphinx-1.1.21 lib/thinking_sphinx/active_record/attribute_updates.rb
ebeigarts-thinking-sphinx-1.1.22 lib/thinking_sphinx/active_record/attribute_updates.rb
ebeigarts-thinking-sphinx-1.2.10 lib/thinking_sphinx/active_record/attribute_updates.rb
factorylabs-thinking-sphinx-1.2.7 lib/thinking_sphinx/active_record/attribute_updates.rb
freelancing-god-thinking-sphinx-1.1.10 lib/thinking_sphinx/active_record/attribute_updates.rb
freelancing-god-thinking-sphinx-1.1.11 lib/thinking_sphinx/active_record/attribute_updates.rb
freelancing-god-thinking-sphinx-1.1.12 lib/thinking_sphinx/active_record/attribute_updates.rb
freelancing-god-thinking-sphinx-1.1.14 lib/thinking_sphinx/active_record/attribute_updates.rb
freelancing-god-thinking-sphinx-1.1.15 lib/thinking_sphinx/active_record/attribute_updates.rb
freelancing-god-thinking-sphinx-1.1.16 lib/thinking_sphinx/active_record/attribute_updates.rb
freelancing-god-thinking-sphinx-1.1.17 lib/thinking_sphinx/active_record/attribute_updates.rb
freelancing-god-thinking-sphinx-1.1.18 lib/thinking_sphinx/active_record/attribute_updates.rb
freelancing-god-thinking-sphinx-1.1.19 lib/thinking_sphinx/active_record/attribute_updates.rb