This repository is private.
All pages are served over SSL and all pushing and pulling is done over SSH.
No one may fork, clone, or view it unless they are added as a member.
Every repository with this icon (
) is private.
Every repository with this icon (
This repository is public.
Anyone may fork, clone, or view it.
Every repository with this icon (
) is public.
Every repository with this icon (
commit 1321953a082e0da3707e4bad604e2c3bd2a0307d
tree 8e4667b6c72d811146643b597a6eb74af72be93f
parent 350806f8a30a53d0289c9cd3234f5e26d350dca4
tree 8e4667b6c72d811146643b597a6eb74af72be93f
parent 350806f8a30a53d0289c9cd3234f5e26d350dca4
| name | age | message | |
|---|---|---|---|
| |
MIT-LICENSE | Mon Jun 25 21:11:51 -0700 2007 | [reagent] |
| |
README | Fri Jul 06 10:00:58 -0700 2007 | [reagent] |
| |
Rakefile | Mon Jun 25 21:11:51 -0700 2007 | [reagent] |
| |
init.rb | Wed Nov 21 11:16:38 -0800 2007 | [reagent] |
| |
lib/ | Wed Nov 21 11:16:38 -0800 2007 | [reagent] |
| |
tasks/ | Mon Jul 09 18:33:51 -0700 2007 | [reagent] |
| |
test/ | Fri Jul 06 12:39:06 -0700 2007 | [reagent] |
| |
test_helper.rb | Fri Jul 13 05:56:57 -0700 2007 | [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




