Skip to content

Change how constant name is found#30

Merged
doliveirakn merged 3 commits intoclio:masterfrom
rhemsing:constant-lookup
Feb 20, 2020
Merged

Change how constant name is found#30
doliveirakn merged 3 commits intoclio:masterfrom
rhemsing:constant-lookup

Conversation

@rhemsing
Copy link
Copy Markdown
Contributor

@rhemsing rhemsing commented Feb 19, 2020

Use compute_type instead of safe_constantize to 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_constantize
compute_type: https://apidock.com/rails/ActiveRecord/Inheritance/ClassMethods/compute_type

This also fixes a typo in a spec that failed with this change.

@rhemsing rhemsing requested a review from doliveirakn February 19, 2020 16:59
def retrieve_polymorphic_type_mapping(polymorphic_type:, class_name:)
return if polymorphic_type.nil?

belongs_to_class = class_name.safe_constantize
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get a spec that might help exercise the changed code?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 doliveirakn merged commit 22755f2 into clio:master Feb 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants