public
Description: Allows the caching of lookup data in your Rails models
Clone URL: git://github.com/vigetlabs/constant_cache.git
reagent (author)
Thu Jul 05 20:50:14 -0700 2007
commit  d35c35222b7b4e938d6d70448c2f7893993d60b7
tree    a998850f9b24d25f109289ff58dbcdf9bd400fe8
parent  fb8e91f60a82d9bd41702b3ca6720a60eb8c2860
name age message
file MIT-LICENSE Mon Jun 25 21:11:51 -0700 2007 Start of constant_cache plugin (for Advanced Ra... [reagent]
file README Thu Jul 05 20:50:14 -0700 2007 Fixed constants in documentation [reagent]
file Rakefile Mon Jun 25 21:11:51 -0700 2007 Start of constant_cache plugin (for Advanced Ra... [reagent]
file init.rb Tue Jul 03 14:14:28 -0700 2007 Renamed files [reagent]
directory lib/ Thu Jul 05 15:34:13 -0700 2007 Use camelize instead of classify when generatin... [reagent]
directory tasks/ Mon Jun 25 21:11:51 -0700 2007 Start of constant_cache plugin (for Advanced Ra... [reagent]
directory test/ Thu Jul 05 15:34:13 -0700 2007 Use camelize instead of classify when generatin... [reagent]
file test_helper.rb Tue Jul 03 14:14:28 -0700 2007 Renamed files [reagent]
README
CachesConstants
===============

When your database has tables that store lookup data, there is a tendency 
to provide those values as constants in the model.  If you have an
account_statuses table with a corresponding model, your constants may look
like this:

  class AccountStatus
    Active   = 1
    Pending  = 2
    Disabled = 3
  end

There are a couple problems with this approach:

As you add more lookup data to the table, you need to ensure that you're 
updating your models along with the data.  

The constants are stored as integer values and need to match up exactly 
with the data that's in the table (not necessarily a bad thing), but this
solution forces you to write code like this:

Account.new(:username => 'preagan', :status => AccountStatus.find(AccountStatus::Pending))

This requires multiple calls to find and obfuscates the code a bit.  Since classes
in Ruby are executable code, we can cache the objects from the database at runtime
and use them in your application.


Example
=======

The caches_constants plugin "out of the box" assumes that you want to generate
constants from a column called 'name' in your database table.  Assuming this schema:

  create_table :account_statuses do |t|
    t.string :name, :description
  end

  AccountStatus.create!(:name => 'Active',   :description => 'Active user account')
  AccountStatus.create!(:name => 'Pending',  :description => 'Pending user account')
  AccountStatus.create!(:name => 'Disabled', :description => 'Disabled user account')

We can use the plugin to cache the data in the table:

  class AccountStatus
    caches_constants
  end

Now you can write code that's a little cleaner and not use multiple unnecessary find calls:

  Account.new(:username => 'preagan', :status => AccountStatus::Pending)

If the column you want to use as the constant isn't 'name', you can set that in the model. If
we have :name, :slug, and :description, we can use 'slug' instead:

  class AccountStatus
    caches_constants :key => :slug
  end
  
The value for the constant is truncated at 64 characters by default, but you can adjust this as
well:

  class AccountStatus
    caches_constants :limit => 16
  end

Copyright (c) 2007 Patrick Reagan (patrick@viget.com), released under the MIT license