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 online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
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)