Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save solisoft/371062 to your computer and use it in GitHub Desktop.
Save solisoft/371062 to your computer and use it in GitHub Desktop.
# 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