Change how constant name is found#30
Merged
doliveirakn merged 3 commits intoclio:masterfrom Feb 20, 2020
Merged
Conversation
doliveirakn
reviewed
Feb 19, 2020
| def retrieve_polymorphic_type_mapping(polymorphic_type:, class_name:) | ||
| return if polymorphic_type.nil? | ||
|
|
||
| belongs_to_class = class_name.safe_constantize |
Contributor
There was a problem hiding this comment.
Can we get a spec that might help exercise the changed code?
Contributor
Author
There was a problem hiding this comment.
That would be good. I'll look into it.
| return if polymorphic_type.nil? | ||
|
|
||
| belongs_to_class = class_name.safe_constantize | ||
| belongs_to_class = compute_type(class_name) |
Contributor
There was a problem hiding this comment.
safe_constantize returns nil when it can't find a constant, but compute_type raises a NameError. I suppose this exception would happen at some point in the process of setting up/accessing an association, but are there any other assumptions in our extension that need to change as a result?
Contributor
There was a problem hiding this comment.
I can't think of a situation where it would return nil, and this gets called during class loading so I think if things work in an eager loaded environment, we should be good.
doliveirakn
approved these changes
Feb 20, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Use
compute_typeinstead ofsafe_constantizeto get the class. This will try to resolve the module to the current module so it can resolve the constant without explicitly adding the module name.For reference:
safe_constantize: https://apidock.com/rails/ActiveSupport/Inflector/safe_constantizecompute_type: https://apidock.com/rails/ActiveRecord/Inheritance/ClassMethods/compute_typeThis also fixes a typo in a spec that failed with this change.