A lightweight and database-level Ruby library to transform any Active Record query into analytics hash ready for use with any chart library.
The Chartable gem is a lightweight and database-level library to transform any Active Record query into analytics hash ready for use with any chart library.
Supported database engines: MySQL
and PostgreSQL
gem install chartable
The gem adds the analytics
class method to all Rails models. Currently, five analytics periods are available: yearly
, weekly
, monthly
, quarterly
and daily
. If it’s not specified then it operates on the created_at
column.
User.analytics(:yearly) # => { 2017 => 5, 2018 => 30 }
User.analytics(:monthly) # => { "November 2018" => 1, "October 2018" => 1 }
User.analytics(:daily) # => {"October 09, 2018" => 1, "October 10, 2018" => 1}
User.analytics(:weekly) # => {"01/28/18 - 02/03/18" => 1, "02/11/18 - 02/17/18" => 1}
User.analytics(:quarterly) # => {"Q1 2018" => 1, "Q2 2018" => 1}
If you want to change the column used for sorting then specify the :on
option:
User.analytics(:yearly, on: 'updated_at')
If you want to narrow the results you can pass from:
or/and to:
options. You can pass any valid value of type String
, Date
, DateTime
or Time
- it will be transformed to the date format:
User.analytics(:yearly, from: Date.yesterday, to: Date.today)
You can also call analytics
on any query:
User.where(first_name: 'John').where.not(last_name: 'Doe').analytics(:daily)
With the 0.0.0.3
version is possible to sort results:
User.analytics(:yearly, order: 'desc') # or order: 'asc'
This gem was tested on the 2.5.0 version. If it’s not working with older versions please add a new issue.
Copyright © 2018 Paweł Dąbrowski.
See LICENSE for details.