New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Jan 10, 2017

Conversation

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

This comment has been minimized.

rails-bot commented Jan 9, 2017

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.

activesupport/lib/active_support/testing/time_helpers.rb Outdated
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) }

This comment has been minimized.

@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.

This comment has been minimized.

@pixeltrix

pixeltrix Jan 9, 2017

Member

How about jd(now.to_date.jd) ?

This comment has been minimized.

@matthewd

matthewd Jan 9, 2017

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)

This comment has been minimized.

@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.

This comment has been minimized.

@jnicklas

jnicklas Jan 10, 2017

Contributor

Updated! Thanks for the hint.

activesupport/lib/active_support/testing/time_helpers.rb Outdated
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) }

This comment has been minimized.

@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)) }

This comment has been minimized.

@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.

This comment has been minimized.

@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.

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.

@jnicklas jnicklas force-pushed the jnicklas:time-travel-with-subclasses branch to 3d78949 Jan 10, 2017

@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

This comment has been minimized.

Member

pixeltrix commented Jan 10, 2017

@jnicklas thanks! 👍

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

@pixeltrix

This comment has been minimized.

Member

pixeltrix commented Jan 10, 2017

Backported to 5-0-stable in 477ae8d

@pixeltrix

This comment has been minimized.

Member

pixeltrix commented Jan 10, 2017

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