Created
July 21, 2010 09:23
-
-
Save Panmind/484259 to your computer and use it in GitHub Desktop.
Thinking Sphinx extension to dynamically update Sphinx attributes
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
# Simple interface to access sphinx dynamic attribute updates feature | |
# from ActiveRecord instances. Spinned off from http://panmind.org/ | |
# (C) 2010 Panmind - Released under the terms of the Ruby license. | |
# | |
module PM | |
module ThinkingSphinxUpdates | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
def sphinx_update(key, value) | |
return unless ThinkingSphinx.updates_enabled? | |
key = key.to_s | |
value = | |
case value | |
when Date then value.to_time.to_i | |
when TrueClass then 1 | |
when FalseClass then 0 | |
else value.to_i | |
end | |
self.class.core_index_names.each do |index_name| | |
self.class.update_in_index index_name, sphinx_document_id, key, value | |
end | |
self.class.delta_index_names.each do |index_name| | |
self.class.update_in_index index_name, sphinx_document_id, key, value | |
end if self.class.delta_indexed_by_sphinx? && toggled_delta? | |
return true | |
rescue NoMethodError | |
false | |
end | |
module ClassMethods | |
def update_in_index(index, document_id, key, value) | |
return unless ThinkingSphinx.sphinx_running? && | |
search_for_id(document_id, index) | |
ThinkingSphinx::Configuration.instance.client.update( | |
index, [key], {document_id => [value]} | |
) | |
end | |
end | |
end | |
end | |
ActiveRecord::Base.__send__ :include, PM::ThinkingSphinxUpdates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment