Read more

SearchableTrait is now a gem: Dusen

Henning Koch
November 23, 2012Software engineer at makandra GmbH

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')
Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

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
Posted by Henning Koch to makandra dev (2012-11-23 15:21)