Created
April 19, 2010 13:52
-
-
Save solisoft/371062 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
# Extend MongoMapper Objects | |
# Usage : | |
# ------- | |
# MyModel.search(["title", "text"], "my search", { :flag => nil }, { :limit => 10 }) | |
module MongoMapper | |
module Document | |
module ClassMethods | |
def search(fields, words, conditions = {}, options = {}) | |
filters = [] | |
words.split(" ").each do |word| | |
ftemp = [] | |
fields.each {|field| ftemp << "(this.#{field} === null ? false : this.#{field}.match(new RegExp('#{word}','i')))" } | |
filters << "(#{ftemp * " || "})" | |
end | |
filters = filters * " && " | |
all({:conditions => { "$where" => filters }.merge(conditions) }.merge(options)) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment