-
-
Save Austio/2468b362f0c466f1e5d33758175ce591 to your computer and use it in GitHub Desktop.
Rails - Groupdate 5.2.2 - active_record.default_timezone = :local
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Groupdate::Magic::Relation.prepend Ext::Groupdate::Magic::Relation | |
Groupdate::Adapters::BaseAdapter.prepend Ext::Groupdate::Adapters::BaseAdapter | |
Groupdate::Adapters::MySQLAdapter.prepend Ext::Groupdate::Adapters::MysqlAdapter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Ext | |
module Groupdate | |
module Adapters | |
module BaseAdapter | |
def initialize(relation, column:, period:, time_zone:, time_range:, week_start:, day_start:, n_seconds:) | |
begin | |
super | |
rescue ::Groupdate::Error | |
end | |
end | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Ext | |
module Groupdate | |
module Adapters | |
module MysqlAdapter | |
def group_clause | |
day_start_column = "#{column} - INTERVAL ? second" | |
query = | |
case period | |
when :minute_of_hour | |
["MINUTE(#{day_start_column})", day_start] | |
when :hour_of_day | |
["HOUR(#{day_start_column})", day_start] | |
when :day_of_week | |
["DAYOFWEEK(#{day_start_column}) - 1", day_start] | |
when :day_of_month | |
["DAYOFMONTH(#{day_start_column})", day_start] | |
when :day_of_year | |
["DAYOFYEAR(#{day_start_column})", day_start] | |
when :month_of_year | |
["MONTH(#{day_start_column})", day_start] | |
when :week | |
["DATE_FORMAT(#{day_start_column} - INTERVAL ((? + DAYOFWEEK(#{day_start_column})) % 7) DAY, '%Y-%m-%d 00:00:00') + INTERVAL ? second", day_start, 12 - week_start, day_start, day_start] | |
when :quarter | |
["DATE_FORMAT(DATE(CONCAT(YEAR(#{day_start_column}), '-', LPAD(1 + 3 * (QUARTER(#{day_start_column}) - 1), 2, '00'), '-01')), '%Y-%m-%d %H:%i:%S') + INTERVAL ? second", day_start, day_start, day_start] | |
when :custom | |
["FROM_UNIXTIME((UNIX_TIMESTAMP(#{column}) DIV ?) * ?)", n_seconds, n_seconds] | |
else | |
format = | |
case period | |
when :second | |
"%Y-%m-%d %H:%i:%S" | |
when :minute | |
"%Y-%m-%d %H:%i:00" | |
when :hour | |
"%Y-%m-%d %H:00:00" | |
when :day | |
"%Y-%m-%d 00:00:00" | |
when :month | |
"%Y-%m-01 00:00:00" | |
else # year | |
"%Y-01-01 00:00:00" | |
end | |
["DATE_FORMAT(#{day_start_column}, ?) + INTERVAL ? second", day_start, format, day_start] | |
end | |
clean_group_clause(@relation.send(:sanitize_sql_array, query)) | |
end | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Ext | |
module Groupdate | |
module Magic | |
module Relation | |
def cast_method | |
@cast_method ||= begin | |
case period | |
when :minute_of_hour, :hour_of_day, :day_of_month, :day_of_year, :month_of_year | |
lambda { |k| k.to_i } | |
when :day_of_week | |
lambda { |k| (k.to_i - 1 - week_start) % 7 } | |
else | |
utc = ActiveSupport::TimeZone[Rails.application.config.time_zone] | |
lambda { |k| (k.is_a?(String) || !k.respond_to?(:to_time) ? utc.parse(k.to_s) : k.to_time).in_time_zone(time_zone) } | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment