public
Description: Easily search you ActiveRecord models with a simple query language using a named scope.
Homepage: http://techblog.floorplanner.com/2008/07/26/easy-search-with-activerecord/
Clone URL: git://github.com/wvanbergen/scoped_search.git
name age message
file .gitignore Wed Sep 03 22:59:07 -0700 2008 Converted to gem [wvanbergen]
file .manifest Sun Sep 21 04:41:51 -0700 2008 Updated manifest/gemspec [wvanbergen]
file CHANGELOG Sun Sep 21 04:38:18 -0700 2008 Project info updated [wvanbergen]
file LICENSE Wed Sep 03 06:09:10 -0700 2008 Relicensed to MIT [wvanbergen]
file README.rdoc Wed Sep 03 23:59:44 -0700 2008 Readme update [wvanbergen]
file Rakefile Wed Sep 03 23:31:58 -0700 2008 Rakefile fix [wvanbergen]
file TODO Sun Sep 21 00:56:16 -0700 2008 Updated TODO and gemspec [weshays]
file init.rb Wed Sep 03 22:59:07 -0700 2008 Converted to gem [wvanbergen]
directory lib/ Sun Sep 21 00:22:25 -0700 2008 Added support for searching date ranges. [weshays]
file scoped_search.gemspec Sun Sep 21 04:44:57 -0700 2008 Set gem version to 0.3.0 [wvanbergen]
directory test/ Sun Sep 21 00:24:15 -0700 2008 Removed duplicate test [weshays]
README.rdoc

scoped_search

This simple plugin will make it easy to search your ActiveRecord models. Searching is performed using a query string, which should be passed to the named_scope search_for that uses SQL %LIKE% conditions for searching. You can specify what fields should be used for searching.

Installation

You can enable scoped_search as a Ruby gem. You must enable the gem in your environment.rb configuration:

  Rails::Initializer.run do |config|
    ...
    config.gem 'wvanbergen-scoped_search', :lib => 'scoped_search', :source => 'http://gems.github.com/'
  end

Make sure the gem is installed by running rake gems:install in you project root. You can also install the gem by running sudo gem install wvanbergen-scoped_search -s gems.github.com

You can use scoped_search as a Rails plugin as well, but this is deprecated. Simply download or clone scoped_search into your +vendor/plugins+ directory of your project.

Usage

First, you have to specify in what columns should be searched:

  class Project < ActiveRecord::Base
    searchable_on :name, :description
  end

Now, the search_for scope is available for queries. You should pass a query string to the scope. This can be empty or nil, in which case all no search conditions are set (and all records will be returned).

  Project.search_for(params[:q]).each { |project| ... }

The search query language is simple. It supports these constructs:

  • words: some search keywords
  • phrases: "a single search phrase"
  • negation: "look for this" -"but do not look for this phrase and this" -word

This functionality is build on named_scope. The searchable_on statement creates a named_scope "search_for". Because of this, you can actually chain the call with other scopes. For example, this can be very useful if you only want to search in projects that are accessible by a given user.

  class Project < ActiveRecord::Base
    searchable_on :name, :description
    named_scope :accessible_by, lambda { |user| ... }
  end

  # using chained named_scopes and will_paginate
  Project.accessible_by(current_user).search_for(params[:q]).paginate(:page => params[:page], :include => :tasks)

Disclaimer

This Rails plugin is written by Willem van Bergen for the Floorplanner.com website. It is released under the MIT license. Please contact me (willem AT vanbergen DOT org if you have any suggestions or remarks.