I have a time-sensitive application…not in the sense that its
completion is urgent (although like all projects, it is), but rather
in the sense that much of the functionality is determined by the
As a result, I’ve found that I need to freeze the concept of today
both in development and testing (more-so in testing so that I can
write static test sets).
I’ve started down the road of using an environment variable to store
our date (Date.today if production, else a date that all of my tests
can be based off of). I’m unhappy with this solution for a few
First, the ENV hash requires my values to be strings (try putting this
in your environment.rb file: ENV[‘today’] = Date.new(2007, 5, 15) —
I was shocked when I got a TypeError). As such, for this to be
useful, I need to write a function that “hides” this
implementation---- explicitly a reader/writer pair that converts
between strings and dates. Since this reader/writer pair needs to be
accessible from all parts of my application (testing, controllers,
views), I find myself having to put this pair in multiple
places…not very DRY.
Furthermore (and perhaps worst of all), I actually want my models to
use this redefinition of Date.today. Now, my models should be
completely unaware that they’re being used in a Rails app, so I have a
big problem with them knowing about this. However, I’m not sure of
another way around it.
My last problem just stems from the fact that my whole app now needs
to know about this “other” way to get Date.today (some global function
defined somewhere)…just doesn’t feel right.
Another route that I considered was to reopen the Date class and
redefine the class method “today” conditionally (based on which
environment was loaded). However, I ran into some errors with the
following snippet of code in my environment.rb file:
new(2007, 5, 15)
It’s possible the environment.rb file is the incorrect place to put
I’m open to any thoughts/suggestions/whatever. How has anyone else
handled this problem in the past? What issues did you run into?