Module: Groupdate::QueryMethods

Defined in:
lib/groupdate/query_methods.rb

Instance Method Summary collapse

Instance Method Details

#group_by_period(period, field, permit: nil, **options) ⇒ Object


15
16
17
18
19
20
21
22
23
# File 'lib/groupdate/query_methods.rb', line 15

def group_by_period(period, field, permit: nil, **options)
  # to_sym is unsafe on user input, so convert to strings
  permitted_periods = ((permit || Groupdate::PERIODS).map(&:to_sym) & Groupdate::PERIODS).map(&:to_s)
  if permitted_periods.include?(period.to_s)
    send("group_by_#{period}", field, **options)
  else
    raise ArgumentError, "Unpermitted period"
  end
end