Forum: Ruby on Rails Default values

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
90842970ceaf7ed19136ce7b6f8e1455?d=identicon&s=25 Michael Daines (Guest)
on 2006-06-02 05:36
(Received via mailing list)
What do you think about doing this in a model?

   class GarageSaleItem
     default_value :price, 0.1
   end

A new GarageSaleItem (with nil for price) will cost 10 cents, the
default value. If a price is set, it'll use that price. If the record
is saved and no price is yet set, 10 cents is saved for the price.
You can do this in the schema, I guess, but can you do this easily?

   class Mixture
     serialize :color
     default_value :color, [255, 0, 0]
   end

As you can see, all mixtures are red mixtures by default.

And then this:

   class Movie
     default_value :rating, Proc.new {|m| m.genre.default_rating }
   end

Clearly this doesn't convincingly map to a real situation, but it's
clear what's going on. Instead of using a literal value, we're
calling a proc. Symbols would work they same way they do with filters.

Also, this:

   class MultiFaceted
     default_value [:foo, :bar], true
   end

I have a little plugin for this and I'm using it, but is this a
pattern people use? Is it even a reasonable pattern? Why or why not?


--
Michael Daines
http://www.mdaines.com
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Dave Teare (Guest)
on 2006-06-03 18:50
(Received via mailing list)
At first glance this looks neat. I normally have class methods for
constructing the objects when needed or use the AR after_create
callback.  While this allows me to have different "default values"
depending on the situation, your syntax is more succinct.

I wouldn't mind giving your plugin a go.  Release your plugin into the
wild world and let it fend for itself :)

On Thursday, June 01, 2006, at 8:34 PM, Michael Daines wrote:
>
>     default_value :rating, Proc.new {|m| m.genre.default_rating }
>   end
>Rails@lists.rubyonrails.org
>http://lists.rubyonrails.org/mailman/listinfo/rails


Cheers!
--Dave Teare
http://1passwd.com - Password Manager + AutoFill for Mac OS X
http://devlists.com - Email list management
http://palmsphere.com - Apps for your hand-held
This topic is locked and can not be replied to.