module ThinkingSphinx::ActiveRecord::Delta

This module contains all the delta-related code for models. There isn't really anything you need to call manually in here - except perhaps ::index_delta, but not sure what reason why.

Public Class Methods

delta_objects() click to toggle source
# File lib/thinking_sphinx/active_record/delta.rb, line 22
def delta_objects
  self.sphinx_indexes.collect(&:delta_object).compact
end
included(base) click to toggle source

Code for after_commit callback is written by Eli Miller: elimiller.blogspot.com/2007/06/proper-cache-expiry-with-aftercommit.html with slight modification from Joost Hietbrink.

# File lib/thinking_sphinx/active_record/delta.rb, line 12
def self.included(base)
  base.class_eval do
    class << self
      # Build the delta index for the related model. This won't be called
      # if running in the test environment.
      #
      def index_delta(instance = nil)
        delta_objects.each { |obj| obj.index(self, instance) }
      end
      
      def delta_objects
        self.sphinx_indexes.collect(&:delta_object).compact
      end
    end
    
    def toggled_delta?
      self.class.delta_objects.any? { |obj| obj.toggled(self) }
    end
    
    private
    
    # Set the delta value for the model to be true.
    def toggle_delta
      self.class.delta_objects.each { |obj|
        obj.toggle(self)
      } if should_toggle_delta?
    end
    
    # Build the delta index for the related model. This won't be called
    # if running in the test environment.
    # 
    def index_delta
      self.class.index_delta(self) if self.class.delta_objects.any? { |obj|
        obj.toggled(self)
      }
    end
    
    def should_toggle_delta?
      self.new_record? || indexed_data_changed?
    end
    
    def indexed_data_changed?
      sphinx_indexes.any? { |index|
        index.fields.any? { |field| field.changed?(self) } ||
        index.attributes.any? { |attrib|
          attrib.public? && attrib.changed?(self) && !attrib.updatable?
        }
      }
    end
  end
end
index_delta(instance = nil) click to toggle source

Build the delta index for the related model. This won't be called if running in the test environment.

# File lib/thinking_sphinx/active_record/delta.rb, line 18
def index_delta(instance = nil)
  delta_objects.each { |obj| obj.index(self, instance) }
end

Public Instance Methods

index_delta() click to toggle source

Build the delta index for the related model. This won't be called if running in the test environment.

# File lib/thinking_sphinx/active_record/delta.rb, line 43
def index_delta
  self.class.index_delta(self) if self.class.delta_objects.any? { |obj|
    obj.toggled(self)
  }
end
indexed_data_changed?() click to toggle source
# File lib/thinking_sphinx/active_record/delta.rb, line 53
def indexed_data_changed?
  sphinx_indexes.any? { |index|
    index.fields.any? { |field| field.changed?(self) } ||
    index.attributes.any? { |attrib|
      attrib.public? && attrib.changed?(self) && !attrib.updatable?
    }
  }
end
should_toggle_delta?() click to toggle source
# File lib/thinking_sphinx/active_record/delta.rb, line 49
def should_toggle_delta?
  self.new_record? || indexed_data_changed?
end
toggle_delta() click to toggle source

Set the delta value for the model to be true.

# File lib/thinking_sphinx/active_record/delta.rb, line 34
def toggle_delta
  self.class.delta_objects.each { |obj|
    obj.toggle(self)
  } if should_toggle_delta?
end
toggled_delta?() click to toggle source
# File lib/thinking_sphinx/active_record/delta.rb, line 27
def toggled_delta?
  self.class.delta_objects.any? { |obj| obj.toggled(self) }
end