Class: Chartable::Queries::Yearly
- Inherits:
-
Object
- Object
- Chartable::Queries::Yearly
- Defined in:
- lib/chartable/queries/yearly.rb
Class Method Summary collapse
-
.call(scope, on:, order:) ⇒ Hash
It returns analytics data for the yearly period.
Class Method Details
.call(scope, on:, order:) ⇒ Hash
It returns analytics data for the yearly period. Example output: ‘{ 2017 => 1, 2018 => 1 }`
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 |