Class: Chartable::Queries::Yearly

Inherits:
Object
  • Object
show all
Defined in:
lib/chartable/queries/yearly.rb

Class Method Summary collapse

Class Method Details

.call(scope, on:, order:) ⇒ Hash

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

Returns:

  • (Hash)


8
9
10
11
12
13
14
# 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