Sha256: aaf2c7e32ec4417b2976658193816492b6e2ce37f3e84371e6dda5c5c6f635a9

Contents?: true

Size: 1.96 KB

Versions: 41

Compression:

Stored size: 1.96 KB

Contents

module ThinkingSphinx
  module ActiveRecord
    # 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.
    # 
    module Delta
      # Code for after_commit callback is written by Eli Miller:
      # http://elimiller.blogspot.com/2007/06/proper-cache-expiry-with-aftercommit.html
      # with slight modification from Joost Hietbrink.
      #
      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_object.index(self, instance)
            end
            
            def delta_object
              self.sphinx_indexes.first.delta_object
            end
          end
          
          def toggled_delta?
            self.class.delta_object.toggled(self)
          end
          
          private
          
          # Set the delta value for the model to be true.
          def toggle_delta
            self.class.delta_object.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_object.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
    end
  end
end

Version data entries

41 entries across 41 versions & 9 rubygems

Version Path
thinking-sphinx-1.4.3 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-2.0.2 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-1.4.2 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-1.4.1 lib/thinking_sphinx/active_record/delta.rb
joshcutler-thinking-sphinx-1.3.18 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-2.0.1 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-2.0.0 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-1.4.0 lib/thinking_sphinx/active_record/delta.rb
thorsson_thinking-sphinx-2.0 lib/thinking_sphinx/active_record/delta.rb
angelf-thinking-sphinx-1.3.18 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-1.3.20 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-2.0.0.rc2 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-1.3.19 lib/thinking_sphinx/active_record/delta.rb
thorsson_thinking-sphinx-1.3.18 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-allen-1.3.18.4 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-allen-1.3.18.3 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-allen-1.3.18.2 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-2.0.0.rc1 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-allen-1.3.18.1 lib/thinking_sphinx/active_record/delta.rb
thinking-sphinx-allen-1.3.18 lib/thinking_sphinx/active_record/delta.rb