Overview

ReportsKit provides a number of easy, powerful form filters that are commonly needed to allow users to interactively filter reports. As shown below, when you call render_report, you can pass it a block and render any form filters using any positioning and styling that you want.

For example, when you call f.multi_autocomplete :author, ReportsKit will render a multi-autocomplete input. When you call f.date_range :created_at, it will render a date range picker.


To create the chart above, simply add the following YAML file and call render_report in any view:

YAML

config/reports_kit/reports/interaction_filters_overview.yml

measure: post
filters:
- author
- title
- is_featured
- created_at
dimensions:
- created_at
- author
chart:
  options:
    scales:
      xAxes:
      - stacked: true
      yAxes:
      - stacked: true

Note: The "chart.options" option is passed to Chart.js, so any values supported by Chart.js are supported here, too.


View

app/views/my_controller/my_view.html.haml

= render_report 'interaction_filters_overview' do |report|
  = report.form do |f|
    .pull-right
      = f.date_range :created_at
    = f.multi_autocomplete :author, placeholder: 'Author...'
    = f.string_filter :title, placeholder: 'Title (e.g. The)...', style: 'width: 175px;'
    .checkbox
      = label_tag :is_featured do
        = f.check_box :is_featured
        Featured

Model

app/models/post.rb

class Post < ApplicationRecord
  belongs_to :author
  has_many :post_views, dependent: :destroy
  has_many :posts_tags, dependent: :destroy
  has_many :tags, through: :posts_tags

  include ReportsKit::Model
  reports_kit do
    aggregation :average_time_to_publish, [:average, 'posts.published_at - posts.created_at']
    contextual_filter :for_author, ->(relation, context_params) { relation.where(author_id: context_params[:author_id]) }
    dimension :approximate_views_count, group: 'ROUND(posts.views_count, -1)'
    filter :is_published, :boolean, conditions: ->(relation) { relation.where(status: 'published') }
  end

  STATUSES = %w(draft private published).freeze

  def to_s
    title
  end
end

Model's Columns

id              integer
author_id       integer
title           string
status          string
published_at    datetime
is_featured     boolean
views_count     integer
created_at      datetime
updated_at      datetime