github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

wvanbergen / scoped_search

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 41
    • 5
  • Source
  • Commits
  • Network (5)
  • Issues (0)
  • Wiki (7)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (2)
    • development
    • master ✓
  • Tags (14)
    • scoped_search-2.0.1
    • scoped_search-2.0.0
    • scoped_search-1.2.0
    • scoped_search-1.1.0
    • scoped_search-1.0.1
    • scoped_search-1.0.0
    • scoped_search-0.7.3
    • scoped_search-0.7.2
    • scoped_search-0.7.0
    • scoped_search-0.6.0
    • scoped_search-0.3.0
    • scoped_search-0.2.0
    • scoped_search-0.1.2
    • scoped_search-0.1.1
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Easily search you ActiveRecord models with a simple query language using a named scope. — Read more

  cancel

http://techblog.floorplanner.com/2008/07/26/easy-search-with-activerecord/

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Fixed URLs in README 
wvanbergen (author)
Wed Jan 20 00:48:56 -0800 2010
commit  807bfd3261c8091b8c242257baf0a43945a850f1
tree    c6a5e91be4a23964da6b9a1cd5c7d13089465ccb
parent  b781daf5f932f791b5afb5778b08cfbde272efe6
scoped_search /
name age
history
message
file .gitignore Wed Aug 26 00:08:20 -0700 2009 Added /classes and /files to gitignore [wvanbergen]
file LICENSE Mon Sep 28 21:44:28 -0700 2009 Improved RDoc documentation and fixed whitespac... [wvanbergen]
file README.rdoc Wed Jan 20 00:48:56 -0800 2010 Fixed URLs in README [wvanbergen]
file Rakefile Thu Sep 10 00:51:42 -0700 2009 Updated to latest gem release tasks [wvanbergen]
file init.rb Wed Sep 03 22:59:07 -0700 2008 Converted to gem [wvanbergen]
directory lib/ Thu Oct 01 21:49:55 -0700 2009 Added VERSION constant and improved documentation. [wvanbergen]
file scoped_search.gemspec Wed Jan 20 00:47:13 -0800 2010 Adjusted some minor pieces of project metadata. [wvanbergen]
directory spec/ Loading commit data...
directory tasks/
README.rdoc

Scoped search

The scoped_search Rails plugin makes it easy to search your ActiveRecord models. Searching is performed using a query string, which should be passed to the named_scope search_for. Based on a definition in what fields to look, it will build query conditions and return those as a named scope.

Scoped search is great if you want to offer a simple search box to your users and build a query based on the search string they enter. If you want to build a more complex search form with multiple fields, searchlogic (see github.com/binarylogic/searchlogic) may be a good choice for you.

Installing

The recommended method to enable scoped search in your project is adding the scoped_search gem to your environment. Add the following code to your Rails configuration in config/environment.rb, and run rake gems:install to install the gem.:

  Rails::Initializer.run do |config|
    ...
    config.gem 'scoped_search'
  end

Alternatively, install scoped search as a Rails plugin (deprecated):

  script/plugin install git://github.com/wvanbergen/scoped_search.git

Usage

Scoped search requires you to define the fields you want to search in:

  class User < ActiveRecord::Base
    scoped_search :on => [:first_name, :last_name]
  end

For more information about options and using fields from relations, see the project wiki on search definitions: wiki.github.com/wvanbergen/scoped_search/search-definition

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).

  User.search_for('my search string').each { |user| ... }

The result is returned as named_scope. Because of this, you can actually chain the call with other scopes, or with will_paginate. An example:

  class Project < ActiveRecord::Base
    searchable_on :name, :description
    named_scope :public, :conditions => {:public => true }
  end

  # using chained named_scopes and will_paginate in your controller
  Project.public.search_for(params[:q]).paginate(:page => params[:page], :include => :tasks)

More information about usage can be found in the project wiki: wiki.github.com/wvanbergen/scoped_search/usage

Query language

The search query language is simple, but supports several constructs to support more complex queries:

words:require every word to be present, e.g.: some search keywords
phrases:use quotes for multi-word phrases, e.g. "police car"
negation:look for "everything but", e.g. police -uniform, -"police car", police NOT car
logical keywords:make logical constructs using AND, OR, &&, ||, &, | operators, e.g. uniform OR car, scoped && search
parentheses:to structure logic e.g. "police AND (uniform OR car)"
comparison operators:to search in numerical or temporal fields, e.g. > 22, < 2009-01-01
explicit fields:search only in the given field. e.g. username = root, created_at > 2009-01-01
NULL checks:using the set? and null? operator with a field name, e.g. null? graduated_at, set? parent_id

A complex query example to look for Ruby on Rails programmers without cobol experience, over 18 years old, with a recently updated record and a non-lame nickname:

  ("Ruby" OR "Rails") -cobol, age >= 18, updated_at > 2009-01-01 && nickname !~ l33t

For more info, see the the project wiki: wiki.github.com/wvanbergen/scoped_search/query-language

Additional resources

  • Source code: github.com/wvanbergen/scoped_search/tree
  • Project wiki: wiki.github.com/wvanbergen/scoped_search
  • RDoc documentation: rdoc.info/projects/wvanbergen/scoped_search
  • wvanbergen’s blog posts: techblog.floorplanner.com/tag/scoped_search

License

This plugin is released under the MIT license. Please contact weshays (github.com/weshays) or wvanbergen (github.com/wvanbergen) for any questions.

Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server