Agile Web Development

Build it. Launch it. Love it.

enum_fu

With enum_fu, you can use integer fields in DB as enum typed in ActiveRecord.

This enables

  • Faster operation in DB
  • Easy coding

Currently enum_fu is managed in GitHub

  • git://github.com/ikspres/enum_fu.git

But svn repository will be updated too for each official release.

  • http://ikspres.googlecode.com/svn/trunk/railsplugins/enumfu

Vitals

Repository http://ikspres.googlecode.com/svn/trunk/rails_plugins/enum_fu
License Ruby's
Tags Tag_red enum
Rating (6 votes)
Owner ikspres
Created 25 October 2007

Comments

  • 26 October 2007

    That could be :limit => 1, since limit uses size in bytes and 2 would be up to 65,000 enum types. Very cool though, Nice for when an entire model/controller group is too much for the field and only needs to be managed by the programmer.

    Possible downside: role names (in this example) are nowhere in the database and if other apps are reporting from it, they wont know what role "1" is. Relatively minor I'd say. Nicely done.

  • Avatar
    Georg Ledermann
    6 December 2007

    There are problems with clearing an enum field (needed in forms):

    u.role = ''

    => ArgumentError: interning empty string

    To fix this I changed line 43 in enum_fu.rb (after "p3 = Proc ...."):

    write_attribute name.to_s, (sym.blank? ? nil : self.class.const_get(const_name).index(sym.to_sym))

  • Avatar
    13 December 2007

    Thanks for this plugin, it's very useful to me right now.

    I made a small change because as it is right now, it does not allow for null values. I noticed that it was defaulting to always returning the first value in the array of enum options, so it wasn't possilble to have a select field with a blank, unselected first option.

    So I made the change linked here starting at line 36.

    http://pastie.caboo.se/128202

  • Avatar
    28 December 2007

    Thank you Norman Clarke and Georg Ledermann. I applied your patches. Now nil is handled also.

    u.role = nil u.save

  • Avatar
    29 December 2007

    Thanks for applying the patches. But there is one more patch needed, if someone wants to set the integer value of enum columns directly, e.g. user.role = 1. The plugin "acts_as_trashable" is doing so.

    I have patched p3 one more time: http://pastie.caboo.se/133043

  • Avatar
    Brenton Bartel
    15 April 2008

    I ran into an issue where depending on edit/create the field corresponding to the enum would be hidden or a select; however, when it was a hidden field the value it would specify was the corresponding into value, not the stringified symbol .. as such upon saving it would screw things up. Taking Georg's patch, I further expanded on that to detect the type:

    http://pastie.caboo.se/181202

  • Bill Siggelkow
    14 August 2008

    I added a minor tweak to Brenton's patch that allows the enum to be set directly with the integer value (e.g. user.role = 1) as well as a string integer value (user.role = '1').

    http://pastie.org/253013

  • Avatar
    19 February 2009

    The plugin wasn't working when a uniqueness validation (or any other validation that checks the value of the corresponding enum column) is present.

    ex: (This is not working) <code> acts_as_enum :role, [:admin, :user] validates_uniqueness_of :name, :scope => :role </code> This translates into an SQL ending with the following when executed <code> .role = '--- :admin\n' </code>

    The reason is that ActiveRecord is calling record.send('role') and this is returning a symbol (after casting).

    What I ended up doing is modifying enum_fu to prepend "enum_" to the attribute name. After the modifications, I'm able to use the following <code> User.role # => 0 User.enum_role #=> :admin User::ENUM_ROLE #=> [:admin, :user] User.enum_role(:admin) #=> 0

    User.enum_role = :user User.role #=> 1 </code>

    I haven't had the enough time to thoroughly/auto test this approach, but, my application tests are so-far passing (after the minor code changes).

Add a comment