Created
January 19, 2011 18:35
-
-
Save simonwistow/786609 to your computer and use it in GitHub Desktop.
Testing
This file contains hidden or 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
extend ThinkingSphinx::ActiveRecord::ClassMethods | |
class << self | |
attr_accessor :sphinx_index_blocks | |
def set_sphinx_primary_key(attribute) | |
@sphinx_primary_key_attribute = attribute | |
end | |
def primary_key_for_sphinx | |
@sphinx_primary_key_attribute || primary_key | |
end | |
def sphinx_index_options | |
sphinx_indexes.last.options | |
end | |
# Generate a unique CRC value for the model's name, to use to | |
# determine which Sphinx documents belong to which AR records. | |
# | |
# Really only written for internal use - but hey, if it's useful to | |
# you in some other way, awesome. | |
# | |
def to_crc32 | |
self.name.to_crc32 | |
end | |
def to_crc32s | |
(subclasses << self).collect { |klass| klass.to_crc32 } | |
end | |
def sphinx_database_adapter | |
@sphinx_database_adapter ||= | |
ThinkingSphinx::AbstractAdapter.detect(self) | |
end | |
def sphinx_name | |
self.name.underscore.tr(':/\\', '_') | |
end | |
# | |
# The above method to_crc32s is dependant on the subclasses being loaded consistently | |
# After a reset_subclasses is called (during a Dispatcher.cleanup_application in development) | |
# Our subclasses will be lost but our context will not reload them for us. | |
# | |
# We reset the context which causes the subclasses to be reloaded next time the context is called. | |
# | |
def reset_subclasses_with_thinking_sphinx | |
reset_subclasses_without_thinking_sphinx | |
ThinkingSphinx.reset_context! | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment