Class: Chartable::Queries::Quarterly

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

Class Method Summary collapse

Class Method Details

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

It returns analytics data for the quarterly period. Example output: ‘2018” => 1, “Q2 2018” => 1`

Returns:

  • (Hash)


8
9
10
11
12
13
14
# File 'lib/chartable/queries/quarterly.rb', line 8

def self.call(scope, on:, order:)
  if ActiveRecord::Base.connection.class.to_s.match(/postgresql/i)
    scope.group(Arel.sql("concat('Q', to_char(#{on},'Q YYYY'))")).order(Arel.sql("concat('Q', to_char(#{on},'Q YYYY')) #{order}")).size
  else
    scope.group(Arel.sql("CONCAT('Q', QUARTER(#{on}), DATE_FORMAT(#{on},' %Y'))")).order(Arel.sql("CONCAT('Q', QUARTER(#{on}), DATE_FORMAT(#{on},' %Y')) #{order}")).size
  end
end