thoughtbot / shoulda-matchers

Simple one-liner tests for common Rails functionality
https://matchers.shoulda.io
MIT License
3.5k stars 911 forks source link

Allow beginless and endless ranges in validates_inclusion_of #1615

Closed pjpires closed 6 months ago

pjpires commented 6 months ago

Problem

Currently, the min and max range values are checked with range.first and range.max, that will fail with cannot get the first element of beginless range (RangeError) and cannot get the maximum of endless range (RangeError) respectively for beginless/endless ranges.

Proposed solution

We can rely on range.begin and range.end (by correctly checking if end should be considered by looking at exclude_end?) instead, that will both return nil 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.