class Chartable::Queries::Yearly

Public Class Methods

call(scope, on:, order:) click to toggle source

It returns analytics data for the yearly period. Example output: `{ 2017 => 1, 2018 => 1 }`

@return [Hash]

# File lib/chartable/queries/yearly.rb, line 8
def self.call(scope, on:, order:)
  if ActiveRecord::Base.connection.class.to_s.match(/postgresql/i)
    scope.group(Arel.sql("cast(to_char(#{on},'YYYY') as integer)")).order(Arel.sql("cast(to_char(#{on},'YYYY') as integer) #{order}")).size
  else
    scope.group(Arel.sql("YEAR(#{on})")).order(Arel.sql("YEAR(#{on}) #{order}")).size
  end
end