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

eugenebolshakov / search_model

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 17
    • 0
  • Source
  • Commits
  • Network (0)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
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.
  cancel

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

This URL has Read+Write access

String attribute values are now stripped 
eugenebolshakov (author)
Mon Dec 14 23:40:30 -0800 2009
commit  01eae5771dc701dd9a99b0775fcff9ba993a5191
tree    05332a9a69358ecb399334b35847ff04f3371142
parent  2d2a1e7c29ac6f10e9da8efff62986f172b0af67
search_model /
name age
history
message
file MIT-LICENSE Loading commit data...
file README
file Rakefile
file init.rb
directory lib/
directory test/
README
SearchModel
===========

The plugin helps to build search forms in your rails application. It allows to 
have complex search parameters, restful and easily testable controller and clean
and standard forms in the view.

It expects that your model has a named scope for every search parameter that 
you'd like to have. I've figured that using named scopes is convenient for 
search parameters cause it allows you to encapsulate db specific stuff:

class Post < ActiveRecord::Base
  has_and_belongs_to_many :categories
  
  named_scope :by_categories, lambda { |categories| 
    { 
      :include => :categories,
      :conditions => ['categories.id IN (?)', categories]
    }
  }
  
  named_scope :by_keyword, lambda { |value| 
    {
      :conditions => ['(title like ? OR body like ?)', "%#{value}%", "%#{value}%"]  
    }
  }
end

In this example by_categories named scope allows to search by ids of categories 
so you could have a list of categories as a bunch of check boxes in your search 
form.

To make a search form restful I usually create a separate controller for it with
new and create actions:

The new action displays a search form and the create action (after the user 
submits the form) displays the same form with the values entered and the search
results.

To make the controller easily testable the plugin provides a SearchModel class
that encapsulates all search related logic and which you can subclass to create
your own search class:

class PostSearch < SearchModel
  search_attributes :categories, :keyword
end

The search_attribute method is used to define which search parameters you would 
like to have. There needs to be a named scope for each parameter in the model.

SearchModel provides a constructor that supports mass assignment and creates
attributes for every search parameter. That means that it can be used like an
ActiveRecord object in the controller:

class PostSearchesController < ApplicationController
  def new
    @search = PostSearch.new
  end
  
  def create
    @search = PostSearch.new(params[:search])
    @posts = @search.results
  end
end

The results method returns records that match the search criteria.

In the view you just build a form as you would do for any ActiveRecord instance:

<% form_for :search, @search do |form| -%>
  <p>
    <%= form.label :keyword %>
    <%= form.text_field :keyword %>
  </p>  
  ....
<% end -%>

SearchModel gets all parameters from the request and generated a query using the
named scopes for the parameters that are not blank. All logic is in the search 
object which makes the controller skinny and easily testable. The search object
has attributes for every search parameter which makes it easy to display the
search form with the parameters entered on the search results page. The fact 
that it uses named scope for search parameters allows to have any kind of search
parameters - just define a named scope for it. It supports search parameters which
are arrays (like a group of check boxes) or hashes (like complex attributes which
consist of several attributes).

Copyright (c) 2008 Eugene Bolshakov, released under the MIT license
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