-
Notifications
You must be signed in to change notification settings - Fork 48
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
Disable find method with a configuration option. #22
Conversation
@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. |
lib/hashid/rails.rb
Outdated
@@ -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) ? |
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.
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 |
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.
"accept"
spec/hashid/rails_spec.rb
Outdated
end | ||
# 100024 was selected because it unhashes to 187412 | ||
model = FakeModel.create! | ||
model.update(id: 100_024) |
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.
Could I suggest update!
instead, so this fails loudly if the model can't be updated?
Nice one; this seems like a great solution to avoid excessive use of |
Thanks for the feedback ollie. Update pushed. |
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 thefind
method forcingfind
to behave in its default manner and to not accept hashids. All arguments passed tofind
will be assumed to be normal ids and will not be unhashed.This pull request should resolve issue 21.