Skip to content

Commit

Permalink
If fire_delta? exists in the model, let it decide.
Browse files Browse the repository at this point in the history
Overriding should_toggle_delta? or indexed_data_changed? isn't an option, because those methods are loaded only when the index definitions are (and thus would actually override custom versions). This allows us some way to delegate the decision.
  • Loading branch information
pat committed Sep 23, 2012
1 parent 10c4c29 commit c9a01d1
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/thinking_sphinx/active_record/delta.rb
Expand Up @@ -3,7 +3,7 @@ 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
Expand All @@ -18,38 +18,40 @@ class << self
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?
return fire_delta? if respond_to?(:fire_delta?)

self.new_record? || indexed_data_changed?
end

def indexed_data_changed?
sphinx_indexes.any? { |index|
index.fields.any? { |field| field.changed?(self) } ||
Expand Down

2 comments on commit c9a01d1

@jrust
Copy link

@jrust jrust commented on c9a01d1 Oct 22, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 Just what I needed!

@pat
Copy link
Owner Author

@pat pat commented on c9a01d1 Oct 23, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great :) It's certainly a feature I should have implemented a long time ago.

Please sign in to comment.