Class: LcpRuby::Widgets::DateGrouper
- Inherits:
-
Object
- Object
- LcpRuby::Widgets::DateGrouper
- 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
- #group ⇒ Object
-
#initialize(scope, field:, period:) ⇒ DateGrouper
constructor
A new instance of DateGrouper.
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
#group ⇒ Object
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 |