tog / tog watch
public
Description: Base gem that bootstrap tog platform configuration in a Rails app
Home | Edit | New

How to make your models searchable

Make your model searchable

You have to add a method ‘site_search(query, search_options={})’ to your model. This methods performs the search.

A very basic implementation for a model called Story could be:

  
  def self.site_search(query, search_options={})
    Story.find(:all, :conditions => ["title like ?", "%#{query}%"])
  end

Inform tog about your model

Tog comes with a built-in search controller that will search your model and merge the results with the returned by other models. To inform tog that you have a searchable model, you will need to add the following line to an initializer or to the init.rb of your plugin.

Tog::Search.sources << "Story"

Showing search results

The search engine included with tog will merge the result of this method from every model. To render your model properly, you will need to add a partial that is used in the serach result page. This partial should be located on the folder “search” and the name the same of your model, e.g.:
Then you have to create the view used to show the results of the search. This is a partial view, with the same name as your model, e.g.:


app/views/search/_story.html.erb
Last edited by coyote, Thu Jan 29 13:32:24 -0800 2009
Home | Edit | New
Versions: