abenari / search-demo2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Welcome to the Scoped Search Demo 2

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 Application

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

Installation instruction

$ git clone git://github.com/abenari/search-demo2.git
$ bundle install
$ rake db:migrate
$ rake db:seed

External resources

Blogs:

Requirements

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.

What to note in demo code

javascript

//= requier scoped_search

Style sheets

*= requier scoped_search

Models

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.

Controllers

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

Routes

The following lines was added to the config/routes.rb file

resources :messages do
  get :auto_complete_search, :on => :collection

About


Languages

Language:Ruby 93.3%Language:JavaScript 4.9%Language:CoffeeScript 1.7%