Project

chartable

0.04
No commit activity in last 3 years
No release in over 3 years
A lightweight and database-level library to transform any Active Record query into analytics data ready for any chart
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 4.0.0
~> 0
>= 3.7.0, ~> 3.7

Runtime

~> 0.10
 Project Readme

The Chartable Ruby gem

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

Installation

gem install chartable

Usage examples

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)

Sorting

With the 0.0.0.3 version is possible to sort results:

User.analytics(:yearly, order: 'desc') # or order: 'asc'

Supported Ruby Versions

This gem was tested on the 2.5.0 version. If it's not working with older versions please add a new issue.

Committers and Contributors

TODO

  • Add the CHANGELOG file
  • Add a separated query class for each database driver
  • When a date period is restricted then return 0 for periods without the data
  • Test code with the older Ruby versions

Copyright

Copyright (c) 2018 Paweł Dąbrowski. See LICENSE for details.