Make time travel work with subclasses of Time/Date/Datetime #27616

Merged
merged 1 commit into from Jan 10, 2017

Projects

None yet

6 participants

@jnicklas
Contributor
jnicklas commented Jan 9, 2017

Closes #27614

Previously when calling now on a subclass of e.g. Time it would return an instance of Time instead of returning an instance of the subclass. This way, we always return the correct class.

@rails-bot

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @rafaelfranca (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

- simple_stubs.stub_object(Date, :today, now.to_date)
- simple_stubs.stub_object(DateTime, :now, now.to_datetime)
+ simple_stubs.stub_object(Time, :now) { at(now.to_i) }
+ simple_stubs.stub_object(Date, :today) { parse(now.to_date.to_s) }
@jnicklas
jnicklas Jan 9, 2017 Contributor

There is probably a more performant way of implementing this, but I figured that this way is simple and the performance of it probably doesn't matter.

@pixeltrix
pixeltrix Jan 9, 2017 Member

How about jd(now.to_date.jd) ?

@matthewd
matthewd Jan 9, 2017 edited Member

This one looks like it could use jd. I don't think DateTime has any similar single-number in+out representation, though.

Edit: Or I could notice that @pixeltrix said the same thing several hours ago 😅 (I swear GH used to be better about live-updating new comments)

@pixeltrix
pixeltrix Jan 10, 2017 Member

I don't think DateTime has any similar single-number in+out representation, though.

@matthewd DateTime.jd can take a Rational with the fraction part representing the time as a fraction of a day. Unfortunately there's no easy way to get that number - it may be worth adding such a method to Active Support.

@jnicklas
jnicklas Jan 10, 2017 Contributor

Updated! Thanks for the hint.

- simple_stubs.stub_object(DateTime, :now, now.to_datetime)
+ simple_stubs.stub_object(Time, :now) { at(now.to_i) }
+ simple_stubs.stub_object(Date, :today) { parse(now.to_date.to_s) }
+ simple_stubs.stub_object(DateTime, :now) { parse(now.to_datetime.to_s) }
@pixeltrix
pixeltrix Jan 9, 2017 Member

For DateTime you can use Rational, e.g:

simple_stubs.stub_object(DateTime, :now) { jd(now.to_datetime.jd + Rational(now.seconds_since_midnight, 86400)) }
@jnicklas
jnicklas Jan 10, 2017 Contributor

I tried this but it seems that Time zone information is lost this way. I found a similar implementation which does preserve time zone implementation though.

@pixeltrix
pixeltrix Jan 10, 2017 Member

seems that Time zone information is lost this way

Ah, that would make sense since a Julian Day is in Universal Time.

@jnicklas jnicklas Make time travel work with subclasses of Time/Date/Datetime
Closes #27614

Previously when calling `now` on a subclass of e.g. `Time` it would return an instance of `Time` instead of returning an instance of the subclass. This way, we always return the correct class.
3d78949
@pixeltrix pixeltrix merged commit a02d47d into rails:master Jan 10, 2017

2 checks passed

codeclimate no new or fixed issues
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@pixeltrix
Member

@jnicklas thanks! 👍

@jnicklas jnicklas deleted the jnicklas:time-travel-with-subclasses branch Jan 10, 2017
@pixeltrix
Member

Backported to 5-0-stable in 477ae8d

@pixeltrix
Member

Also managed to backport it to 4-2-stable in d7ac341 - had to adjust the stubbing in the tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment