-
-
Notifications
You must be signed in to change notification settings - Fork 909
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 association strict_loading option #1607
Conversation
`strict_loading` was added to [Rails 6.1] to prevent lazy loading of associations. As adding it to an association declaration can have a massive impact on the way the record and its association is treated, it can be useful to ensure in a test suite the presence of this option. This adds support for adding the `strict_loading` option to an association declaration. [Rails 6.1]: rails/rails#37400 Co-authored-by: Jose Blanco @laicuRoot <jose.blanco@thoughtbot.com>
That's a great contribution! Thanks for working on this. The
We should consider that when making the check in the |
# # RSpec | ||
# RSpec.describe Organization, type: :model do | ||
# it { should have_many(:people).strict_loading(true) } | ||
# end | ||
# | ||
# # Minitest (Shoulda) | ||
# class OrganizationTest < ActiveSupport::TestCase | ||
# should have_many(:people).strict_loading(true) | ||
# end | ||
# |
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.
We could add to the documentation that we can use strict_loading
without specifying the argument and it'll default to true
.
Thank you for your review, @matsales28 It got into our mind to deal with more possible situations with this option, as you mentioned. We might be missing something, so please forgive us if the issue is written as "bug report" if it's not a bug. I'm happy to discuss the situation, and make the necessary changes to this PR based on the outcome of our discussion. |
We have started working on covering all possible combinations but it's going to take a while, so I'm moving the PR to draft again. |
This PR adds the tests and logic for support at model level for strict_loading.
Co-authored-by: Rémy Hannequin <remy@thoughtbot.com>
I'm reopening the PR as we have a working solution for all the cases. We have added the logic in the |
Thanks for adding the logic to handle all the cases. It's very much appreciated!
I think we could split this logic into new methods on diff --git a/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb b/lib/shoulda/matchers/active_record
/association_matchers/model_reflection.rb
index 92e1f394..a415d84c 100644
--- a/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb
+++ b/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb
@@ -13,13 +13,7 @@ module Shoulda
end
def associated_class
- associated_class = reflection.klass
-
- if subject.strict_loading_by_default && !(reflection.options[:strict_loading] == false)
- reflection.options[:strict_loading] = true
- end
-
- associated_class
+ reflection.klass
end
def polymorphic?
@@ -76,6 +70,10 @@ module Shoulda
reflection.options[:through]
end
+ def strict_loading?
+ reflection.options.fetch(:strict_loading, subject.strict_loading_by_default)
+ end
+
protected
attr_reader :reflection, :subject
diff --git a/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb b/lib/shoulda/matchers/active_record/
association_matchers/option_verifier.rb
index ecf7818b..15cf0032 100644
--- a/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb
+++ b/lib/shoulda/matchers/active_record/association_matchers/option_verifier.rb
@@ -122,6 +122,10 @@ module Shoulda
reflector.associated_class
end
+ def actual_value_for_strict_loading
+ reflection.strict_loading?
+ end
+ What I'm doing is basically creating a method specific for checking the value for strict loading, and that method basically uses a new method on the |
@@ -1199,6 +1199,409 @@ def belonging_to_non_existent_class(model_name, assoc_name, options = {}) | |||
expect(Parent.new).to have_many(:children) | |||
end | |||
|
|||
context 'when the application is configured with strict_loading disabled by default' do |
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.
What do you think about nesting all those specs under the same context? When trying this branch, I wrapped everything in a context 'strict_loading'
to run the specs in isolation, it also helps to identify between all the other features presents in the association_matcher_spec
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.
I left two comments after adjusting those comments, it's ready for merge to me!
- Refactor verifier logic - Nest all specs in one `describe` block Co-authored-by: Jose Blanco <jose.blanco@thoughtbot.com>
f3c02f3
to
75b2c5c
Compare
@matsales28 Thanks a lot for helping us with a suggested change. We just applied it and also nested all specs inside a single |
strict_loading
was added to Rails 6.1 to prevent lazy loading of associations.As adding it to an association declaration can have a massive impact on the way the record and its association is treated, it can be useful to ensure in a test suite the presence of this option.
This adds support for adding the
strict_loading
option to an association declaration.Co-authored-by: Jose Blanco @laicuRoot jose.blanco@thoughtbot.com