cblunt / rails-attribute_searchable

(NOTE: Since starting this plugin, I've learned that similar results can be attained with named_scope. I'll therefore be updating the code of this plugin to use named_scope.) Rails plugin that lets ActiveRecord models be filtered by terms and attributes.

This URL has Read+Write access

name age message
file MIT-LICENSE Sun Jun 21 00:54:37 -0700 2009 Updated licence [cblunt]
file README.rdoc Mon Jun 29 15:24:56 -0700 2009 Updated readme [cblunt]
file Rakefile Sat Jun 20 14:31:17 -0700 2009 Initial import [cblunt]
file init.rb Sun Jun 21 00:49:53 -0700 2009 Stubbed search method and added attribute_searc... [cblunt]
file install.rb Sat Jun 20 14:31:17 -0700 2009 Initial import [cblunt]
directory lib/ Thu Jun 25 11:31:30 -0700 2009 Evaluate attributes from :filters and build con... [cblunt]
directory tasks/ Sat Jun 20 14:31:17 -0700 2009 Initial import [cblunt]
directory test/ Sat Jun 20 14:31:17 -0700 2009 Initial import [cblunt]
file uninstall.rb Sat Jun 20 14:31:17 -0700 2009 Initial import [cblunt]
README.rdoc

Attribute-searchable

Introduction

attribute_searchable is a small plugin that adds filtering ActiveRecord to models. The plugin makes use of the ez_where plugin to allow complex attribute filters to be built up.

To see my original blog posts that led to attribute_searchable, please visit chrisblunt.com/blog/2009/05/12/rails-building-complex-search-filters-with-activerecord-and-ez_where/

Installation

attribute_searchable is dependent on the ez_where plugin:

  ruby script/plugin install http://opensvn.csie.org/ezra/rails/plugins/dev/ez_where

Once ez_where is installed, install attribute_searchable with:

  ruby script/plugin install git://github.com/cblunt/rails-attribute_searchable.git

Usage

To make a model class attribute_searchable

  class User < ActiveRecord::Base
    attribute_searchable
  end

User will now have a search method which wraps the normal find method. You can use search to wrap find, e.g:

  User.search(:all)
  => [...]

As well as all the normal find options, search takes an optional filters hash, which is used to specify attribute filters, e.g:

  # SQL: SELECT * FROM users WHERE (status = 5) AND (archived = TRUE)
  User.search(:all, :filters => { :status => 5, :archived => true })

attribute_searchable models can also be searched using terms. terms is a special filter option which is used to build SQL ILIKE queries on your model’s string-type attributes.

You must first specify which attributes will be searched when filtering by terms, e.g:

To search for users by first_name, last_name or email_address:

  class User < ActiveRecord::Base
    attribute_searchable :terms_attributes => [:first_name, :last_name, :email_address]
  end

You can now filter user’s by search terms, e.g:

  # SQL: SELECT * FROM users
  # WHERE (first_name ILIKE '%mary%' OR last_name ILIKE '%mary%' OR email_address ILIKE '%mary%')
  User.search(:all, :filters => {:terms => %w{mary} })

Where multiple terms are provided, they are joined using an AND clause:

  # SQL: SELECT * FROM users
  # WHERE (first_name ILIKE '%mary%' OR last_name ILIKE '%mary%' OR email_address ILIKE '%mary%')
  #   AND (first_name ILIKE '%company%' OR last_name ILIKE '%company%' OR email_address ILIKE '%company%')
  User.search(:all, :filters => {:terms => %w{mary company} })

Links/References

Copyright © 2009 Chris Blunt, released under the MIT license