0.0
No commit activity in last 3 years
No release in over 3 years
This gem provides dynamically created constants for activerecord models
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

constantize

Description

Simple, dynamic and efficient activerecord model constants.

Ever wanted to be able to be able to do something like:

case buoy.buoy_type
when BuoyType::NOAA
..
when BuoyType::SCRIPPS
..
end

or

Bouy.create(:name => :foo, :bouy_type => BuoyType::NOAA)

but weren't sure the best / most efficient way to implement?

One way would be to do something like:

class BuoyType < ActiveRecord::Base
  NOAA = find_by_name('noaa')
  ..
end

However, this approach can become tedious and tough to manage.

Another way would be to dynamically create the constants for all the rows in the given table at rails start. However, for models w/hundreds of rows, e.g., Country, City, this wouldn't be very efficient either.

This gem minimizes the db calls by memoizing the finder that is used to look up the constant values. So, it depends on your app server but only the first refernece to a given constant should require a db query (see the code and specs for more details).

Install

Manual Install

Standard gem install:

gem install constantize

After installing the gem you would need to require it:

require 'constantize'

Rails 2.x / No Bundler

In a Rails 2.x project you can add this to your environment.rb:

config.gem 'constantize'

followed by:

rake gems:install

Rails 3.x / Bundler

In Rails 3.x add this to your Gemfile:

gem 'constantize'

followed by:

bundle install

Usage

In the model that you will be "constantizing" simply add the following to the top:

constantize :key

where :key is the name of the field that will be used to build the finder.

E.g.,

class BuoyType < ActiveRecord::Base
  constantize :name
  ..
end

this would assume that your buoy_types table looked something like:

+----+---------+
| id | name    |
+----+---------+
|  1 | noaa    |
|    ..        |
+----+---------+

if your table used a different field, say :title, rather than :name:

+----+---------+
| id | title   |
+----+---------+
|  1 | noaa    |
|    ..        |
+----+---------+

then your model definition would look like:

class BuoyType < ActiveRecord::Base
  constantize :title
  ..
end

Finally, if you wanted only the :id to be returned from the constant call (rather than the entire model) you could do:

class BuoyType < ActiveRecord::Base
  constantize :name, :id
  ..
end

and your call to BuoyType::NOAA would return only the interger :id value (1 in this case).

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright (c) 2010 Adam Weller. See LICENSE for details.