Searchify plugin

Plugin details

Rails plugin to add extra searching functionality to models.

Websitehttp://github.com/ryanb/searchify Repositorygit://github.com/ryanb/searchify.git Author Ryan Bates Tags Search LicenseMIT

Documentation

Install the plugin:
ruby script/plugin install git://github.com/ryanb/searchify.git

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.

  class Product < ActiveRecord::Base
    searchify :name, :description, :price, :created_at, :discontinued
  end


You can then use the "search" method to search all or some of these columns. Here are some examples.

  Product.search(:name => 'Puzzle', :description => '%forest%') # use percent sign for partial search
  Product.search(:all => 'blue puzzle') # searches for partial word "blue" and "puzzle" in all columns
  Product.search(:created_at_from => '2008-01-01', :created_at_to => '2008-02-01')
  Product.search(:price_operator => '<', :price => '40') # changes the comparison operator
  Product.search(:page => 5, :per_page => 20, :order => 'name') # pass other options besides conditions


The searchify also allows you to include associations in the search. For example:

  class Category < ActiveRecord::Base
    has_many :products
    searchify :name, :products => [:name, :description]
  end


To search the product columns you need to prefix them with "products_" like this:

  Category.search(:products_name => 'Big Ben') # finds all categories with a product called Big Ben
  Category.search(:all => 'puzzle') # product columns are included




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:

  <% form_tag products_path, :method => 'get' do %>
    <%= searchify_fields_for Product %>
    < p><%= submit_tag 'Search', :name => nil, :onclick => 'return searchify_submit()' %>< /p>
  <% end %>


Make sure you have the prototype libraries included and it should allow you to dynamically add and remove fields. Pretty cool huh?

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: hardway, about 1 year ago