SimplySearchable plugin

Update 1:Associations support has been implemented.

Almost a year ago I released SimplySearchable plugin to help me implement the filtering options of restate.ae and during this year I’ve been updating it regularly until it become something completely different so I did a quick rewrite, moved all the logic to the model, moved the repository to github and here it is.

SimplySearchable

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.

This plugin 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. RDocs are available at http://ridaalbarazi.com/code/simply_searchable/.

Installation

  ./script/plugin install git://github.com/rbarazi/simply_searchable.git

And in your model:

  class Post < ActiveRecord::Base
    simply_searchable
  end

Then in your controller you can say:

  class PostsController < ApplicationController
    def index
    @posts = Post.list(params)
  end

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

And 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

In the new implementation there is no support for associations’ filtering yet, it will come very soon though so stay tuned. Association support has been implemented.

Comments

8

  1. Double Shot #329 « A Fresh Cup said..

    [...] SimplySearchable plugin - A new take on automatically building some useful named scopes. [...]

  2. Dwayne said..

    I like the list method added.

    What’s the difference between _where_name and _by_name, already implemented in RoR. ?

  3. Rida said..

    @Dawyne, in SimplySearchable the where_* methods are actually named scopes while the by_* methods in RoR are regular methods. the other difference is that the where_* methods apply conditions depending on the column type so for strings and descriptions it uses ‘like’ instead of ‘=’.

    The main advantage of using named_scopes is the ability to use it several times on the same object like: Post.where_name(”test”).where_id(1) which is not the case in by_* methods of RoR.

  4. เร็วส์ หกสิบหก » นั่งเทียนเขียนข่าว#27 said..

    [...] SimplySearchable Plugin [...]

  5. mike grassotti said..

    Nice plugin. Just checking it out and noticed that you’ve hard-coded Post class in the list method. This will work for your example (and unit tests) but fails when used on other active record models. I made the following modification and so far so good….

    #76: return Post.with_pagination ? listings.paginate(:page => options[:page], :per_page => options[:per_page]) : listings.all

    >>>>>>

    #76: return self.with_pagination ? listings.paginate(:page => options[:page], :per_page => options[:per_page]) : listings.all

  6. Rida said..

    @mike thanks for the patch dude.. it’s already live ;)

  7. Shuaib Zahda said..

    Thanks for the plugin. I wanted to use it but I found it does not support OR, AND. eg I want to search the word in several columns in the table and if there is a match it shall return the row

    post.title LIKE ? || post.body || etc….
    when I implemented it, I found it does search in the specified field using AND. Do u have a choice of using OR

    Thanks

  8. Rida said..

    @Shuaib, SimplySearchable uses named_scope convention to apply conditions which by default uses AND as junction. I see your valid request, I’ll do some research and see what will be the best way to do it and eventually launch it as a new feature.

Leave a Reply

you can use these xhtml tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Articles Permanent Link