public
Description: Add default value for AR attributes
Clone URL: git://github.com/fnando/has_defaults.git
Search Repo:
name age message
folder MIT-LICENSE Sun Jul 20 18:25:04 -0700 2008 first commit [fnando]
folder README Sun Jul 20 20:14:21 -0700 2008 Attribute now accepts a Proc [fnando]
folder init.rb Sun Jul 20 18:25:04 -0700 2008 first commit [fnando]
folder lib/ Sun Jul 20 20:14:21 -0700 2008 Attribute now accepts a Proc [fnando]
folder test/ Sun Jul 20 20:14:21 -0700 2008 Attribute now accepts a Proc [fnando]
README
has_defaults
============

Instalation
-----------

1) Install the plugin with `script/plugin install git://github.com/fnando/has_defaults.git`

Usage
-----

1) Add the method call `has_defaults` to your model.

class Page < ActiveRecord::Base
  has_defaults :title => "New page", :body => "Put your text here"
end

Attributes will be set only if it's a new record and the attribute is blank.

Retrieve the default attribute with the `default_for` instance method:
@page.default_for(:title)

You can pass Proc as attribute:
has_defaults :expires_at => proc { Time.now }

You can override the default attributes as follow:
Page.has_defaults_options = {:title => "Here's your new page", :body => "Write your page text"}

Copyright (c) 2008 Nando Vieira, released under the MIT license