Forum: Ruby on Rails How to DRY with Fixtures (helper or extend Time class, how)?

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.
Henrik (Guest)
on 2006-01-07 12:21
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".
Marcel Molina Jr. (Guest)
on 2006-01-07 21:34
(Received via mailing list)
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
This topic is locked and can not be replied to.