Created
May 6, 2010 18:03
-
-
Save jqr/392468 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Including this module causes indexing and index removal to occur in | |
# DelayedJobs. Requires all automatic index/removal code to be enabled (the | |
# default) to work properly. Include this module after calling searchable. | |
# | |
# searchable do # TODO: automatically enable after calling searchable? | |
# text :name | |
# end | |
# | |
# include DelayedIndex | |
module DelayedIndex | |
def self.included(receiver) | |
receiver.extend ClassMethods | |
receiver.send :include, InstanceMethods | |
# HACK: Work around some funkiness with remove_from_index, not sure why | |
# it can't be overidden in InstanceMethods. | |
receiver.send :alias_method, :remove_from_index, :solr_remove_from_index | |
end | |
module ClassMethods | |
# Removes an object from the index by id, the object is already destroyed | |
# and it's data is gone. Necessary because the remove command needs to be | |
# sent from a serializable object to work with DelayedJob. | |
def remove_from_index_by_id(id) | |
Sunspot.remove_by_id!(self, id) | |
end | |
end | |
module InstanceMethods | |
# Override the default method and make it backgrounded and auto-committed. | |
def solr_remove_from_index | |
self.class.send_later :remove_from_index_by_id, id | |
end | |
# Override the default method and make it backgrounded and auto-committed. | |
def solr_index | |
send_later :solr_index! | |
end | |
# Improved maybe_mark_for_auto_indexing. | |
def maybe_mark_for_auto_indexing | |
@marked_for_auto_indexing = new_record? || | |
(changed.map(&:to_s) - self.class.sunspot_options[:ignore_attribute_changes_of].to_a.map(&:to_s)).present? | |
true | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment