-
-
Save bravoecho/4232249 to your computer and use it in GitHub Desktop.
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
module Suggestions | |
def self.included( base ) | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
# USAGE: | |
# | |
# class Doctor | |
# # this model should have the typical Sunspot.setup clause somewhere else | |
# | |
# include Suggestions | |
# end | |
# | |
# Doctor.suggestions "John Smith D" | |
#- | |
def suggestions( keywords ) | |
prefix, full_words = prefix_and_full_words_from( keywords ) | |
Sunspot.search(Post) do |query| | |
query.keywords(full_words, :fields => all_text_fields) | |
text_fields do |text_fields_query| | |
text_fields_query.any_of do |any_of_query| | |
all_text_fields.each do |text_field| | |
any_of_query.with(text_field).starting_with(prefix) | |
end | |
end | |
end | |
end | |
end | |
private | |
def all_text_fields | |
Sunspot::Setup.for(self).all_text_fields.collect(&:name) | |
end | |
def prefix_and_full_words_from( keywords ) | |
words = keywords.split(/\s+/) | |
return words.pop, words.join(' ') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment