lib/groupdate/series.rb in groupdate-1.0.1 vs lib/groupdate/series.rb in groupdate-1.0.2
- old
+ new
@@ -1,27 +1,28 @@
module Groupdate
class Series
- def initialize(relation, field, column, time_zone, time_range)
+ def initialize(relation, field, column, time_zone, time_range, week_start)
@relation = relation
if time_range.is_a?(Range)
@relation = relation.where("#{column} BETWEEN ? AND ?", time_range.first, time_range.last)
end
@field = field
@time_zone = time_zone
@time_range = time_range
+ @week_start = week_start
end
def build_series(count)
utc = ActiveSupport::TimeZone["UTC"]
cast_method =
case @field
when "day_of_week", "hour_of_day"
lambda{|k| k.to_i }
else
- lambda{|k| (k.is_a?(Time) ? k : utc.parse(k)).utc }
+ lambda{|k| (k.is_a?(String) ? utc.parse(k) : k.to_time).utc }
end
count = Hash[count.map do |k, v|
[cast_method.call(k), v]
end]
@@ -53,11 +54,12 @@
when "hour"
time.change(:min => 0)
when "day"
time.beginning_of_day
when "week"
- # beginning_of_week does not support :sunday argument in activesupport < 3.2
- (time - time.wday.days).midnight
+ # same logic as MySQL group
+ weekday = (time.wday - 1) % 7
+ (time - ((7 - @week_start + weekday) % 7).days).midnight
when "month"
time.beginning_of_month
else # year
time.beginning_of_year
end