Skip to content
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

Fix .value_for to not search for constants in ancestors #97

Conversation

diego-silva
Copy link
Contributor

Right now .value_for might return any constant that exists in the enum ancestors.

This causes some weird behaviors like:
MyEnum.value_for('Module') to return the Module class.

This PR fixes this behavior by passing false to the optional inherit argument of .const_get.

Docs here: https://ruby-doc.org/core-2.5.1/Module.html#method-i-const_get

Right now .value_for might return any constant
that exists in the enum ancestors.

This causes some weird behaviors like:
MyEnum.value_for('Module') to return the Module
class.

This commit fixes this behavior by passing false
to the optional inherit argument of .const_get.
Copy link
Owner

@lucascaton lucascaton left a comment

Choose a reason for hiding this comment

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

Thanks, @diego-silva!

I'm curious to know in what scenario exactly this was causing issues (if you don't mind sharing 🙂).

@lucascaton lucascaton merged commit 0ebb435 into lucascaton:main Aug 5, 2021
@lucascaton
Copy link
Owner

Hey @diego-silva - I've released version 3.2.1 with this fix: https://rubygems.org/gems/enumerate_it/versions/3.2.1.

@diego-silva diego-silva deleted the do-not-search-enum-values-in-ancestors branch August 5, 2021 15:35
@diego-silva
Copy link
Contributor Author

Hey @lucascaton, thanks for the very fast review and release 🙌

So, we have this external integration that we call and we store some of its information back on our side, and we are using enumerate_it to map a field from their response to our database.

This is an optional field, so we expect to not always have its value, and this might happen either because it was indeed missing on their response, or because they added something new that we don't yet support.
In case this is a new value, value_for would return nil, we would write a log (to check if we want to support this new value), and everything else would work 'the same way' as if it was missing.

This process has been working fine, except that they added a value called "API" and value_for was now returning our API module class that we use to scope our API controllers on the Rails app, and then it would fail on the model validations when trying to store that info 😅

Everything is solved now, thanks again. 🙏

@lucascaton
Copy link
Owner

Gotcha, thanks for explaining, @diego-silva.
It's great to hear that it working as expected now, thanks again for the PR! 😃

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.

2 participants