0
-This is a Rails plugin which will aid in adding searching functionality to models. It is in very early development.
0
+This is a Rails plugin which adds some search functionality to models. It also includes a javascript file and helper methods for building a dynamic search form.
0
+If you are running edge rails you can install the plugin straight from the repository:
0
+ script/plugin install git://github.com/ryanb/searchify.git
0
+Otherwise you can install it with this command:
0
+ git clone --depth=1 git://github.com/ryanb/searchify.git vendor/plugins/searchify
0
+To use the dynamic form you need to install a javascript file using this command:
0
+ script/generate searchify_javascript
0
+This plugin uses the "paginate" method to perform a search on the model, so you need to install the will_paginate plugin/gem.
0
+Currently only MySQL is fully supported. It may work on other database engines, just not as well.
0
+You can use the "searchify" method in your model class to enable it for searching. Pass the columns you want to search to this method.
0
+ class Product < ActiveRecord::Base
0
+ searchify :name, :description, :price, :created_at, :discontinued
0
+You can then use the "search" method to search all or some of these columns. Here are some examples.
0
+ Product.search(:name => 'Puzzle', :description => '%forest%') # use percent sign for partial search
0
+ Product.search(:all => 'blue puzzle') # searches for partial word "blue" and "puzzle" in all columns
0
+ Product.search(:created_at_from => '2008-01-01', :created_at_to => '2008-02-01')
0
+ Product.search(:price_operator => '<', :price => '40') # changes the comparison operator
0
+ Product.search(:page => 5, :per_page => 20, :order => 'name') # pass other options besides conditions
0
+The searchify also allows you to include associations in the search. For example:
0
+ class Category < ActiveRecord::Base
0
+ searchify :name, :products => [:name, :description]
0
+To search the product columns you need to prefix them with "products_" like this:
0
+ Category.search(:products_name => 'Big Ben') # finds all categories with a product called Big Ben
0
+ Category.search(:all => 'puzzle') # product columns are included
0
+The main benefit of this plugin is how easy it is to add a dynamic search. You can use the searchify_fields_for helper method to generate dynamic fields for searching. Like this:
0
+ <% form_tag products_path, :method => 'get' do %>
0
+ <%= searchify_fields_for Product %>
0
+ <p><%= submit_tag 'Search', :name => nil, :onclick => 'return searchify_submit()' %></p>
0
+Make sure you have the prototype libraries included and it should allow you to dynamically add and remove fields. Pretty cool huh?
0
Copyright (c) 2008 Ryan Bates, released under the MIT license
Comments
No one has commented yet.