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 D.
http://www.mdaines.com