Allow beginless and endless ranges in validates_inclusion_of #1615
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.
Problem
Currently, the min and max range values are checked with
range.first
andrange.max
, that will fail withcannot get the first element of beginless range (RangeError)
andcannot get the maximum of endless range (RangeError)
respectively for beginless/endless ranges.Proposed solution
We can rely on
range.begin
andrange.end
(by correctly checking ifend
should be considered by looking atexclude_end?
) instead, that will both returnnil
instead of raising for beginless/endless ranges.Also added the required early returns on minimum/maximum value checks when the begin/end of range is absent and updated the specs with two scenarios for beginless/endless ranges.
Fixes #1614.