Created
April 10, 2009 16:48
-
-
Save joshnabbott/93164 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 ActiveRecord | |
| module Searchable | |
| def self.included(base) | |
| base.extend(ClassMethods) | |
| end | |
| module ClassMethods | |
| def searchable_on(*args) | |
| args.flatten! | |
| @args = args.compact | |
| self.instance_eval do | |
| def search(query) | |
| fields = @args.join(',') | |
| query = ActiveRecord::Base.connection.quote(query) | |
| all({ | |
| :conditions => "MATCH (#{fields}) AGAINST (#{query} IN BOOLEAN MODE)", | |
| :order => 'score DESC', | |
| :select => "SQL_CALC_FOUND_ROWS *, MATCH (#{fields}) AGAINST (#{query}) AS score" | |
| }) | |
| end | |
| end | |
| end | |
| end | |
| end | |
| end | |
| ActiveRecord::Base.class_eval { include ActiveRecord::Searchable } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment