Enum Column plugin

Plugin details

This is an ROR extension to support enumerations in the database using the enum type in MySQL or column constraints in other databases. Currently only MySQL has been implemented, but other connection adapters can easily be extended to check for column constraints and then parse out the possible values.

Websitehttp://enum-column.rubyforge.org/ Repositorysvn://rubyforge.org/var/svn/enum-column Author William Sobel Tags Database LicenseRuby's (MIT)

Documentation

Install the plugin:
ruby script/plugin install svn://rubyforge.org/var/svn/enum-column

In your schema
==============
When you create your schema, specify the constraint as a limit:

  create_table :enumerations, :force => true do |t|
    t.column :severity, :enum, :limit => [:low, :medium, :high, :critical],
       :default => :medium
    t.column :color, :enum, :limit => [:red, :blue, :green, :yellow]
    ...
  end


In the model
===========
You can then automatically validate this column using:

    validates_columns :severity, :color


The rest will be handled for you. All enumerated values will be given as symbols.

    @e = Enumeration.new
    @e.severity = :low



In the views.
=============
In the controller:

   @e = Enumeration.new


The enumerates list of values will be specified as follows:

    <%= input 'e', 'severity' %>


Will create a select/option list:

     < select id="e_severity" name="e[severity]">
     	     < option value="low">low< /option>
	     < option value="medium" selected="selected">medium< /option>
	     < option value="high">high< /option>
	     < option value="critical">critical< /option>
     < /select>


You can also create a set of radio buttons using the following helper:

    <%= enum_radio('e', 'severity') %=>


Will produce the following group of radio buttons:

    < label>low: < input id="test_severity_low" name="test[severity]" type="radio" value="low" />< /label>
    < label>medium: < input checked="checked" id="test_severity_medium" name="test[severity]" type="radio" value="medium" />< /label>
    < label>high: < input id="test_severity_high" name="test[severity]" type="radio" value="high" />< /label>
    < label>critical: < input id="test_severity_critical" name="test[severity]" type="radio" value="critical" />< /label>



You can always use the column reflection to get the list of possible values from the database column.

    Enumeration.columns_hash['color'].values



Will yield: [:red, :blue, :green, :yellow]

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | Back in time (5 older versions) | Last edited by: hardway, 10 months ago