This software is all about demonstrating the capabilities and usage of the ‘scoped_search’ plugin github.com/wvanbergen/scoped_search. The socped_search is a Rails plugin that let a user easily search ActiveRecord models with a simple query language using a named scope. The scoped search can be used by the programmer as well as the end user. It includes a syntax auto completer to get the end users familiar with the query syntax.
The demo application has one main pages “messages”, the page includes a search box at the to try it out. A running version of the demo can be found here: demo2-scopedsearch.rhcloud.com/messages
$ git clone git://github.com/abenari/search-demo2.git $ bundle install $ rake db:migrate $ rake db:seed
Blogs:
-
abenari’s blog: scopedsearch.wordpress.com
-
wvanbergen’s blog posts: techblog.floorplanner.com/tag/scoped_search
Scoped search runs on both Rails 2 and 3. However this demo shows how to use the plugin with assets pipline, this demo uses Rails 3.2.
//= requier scoped_search
*= requier scoped_search
The search language definition is in the “message” mode
scoped_search :on => :subject, :complete_value => :true scoped_search :on => :body, :rename => 'message', :complete_value => :true scoped_search :on => :sender, :rename => 'from',:complete_value => :true scoped_search :on => :recipient, :rename=> 'to', :complete_value => :true scoped_search :on => :read, :rename=> 'read', :complete_value => {:yes => true, :no => false} scoped_search :on => :read, :rename=> 'unread', :complete_value => {:yes => false, :no => true}
The :on
mark the column name in the database, :in
specify relation, :only_explicit
exclude a search term from the free text search. The :complete_value
make the auto completer suggest values to the user, :rename
will rename the search term.
The following two methods are used for showing the filtered list of messages and auto-complete the search syntax.
def index @messages = Message.search_for(params[:search], :order => params[:order]) respond_to do |format| format.html # index.html.erb format.json { render :json => @messages } end end def auto_complete_search begin @items = Message.complete_for(params[:search]) rescue ScopedSearch::QueryNotSupported => e @items = [{:error =>e.to_s}] end render :json => @items end
The following lines was added to the config/routes.rb file
resources :messages do get :auto_complete_search, :on => :collection