-
Notifications
You must be signed in to change notification settings - Fork 901
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
Support belongs_to_required_by_default #985
Conversation
Test failures are due to the CI using older versions of the gems (Rails 4.2), due to there being no Gemfile.lock. |
Hi Joel, thanks for the contribution!
|
@jaredbeck The gem is needed for Rails 5 backwards compatibility with the controller tests, specifically the The This isn't showing up in CI because, it seems to me, Travis is including the Rails 4.2 gems instead of Rails 5.0 or 5.1. |
Gemfile
Outdated
@@ -1,2 +1,3 @@ | |||
source "https://rubygems.org" | |||
gemspec | |||
gem "rails-controller-testing" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Re: The rails-controller-testing gem: Why do we need it? We haven't needed it before. ...
Oops, I was wrong about this, we already are using the rails-controller-testing
gem. Please see our Appraisals
file. No changes to our Gemfile
are necessary.
Sorry about my confusion. I removed the commit that edited the Gemfile and added some version conditionals where necessary. Seems to pass in all versions for me locally, so I suspect it'll pass on CI soon. |
I updated the commit message and renamed this PR for improved clarity. |
Note that it now prints out this warning message in AR 5.0 and 5.1 appraisal specs:
|
Joel, thanks for addressing my concerns re: the Now, let me see if I understand the changes that remain in this PR.
Do you get a
Earlier, I asked:
Did this question get answered?
Please include a test that demonstrates this issue. Use an existing test model in
Based only on the release notes I quoted above, I agree that adding Please add an entry to the changelog, under Unreleased -> Fixed. Do we need to add
Well that's a problem. We don't want our test suite printing any deprecation warnings. First, why is the warning printed? Clearly, because of the changes to Thanks again for your work on this. |
On second thought, is it possible that many tests demonstrate this issue once your change to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Various concerns given in comment 324822869: #985 (comment)
It wasn't too hard after I figured out how to use appraisal. I didn't look at the guide until you mentioned it just now; it seems fairly straight-forward to me.
Yes, I get that error, and I'm referring to that change.
I didn't answer it. According to the API documentation: "When set to
Yes, that is the case. It's why I changed the models I did, so that the tests would pass. If you remove any of those changes, there will be test failures. That's the only reason I didn't add a new test, but I'm not totally against making a new one anyway. Just not sure if it's necessary.
It's printed because we now have this configuration option enabled. I'm not really sure why the
I am happy to do it, and it's in my interest, as this is acting as a blocker for enabling this default in our code. Thank you for your interest in this PR. |
I've made some updates to this PR. To avoid the deprecation warning, it will now check the version and config setting prior to defining and testing the :after callback. I also added a description of the fix to the Changelog. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Joel, this is quite close, just a few nit-picks left, thanks.
CHANGELOG.md
Outdated
@@ -1,3 +1,4 @@ | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove blank line
CHANGELOG.md
Outdated
@@ -15,7 +16,7 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/). | |||
|
|||
### Fixed | |||
|
|||
- None | |||
- The "item" association will no longer be required when belongs_to_required_by_default is enabled. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please follow the convention of this file and put in a link to the PR, so people can find out more about the change if they have questions.
@@ -31,7 +31,12 @@ class Application < Rails::Application | |||
if v >= Gem::Version.new("4.2") && v < Gem::Version.new("5.0.0.beta1") | |||
config.active_record.raise_in_transactional_callbacks = true | |||
end | |||
if v >= Gem::Version.new("5.0.0.beta1") | |||
if v >= Gem::Version.new("5.0.0.beta1") && v < Gem::Version.new("5.1") | |||
config.active_record.belongs_to_required_by_default = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not load_defaults("5.0")
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently this command was added in Rails 5.1.
spec/dummy_app/config/application.rb
Outdated
config.active_record.time_zone_aware_types = [:datetime] | ||
end | ||
if v >= Gem::Version.new("5.1") | ||
config.load_defaults 5.1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is bordering on annoyingly pedantic, I'm sure, but we should not use a Float
here. Float#to_s
is not predictable. Please use string "5.1"
. Thanks.
5.1.to_s
# => "5.1"
5.99999999999999999999999999999999999999999999999999999.to_s
=> "6.0"
I know, I know, it's not relevant, how likely are we to hit a floating point issue in a rails version number? But, I can't help it. :)
Since Rails 5.0, belongs_to_required_by_default has been the official ActiveRecord default. Add the configuration lines necessary to enable this default in both 5.0 and 5.1. Add "optional: true" where necessary to fix spec failures caused by this change. Add version-checking conditionals where necessary. Update the Changelog appropriately.
I believe I have corrected all of the reported issues. Please let me know if anything else is necessary. Thanks! |
Looks good, thanks Joel. |
We're trying to upgrade our app to use the Rails 5.1 defaults, but we ran into a problem with PaperTrail. Specifically, when we try to soft delete certain records (i.e., mark them as deleted without actually deleting the row from the DB), and then try to make a change to the soft-deleted record, it raises an error saying the versions are invalid. This is because of the
belongs_to :item
on VersionConcern, which makes the association required with the new defaults. To stay compatible with the old way and allow deleting and soft deleting of records, we should make this association optional.An unrelated commit adds the
rails-controller-testing
gem to the Gemfile. I tried adding it as a development dependency in the gemspec, but couldn't get it to work. Without this gem, a lot of the tests fail for me.