Class: LcpRuby::Widgets::DateGrouper

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/widgets/date_grouper.rb

Constant Summary collapse

VALID_PERIODS =
%w[day week month quarter year].freeze

Instance Method Summary collapse

Constructor Details

#initialize(scope, field:, period:) ⇒ DateGrouper

Returns a new instance of DateGrouper.



6
7
8
9
10
11
12
13
14
# File 'lib/lcp_ruby/widgets/date_grouper.rb', line 6

def initialize(scope, field:, period:)
  @scope = scope
  @field = field
  @period = period

  if @period.present? && !VALID_PERIODS.include?(@period)
    raise ArgumentError, "Invalid period '#{@period}'. Must be one of: #{VALID_PERIODS.join(', ')}"
  end
end

Instance Method Details

#groupObject



16
17
18
19
20
21
22
# File 'lib/lcp_ruby/widgets/date_grouper.rb', line 16

def group
  if @period.present?
    @scope.group(date_trunc_sql)
  else
    @scope.group(@field)
  end
end