lib/groupdate/scopes.rb in groupdate-2.4.0 vs lib/groupdate/scopes.rb in groupdate-2.5.0

- old
+ new

@@ -1,8 +1,7 @@ module Groupdate module Scopes - Groupdate::FIELDS.each do |field| define_method :"group_by_#{field}" do |*args| args = args.dup options = args[-1].is_a?(Hash) ? args.pop : {} options[:time_zone] ||= args[1] unless args[1].nil? @@ -10,7 +9,16 @@ Groupdate::Magic.new(field, options).relation(args[0], self) end end + def group_by_period(period, field, options = {}) + # to_sym is unsafe on user input, so convert to strings + permitted_periods = ((options[:permit] || Groupdate::FIELDS).map(&:to_sym) & Groupdate::FIELDS).map(&:to_s) + if permitted_periods.include?(period.to_s) + send("group_by_#{period}", field, options) + else + raise ArgumentError, "Unpermitted period" + end + end end end