Skip to content

Translate query string to Activerecord query parameters

License

Notifications You must be signed in to change notification settings

reinaldooli/saringan

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Saringan · Build Status GitHub license Gem Version

Translate query strings to activerecord query parameters.

Installation

Add this line to your application's Gemfile:

gem 'saringan'

And then execute:

$ bundle

Or install it yourself as:

$ gem install saringan

Usage

Use Saringan::Translator to translate query string to activerecord parameters:

irb> query = "name::John"
irb> Saringan::Translator.translate(query)
{ name: 'John' }

Operators

Obs.: This is a preliminar version, so others operators will be implemented in future versions.

Equals

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' }

Parsers

Obs.: This is a preliminar version, so others parsers will be implemented in future versions.

String Parser

Just pass value without any qualifier

Date Time Parser

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 }
Range

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 }

License

The gem is available as open source under the terms of the MIT License.

About

Translate query string to Activerecord query parameters

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages