Sha256: c5ec493d3aef3ec82cabd509c757a21d9643578198adeb2455c4beb19874c05f

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 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
            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
          
          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

2 entries across 2 versions & 1 rubygems

Version Path
skalee-thinking-sphinx-1.3.14.2 lib/thinking_sphinx/active_record/delta.rb
skalee-thinking-sphinx-1.3.14.1 lib/thinking_sphinx/active_record/delta.rb