diff --git a/lib/groupdate/magic.rb b/lib/groupdate/magic.rb index d5a325f..26ce88e 100644 --- a/lib/groupdate/magic.rb +++ b/lib/groupdate/magic.rb @@ -85,6 +85,10 @@ module Groupdate raise "Connection adapter not supported: #{adapter_name}" end + if adapter_name == "MySQL" && field == :week + query[0] = "CAST(#{query[0]} AS DATETIME)" + end + group = relation.group(Groupdate::OrderHack.new(relation.send(:sanitize_sql_array, query), field, time_zone)) if options[:series] == false group @@ -126,7 +130,7 @@ module Groupdate lambda { |k| k.to_i } else utc = ActiveSupport::TimeZone["UTC"] - lambda { |k| (k.is_a?(String) ? utc.parse(k) : k.to_time).in_time_zone(time_zone) } + lambda { |k| (k.is_a?(String) ? utc.parse(k) : (k.is_a?(BigDecimal) ? Time.at(k) : k.to_time)).in_time_zone(time_zone) } end count = diff --git a/test/test_helper.rb b/test/test_helper.rb index cd82e23..42e4f87 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -864,6 +864,7 @@ module TestGroupdate def test_zeros_null_value create_user nil + User.update_all created_at: nil, created_on: nil # for MySQL adapter assert_equal 0, call_method(:hour_of_day, :created_at, range: true)[0] end -- libgit2 0.21.0