How to DRY with Fixtures (helper or extend Time class, how)?


#1

I have a test/fixtures/users.yml like so:

apa:
id: 1
username: apa
[…]
created_at: <%=Time.now.strftime("%Y-%m-%d %H:%M:%S")%>
updated_at: <%=Time.now.strftime("%Y-%m-%d %H:%M:%S")%>

I don’t like how I’m repeating myself with the strftime bit.

Is there some shorter Time method to format time for a (MySQL) datetime
field that I’ve missed? The magic of created_at/updated_at does not seem
to work with fixtures.

Assuming I want to avoid repeating myself, in what file would I add a
helper method for this, or extend the Time class with a method?
Apparently not test_helper - “undefined local variable or method”.


#2

On Sat, Jan 07, 2006 at 11:21:04AM +0100, Henrik wrote:

Is there some shorter Time method to format time for a (MySQL) datetime
field that I’ve missed? The magic of created_at/updated_at does not seem
to work with fixtures.

created_at: <%= 5.days.ago.to_s(:db) %>
updated_at: <%= 18.minutes.ago.to_s(:db) %>

You can add custom Time and Date strftime formats by adding key/value
pairings (in e.g. your config/environment.rb) to the
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS and
ActiveSupport::CoreExtensions::date::Conversions::DATE_FORMATS hashes.

For example:
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!
:day => ‘%Y-%m-%d’

marcel