Brevity is an obvious benefit, but reliability even more so. For example, the code in the article has at least two major mistakes:
1) `from_price` will silently never be applied
2) `sort_type` and `sort_direction` defaults are mixed up and always nil, either of which will raise an exception, but only when the corresponding request params are omitted
Another benefit of using the gem: instances of the `ProductSearch` class from above can be used with the stock Rails form builder (i.e. `form_for` and `form_with model:`).
So the final code from the article would look like:
Brevity is an obvious benefit, but reliability even more so. For example, the code in the article has at least two major mistakes:1) `from_price` will silently never be applied
2) `sort_type` and `sort_direction` defaults are mixed up and always nil, either of which will raise an exception, but only when the corresponding request params are omitted
Another benefit of using the gem: instances of the `ProductSearch` class from above can be used with the stock Rails form builder (i.e. `form_for` and `form_with model:`).