Skip to content
rbarazi edited this page Sep 14, 2010 · 2 revisions

SimplySearchable

SimplySearchable is a search plugin created by Rida Al Barazi 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 automagically create some named_scope methods for common conditions.

This plugin adds a method to the model named “list” that will find and filter records smartly.

RDoc available at: http://ridaalbarazi.com/code/simply_searchable/

Example:

If you have the following attributes in you posts table:

Post
id:integer
title:string
body:text
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)

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

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

Clone this wiki locally