a work on process

Viewing posts tagged: plugins

model_auto_completer

19 February 2007 (8:23 am)

By James Stewart
Filed under: Notes
Tagged: , , ,

Rails’ auto_complete integration is pretty nice for building search functionality, but for entry forms it all too often falls short.

Typically if I’m using an auto complete field in a form it’s because I want to link that record with an existing entry in another table, such as connecting an event with a location, or a book with a publisher, and that won’t work because the value obtained from the auto completion is the name of the linked entity, not its ID. I’ve tried various ways of managing that, either by overriding the standard methods or by adding some extra code in my model that will convert a name back into the associated model ID, but they all feel like hacks and I’d not had time to clean up the code and package anything as a plugin.

Thankfully Xavier Nora’s already done it with the model_auto_completer plugin. The plugin provides a set of helper methods that will allow you to use auto completion, but have the ID of the record found stored as a hidden field in your form, making processing considerably easier.

Initially I had a problem using the plugin, and in fact found my rails application falling over because the plugin wasn’t getting the right name for the foreign key and since I have other fields that allow entry of a new location, rails couldn’t tell if a post with:

location[]=123&location[name]=My%20Location

should use the first location data or the later data.

Looking through the code I found that instead of using:

foreign_key  = real_object.class.reflect_on_association(association).options[:foreign_key]

to get the foreign key it should be using:

foreign_key = real_object.class.reflect_on_association(association).primary_key_name

Having submitted a patch, I was very pleased to hear over the weekend that the change has been integrated into the plugin so I can let piston update back to the official trunk. With that change in place, the plugin is a great way to make data entry much more convenient.

Recommend this post:

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

 

I’ve been procrastinating on getting my first Ruby on Rails plugin (first anyone else might be interested in, at least) out the door, mainly due to some configuration problems with Subversion, but finally I have that all sorted out and the plugin ready to go.

loads_from_amazon is an ActiveRecord Mixin that works with the Ruby/Amazon library to let you populate an ActiveRecord object with the details of a product listed on amazon. In the README I use the following example:

class Book < ActiveRecord::Base
  acts_as_amazon
  maps_to_amazon_attribute :authorlist => 'authors', :combine => ';'
  maps_to_amazon_attribute :title => 'product_name'
  maps_to_amazon_attribute :isbn => 'asin'
  maps_to_amazon_attribute :publisher => 'manufacturer'
  maps_to_amazon_attribute :pubdate => 'release_date'
end
 
@book = Book.load_from_amazon(isbn) # Loads but does not save
@otherbook = Book.load_from_amazon!(other_isbn) # Loads data and saves to database

So feel free to grab it from svn (http://projects.jystewart.net/svn/rails/loads_from_amazon/trunk) and let me know what you think.

Recommend this post:

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

 
« Previous Page