SearchableTrait is now a gem: Dusen

Updated . Posted . Visible to the public.

For two years we've been using SearchableTrait which gives models the ability to process Googlesque queries like this:

Contact.search('a mix of words "and phrases" and qualified:fields')

This trait used to be a huge blob of code without tests and documentation, so I made a gem out of it. Check out https://github.com/makandra/dusen Show archive.org snapshot for code, tests, and a huge README.

You should use the Dusen gem and delete SearchableTrait in all future projects.

Note that the syntax to define query processors has changed lightly:

class Contact < ActiveRecord::Base

  search_syntax do

    search_by :text do |scope, phrase|
      columns = [:name, :street, :city, :email]
      scope.where_like(columns => phrase)
    end

  end
  
   search_by :email do |scope, email|
     scope.where(:email => email)
   end  

end

Note the changes:

  • Query processors are defined within a search_syntax block
  • Processor blocks now take the current scope chain as the first argument (the current chain used to be self)
  • A new conveniance method where_like that is useful for text queries
Henning Koch
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2012-11-23 14:21)