Translate query strings to activerecord query parameters.
Add this line to your application's Gemfile:
gem 'saringan'
And then execute:
$ bundle
Or install it yourself as:
$ gem install saringan
Use Saringan::Translator to translate query string to activerecord parameters:
irb> query = "name::John"
irb> Saringan::Translator.translate(query)
{ name: 'John' }
Obs.: This is a preliminar version, so others operators will be implemented in future versions.
This handle absolute equals query like "WHERE name = 'John'". To do it you must use Saringan equals operator '::'
In URL: http://site.com?query=name::John
In code:
irb> query = params[:query]
irb> Saringan::Translator.translate(query)
{ name: 'John' }
Obs.: This is a preliminar version, so others parsers will be implemented in future versions.
Just pass value without any qualifier
Pass value using Saringan Date Time qualifier. Saringan will parse value to DateTime object. It expect date time in '%Y-%m-%d %H:%M:%S' format, see:
In URL: http://site.com?query=starts_at::dt[2018-06-30 00:00:00]
In code:
irb> query = params[:query]
irb> Saringan::Translator.translate(query)
{ starts_at: DateTime object }
Pass value using Saringan range operator '|'
In URL: http://site.com?query=starts_at::dt[2018-06-30 00:00:00|2018-06-30 23:59:59]
In code:
irb> query = params[:query]
irb> Saringan::Translator.translate(query)
{ starts_at: DateTime object..DateTime object }
The gem is available as open source under the terms of the MIT License.