jackc / tod

Time of day and shift types for Ruby
MIT License
435 stars 56 forks source link

to_s deprecated in rails 7, becomes to_fs #93

Closed michael-groble closed 1 year ago

michael-groble commented 2 years ago

Rails 7 deprecates to_s, so

Time.new.to_s(:db)
DEPRECATION WARNING: Time#to_s(:db) is deprecated. Please use Time#to_fs(:db) instead.

It would be nice to alias to_formatted_s to to_fs to maintain consistency.

Morozzzko commented 1 year ago

UPD: Thought that I faced the same issue so I sent a PR. Turns out that I wasn't having the issue. See "details" for the original message

That was an original message. Retaining it for the history This issue introduces a pretty severe bug when dealing with Rails fixtures. I'm glad it only took me ≈1hr to figure out.~~ How to reproduce: 1. Have a Rails model with an attribute, like: `attribute :time_of_day, :time_only` 2. Have a Rails fixture with `time_of_day: "12:00"` 3. Run the test which loads the fixture 4. Open `bin/rails dbconsole RAILS_ENV=test` 5. Run `select * from ` 6. You'll see that the value is 43200 instead of 12:00 The value isn't random: ``` irb(main):001:0> Tod::TimeOfDay.new(12).to_i => 43200 ``` That messed up my tests _real bad_ ~~Ran a simple test to see if the issue is related to the `to_fs` deprecation. It does. Here's what I added to make the tests pass:~~ ```ruby module Tod class TimeOfDay alias to_fs to_s end end ``` UPD: as it turns out, YAML treats `12:00` as seconds of day. Replace it with `time_of_day: !!str 12:00`