rbarazi / simply_searchable

Ruby on Rails plugin to simplify searching your model

Home Page:http://www.ridaalbarazi.com/blog/2008/11/05/simplysearchable-plugin/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SimplySearchable

SimplySearchable is a search plugin created by Rida Al Barazi - rida.me to be used in SpinBits’s Skeleton App.

The main goal of SimplySearchable is to make it easy to do queries on your model by auto-magically creating some named_scope methods for common conditions, it adds a method to the model named “list” that will find and filter records smartly.

Please submit your bugs, requests and feedback at the project’s page on Lighthouse - rbarazi.lighthouseapp.com/projects/19246/home. RDocs are available at ridaalbarazi.com/code/simply_searchable/.

Example:

If you have the following attributes in you posts table:

Post
	id:integer
	title:string
	body:text
	category_id:integer
	created_at:datetime
	updated_at:datetime

In your Model:

class Post < ActiveRecord::Base
  has_many   :comments
  belongs_to :category
  simply_searchable
end

This will create the following named scopes:

named_scope where_id,         lambda {|value| { :conditions => ["id = ?", value] }}
named_scope where_title,      lambda {|value| { :conditions => ["title like ?", "%#{value}%"] }}
named_scope where_body,       lambda {|value| { :conditions => ["body like ?", "%#{value}%"] }}
named_scope where_created_at, lambda {|value| { :conditions => ["created_at = ?", value] }}
named_scope where_updated_at, lambda {|value| { :conditions => ["updated_at = ?", value] }}
named_scope where_categories, lambda {|value| { :conditions => ["category_id in (?)", [*value]] }}
named_scope where_comments,   lambda {|value| { :conditions => ["comments.ids in (?)", [*value]] }}

It will also create the method ‘list’ which you can use like:

Post.list(:title => 'abc', :created_at => Date.today, :category => [1,3])

Which will return the posts that contain ‘abc’ in their title and created today from categories 1 and 3.

By default SimplySearchable list will_paginate, you can still disable it:

class Post < ActiveRecord::Base
  simply_searchable :will_paginate => false
end

Or customize its settings:

class Post < ActiveRecord::Base
  simply_searchable :per_page => 20
end

About

Ruby on Rails plugin to simplify searching your model

http://www.ridaalbarazi.com/blog/2008/11/05/simplysearchable-plugin/


Languages

Language:Ruby 100.0%