WhereChain
In Rails, we usually use Active Record, which allows us to escape from writing SQL code like this SELECT * FROM posts WHERE posts.name = 'Foo'
and allows us to write Post.where(name: 'Foo')
instead. However, this has always been limited to matching equality, so you still have to write Post.where('comments > ?', 5)
to get Posts that have more than 5 comments.
In the older versions, you also had to write Post.where('name IS NOT null')
to do a negation. Rails 4.0 added a class called WhereChain
that added some new possibilities, one of which was a not
method. The proper way to write became Post.where.not(name: nil)
instead.
Within the same commit there were also two new methods that didn't survive to the release of Rails 4.0 - .like
and .not_like
. As you can read in this commit discussion, there has been work made to bring them back, like the activerecord-like gem or Squeel, but these have their own problems - activerecord-like only adds .like
and .not_like
back and the latest version is locked to Active Record 5; and Squeel provides a whole new query DSL, which not everyone will like. There was actually a pull request adding .gt
and other inequality methods, which was closed even faster than the first one.
This gem brings these two methods back and extends WhereChain with additional methods: .gt
, .gte
, .lt
and .lte
, so that by using it you can replace the SQL strings like Post.where('comments > 5')
with Post.where.gt(comments: 5)
.
WhereChain depends on the Active Record gem in a version higher than 4.2. The gem is tested on the latest Ruby and all current Rails versions - 5.2, 6.0, 6.1, and 7.0. If you have an older version, it will probably still work, because Active Record doesn't really change all that often, but Rails versions older than 5.2 depend on old and unsupported Ruby versions, so I'm not making any promises there.
Usage
Some examples of using the gem and what can be replaced with it:
Rails SQL string | with WhereChain |
---|---|
Post.where('comments > ?', 5) |
Post.where.gt(comments: 5) |
Post.where('comments >= ?', 5) |
Post.where.gte(comments: 5) |
Post.where('comments < ?', 5) |
Post.where.lt(comments: 5) |
Post.where('comments <= ?', 5) |
Post.where.lte(comments: 5) |
Post.where('name LIKE ?', "%foo%") |
Post.where.like(name: "%foo%") |
Post.where('name NOT LIKE ?', "%foo%") |
Post.where.unlike(name: "%foo%") |
Post.where('comments > ? AND shares > ?', 5, 10) |
Post.where.gt({ comments: 5, shares: 10 }) |
Post.where('comments > ? OR shares > ?', 5, 10) |
Post.where.gt(comments: 5).or(Post.where.gt(shares: 10)) |
You can now also chain the methods in your Active Record queries:
Post.where.not.gt(comments: 5).where.like(name: '%foo%')
This, however, will NOT work at all:
Post.gt(comments: 5).like(name: '%foo%')
Post.where.gt(comments: 5).lt(comments: 10)
You need to prepend each of the new methods with either .where
or .where.not
for them to work.
All the methods accept a Hash attribute and can compare all proper types as values, except Arrays and Hashes. These will not work and will raise an ArgumentError exception:
Post.where.gt(comments: [1, 2, 3])
Post.where.gt(comments: { bad: :thing })
ArgumentError: The value passed to this method should be a valid type.
Post.where.gt('comments > ?', 5)
Post.where.gt([{ number: 5 }, 'name > ?'], 'abc')
ArgumentError: This method requires a Hash as an argument.
Installation
Add this line to your application's Gemfile:
gem 'where_chain'
And then execute:
$ bundle
Or install it yourself as:
$ gem install where_chain
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Make sure all the tests pass on all versions
- Create new Pull Request
Testing the code
Before running any test, you should finish installing the appraised gems:
$ bundle exec appraisal install
Running all the tests for all Rails version targets:
$ bundle exec appraisal rspec
Running tests for a specific version:
$ bundle exec appraisal 7.0 rspec
You can see all the targets to use instead of 7.0
in the Appraisals
file.
License
The gem is available as open source under the terms of the MIT License.