The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Acts As Suggest plugin

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Antonio Cangiano

Posts: 333
Nickname: acangiano
Registered: Dec, 2005

Antonio Cangiano is a Ruby hacker
Acts As Suggest plugin Posted: Feb 7, 2007 11:49 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Antonio Cangiano.
Original Post: Acts As Suggest plugin
Feed Title: Zen and the Art of Ruby Programming
Feed URL: http://programmingzen.com/category/ruby/feed/
Feed Description: Antonio Cangiano's blog about Ruby development.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Antonio Cangiano
Latest Posts From Zen and the Art of Ruby Programming

Advertisement

When searching for the word “honnolullu”, Google will promptly suggest “Did you mean: honolulu”. This feature is quite useful because it drastically improves usability. The lack of this exact function makes Wikipedia a pain in the neck as far as searching goes. So much so that I, and I suspect most people, just google for “wikipedia searched_term”.

Hence, I decided to create a small plugin. The acts_as_suggest Rails plugin allows developers to easily add this functionality to any ActiveRecord class, basing the suggestions on the existing values in the table. For example, if you have a table Articles with a column Title, you will be able to retrieve a list of possible corrections for a misspelled article title. The suggestions are not coming from a dictionary, making it very flexible.

Installation


Make sure you have the required Text rubygem, if not please install it: gem install Text

Download acts_as_suggest.zip, extract it and copy the acts_as_suggest folder into the vendor/plugins directory of your application. You’ll also need to restart your web server.

Usage


Assuming you have a model for a table that contains a list of articles, you would start by including acts_as_suggest in your model:

class Article < ActiveRecord::Base
  acts_as_suggest
end

This will provide the model with the suggest method, which is the core of the plugin. At this point you can retrieve suggestions as follows:

Article.suggest(:title, 'honnolullu')
Article.suggest([:title,:author], 'David Copperffelds')

These will return an array of records if the searched string is not misspelled and matches an existing value in the database. If no matches are found, the method will attempt to find similar existing values and return an array of strings (your suggestions). If none of the strings in the given column(s) are close enough, an empty suggestion array is returned.

The return values reflect a fairly common scenario. With a single method call I can retrieve all the records that match a user search, or I can retrieve suggestions to correct the spelling of his/her search if no records are found.

Please check the documentation for further details.

Documentation


You can read the short documentation online here or access the local copy in the /doc folder.

Bugs and Feedback


Here comes a little disclaimer. I wrote this plugin in half an hour, including the basic documentation and a few tests. Therefore, I’ve not yet setup an SVN repository for it; there are just a few test cases (I’m not even publishing them at this time) and it is very likely that there will be bugs, issues or possible improvements required. I’m releasing this .zip file as early as possible, while the code is in its infancy. In this way I can survey user interest and eventually further develop it. So please, send in any feedback and bug reports by email.

Read: Acts As Suggest plugin

Topic: Data Extraction for Web 2.0: Screen Scraping in Ruby/Rails, Episode 1 Previous Topic   Next Topic Topic: JRuby article published on Developer.com

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use