There are a lot of approaches, but I was wondering what the idiomatic
Rails-y way of doing it is. Should I create a Weekday class? It
wouldn't be backed by the db - that's not localizable or DRY...
Any help appreciated.
There are two pieces to this. One is how you deal with the data readably in
your code, the other is rendering a localized version.
> There are a lot of approaches, but I was wondering what the idiomatic
> Rails-y way of doing it is. Should I create a Weekday class? It
> wouldn't be backed by the db - that's not localizable or DRY...
In your code, the idiomatic way is to use an array:
DAYS = %w(sunday monday tuesday wednesday thursday friday saturday)
You should override your accessors that otherwise return a number 0-6 to
instead return that DAYS[value] (or, more likely, value && DAYS[value]).
Now you can deal with days by string. (Probably a better choice is to make
that an array of symbols and deal with them by symbol.) Once you get to the
rendering phase, just have your local substitutions based on the internal
symbols.
> Any help appreciated.
--Greg
hth,
-jc
Date.new(2001,1,weekday_number+7).strftime("%A")
Someone please tell me a better way. :)