As you may now there are two hard things in computer science and one of them is naming things.
And although naming things is an art, is not bad to have a little guide on how to do it.
That’s why I want to share with you a guide on how to name scopes that depend on a single column based on its type.
This suggestion is based on a very interesting gem called type_scopes, that I found on reddit.
I think that even if you don’t use the gem you can benefit from the names that it is proposing. Maybe it won’t fit all cases but I think that it could make our work a little easier.
Record.received_to("2017-09-06")
# => where("received_on <= '2017-09-06'")
Record.received_from("2017-09-06")
# => where("received_on >= '2017-09-06'")
Record.received_after("2017-09-06")
# => where("received_on > '2017-09-06'")
Record.received_before("2017-09-06")
# => where("received_on < '2017-09-06'")
Record.received_between("2017-09-06", "2017-09-07")
# => where("received_on BETWEEN '2017-09-06' AND '2017-09-07'")
Record.received_not_between("2017-09-06", "2017-09-07")
# => where("received_on NOT BETWEEN '2017-09-06' AND '2017-09-07'")
Record.received_within("2017-09-06", "2017-09-07")
# => where("received_on > '2017-09-06' AND received_on < '2017-09-07'")
Record.received_not_within("2017-09-06", "2017-09-07")
# => where("received_on <= '2017-09-06' OR received_on >= '2017-09-07'")
Record.completed_to("2017-09-06")
# => where("completed_at <= '2017-09-06'")
Record.completed_from("2017-09-06")
# => where("completed_at >= '2017-09-06'")
Record.completed_after("2017-09-06")
# => where("completed_at > '2017-09-06'")
Record.completed_before("2017-09-06")
# => where("completed_at < '2017-09-06'")
Record.completed_between("2017-09-06", "2017-09-07")
# => where("completed_at BETWEEN '2017-09-06' AND '2017-09-07'")
Record.completed_not_between("2017-09-06", "2017-09-07")
# => where("completed_at NOT BETWEEN '2017-09-06' AND '2017-09-07'")
Record.completed_within("2017-09-06", "2017-09-07")
# => where("completed_at > '2017-09-06' AND completed_at < '2017-09-07'")
Record.completed_not_within("2017-09-06", "2017-09-07")
# => where("completed_at <= '2017-09-06' OR completed_at >= '2017-09-07'")
Record.price_to(100)
# => where("price <= 100")
Record.price_from(100)
# => where("price >= 100")
Record.price_above(100)
# => where("price > 100")
Record.price_below(100)
# => where("price < 100")
Record.price_between(100, 200)
# => where("price BETWEEN 100 AND 200")
Record.price_not_between(100, 200)
# => where("price NOT BETWEEN 100 AND 200")
Record.price_within(100, 200)
# => where("price > 100 AND price < 200")
Record.price_not_within(100, 200)
# => where("price <= 100 OR price >= 200")
Record.name_contains("foo")
# => where("name LIKE '%foo%'")
Record.name_contains("foo", sensitive: false)
# => where("name ILIKE '%foo%'")
Record.name_starts_with("foo")
# => where("name LIKE 'foo%'")
Record.name_starts_with("foo", sensitive: false)
# => where("name ILIKE 'foo%'")
Record.name_ends_with("foo")
# => where("name LIKE '%foo'")
Record.name_ends_with("foo", sensitive: false)
# => where("name ILIKE '%foo'")
Record.name_like("%foo%")
# => where("name LIKE '%foo%'")
Record.name_ilike("%foo%")
# => where("name ILIKE '%foo%'")
Record.completed
# => where("completed = true")
Record.not_completed
# => where("completed = false")
Record.is_native
# => where("is_native = true")
Record.is_not_native
# => where("is_native = false")
Record.has_invitation
# => where("has_invitation = true")
Record.has_not_invitation
# => where("has_invitation = false")
Record.was_terminated
# => where("was_terminated = true")
Record.was_not_terminated
# => where("was_terminated = false")
Here I try to share knowledge and fixes to common problems and struggles for ruby on rails developers, like How to fetch the latest-N-of-each record or How to test that an specific mail was sent or a Capybara cheatsheet. You can see more examples on Most recent posts or all posts.