Acts As Dropdown

Posted by DeLynn Berry on September 18, 2006 at 02:12 pm

Overview

The acts_as_dropdown Plugin allows any ActiveRecord class to be easily used as the contents of a HTML select tag.

Installation

To install the acts_as_dropdown plugin into a current Rails application run the script/plugin script from the root of your application passing it the url of svn://delynnberry.com/code/plugins/acts_as_dropdown/trunk. For example:


[ruby] script/plugin install svn://delynnberry.com/code/plugins/acts_as_dropdown/trunk

Once installed you will need to restart your webserver for the plugin to be loaded into the Rails environment.

Usage

Here is a simple example for how to use the acts_as_dropdown plugin. Assuming you have a model that contains a list of states, consider the following code:

1
2
3
class State < ActiveRecord::Base
  acts_as_dropdown
end

Since the plugin automatically assumes the use of the :name and :id attributes of the class for use when creating the text and value attributes of the option tags, you can now easily use the select form helper to create a dropdown of the States:

1
2
3
4
5
6
7
8
<%= select("person", "state_id", State.to_dropdown) %>

<select name="person[state_id]">
  <option value="1">Alabama</option>
  <option value="2">Alaska</option>
  <option value="3">Arizona</option>
  ...
</select>

You can also customize what the to_dropdown method returns using the :value, :text, :conditions, and :order parameters (both at the class level and at the method level. For example:

1
2
3
class State < ActiveRecord::Base
  acts_as_dropdown :text => "abbreviation", :order => "id DESC"
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%= select("person", "state_id", State.to_dropdown) %>

<select name="person[state_id]">
  <option value="3">AZ</option>
  <option value="2">AK</option>
  <option value="1">AL</option>
  ...
</select>

<%= select("person", "state_id", State.to_dropdown(:text => "name") %>


<select name="person[state_id]">
  <option value="3">Arizona</option>
  <option value="2">Alaska</option>
  <option value="1">Alabama</option>
  ...
</select>

The plugin also provides a to_dropdown method for the Array class which allows you to easily turn any array into a group of select options. Just like the ActiveRecord class method the Array method assumes the :name and :id attributes for the text and value of the option tag. For example:


@people = People.find(:all, :limit => 5, :order => "id")
1
2
3
4
5
6
7
8
9
<%= select("post", "person_id", @people.to_dropdown) %>

<select name="post[person_id]">
  <option value="1">David</option>
  <option value="2">Sam</option>
  <option value="3">Tobias</option>
  <option value="4">Courtney</option>
  <option value="5">DeLynn</option>
</select>

Uninstall

Uninstalling is simply a matter of running script/plugin from the root of your Rails application. Except this time you pass the remove parameter:


[ruby] script/plugin remove acts_as_dropdown

Documentation

RDoc has been run on the plugin directory and is available in the download. You can also view it online from here: http://code.delynnberry.com/rdoc/acts_as_dropdown/

Running Unit Tests

There are extensive unit tests in the “test” directory of the plugin. Currently, only MySQL is supported, but you should be able to easily fix this by looking at “connection.rb”. You’ll need to create a database for the tests and put the connection information into “connection.rb” as well as import the schema file for MySQL that can be found at “test/fixtures/mysql.sql”.

To run the test simply execute the follow from the test directory inside the Userstamp plugin directory:


ruby dropdown_test.rb

Bugs & Feedback

Bug reports and feedback are always welcome. Please send them to delynn@gmail.com with [ActsAsDropdown] in the subject line. You can also visit http://delynnberry.com/articles/category/acts_as_dropdown/ and post a comment on any of the posts.

Credits and Special Thanks

The original idea for this plugin came from courtenay’s ”handy select functions” post on http://habtm.com.