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

Disable find method with a configuration option. #22

Merged
merged 1 commit into from
Feb 16, 2017
Merged

Disable find method with a configuration option. #22

merged 1 commit into from
Feb 16, 2017

Conversation

taranda
Copy link
Contributor

@taranda taranda commented Feb 12, 2017

As noted in issue 21, when using alphabets that contain numerals, a valid hashid could be identical to a valid id. This can cause the find method to behave unpredictable and lead to subtle bugs at runtime.

This pull request adds a configuration option, config.disable_find which will disable the hashid feature of the find method forcing find to behave in its default manner and to not accept hashids. All arguments passed to find will be assumed to be normal ids and will not be unhashed.

This pull request should resolve issue 21.

@taranda taranda mentioned this pull request Feb 12, 2017
@jcypret
Copy link
Owner

jcypret commented Feb 12, 2017

@taranda Thanks for putting this together! The code looks good to me. Once the Codacy style issues are corrected, I'd be happy to merge it in.

@@ -63,7 +63,9 @@ def decode_id(ids)
end

def find(hashid)
model_reload? ? super(hashid) : super(decode_id(hashid) || hashid)
(model_reload? || Hashid::Rails.configuration.disable_find) ?
Copy link
Owner

Choose a reason for hiding this comment

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

Just switching this to a regular if/else would be good 👍

README.md Outdated
in some cases, and as an id in others. This unpredictably is usually not desired and can lead to subtle bugs appearing at runtime

In order to avoid this problem, users can add `config.disable_find = true` to their initializer. This will disable the hashid
functionality of the `find` method and force `find` to only accpet normal (unhashed) ids. Under this configuration, programmers
Copy link
Contributor

Choose a reason for hiding this comment

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

"accept"

end
# 100024 was selected because it unhashes to 187412
model = FakeModel.create!
model.update(id: 100_024)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could I suggest update! instead, so this fails loudly if the model can't be updated?

@olliebennett
Copy link
Contributor

olliebennett commented Feb 13, 2017

Nice one; this seems like a great solution to avoid excessive use of find_by_id! and to make sure the developer is explicit when they want to find_by_hashid. 👍

@taranda
Copy link
Contributor Author

taranda commented Feb 13, 2017

Thanks for the feedback ollie. Update pushed.

@jcypret jcypret merged commit f1c54f9 into jcypret:master Feb 16, 2017
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.

Disable find(hashid)
3 participants