Skip to content

Instantly share code, notes, and snippets.

@essame
Forked from leods92/group_date.rb
Last active March 25, 2016 07:05
Show Gist options
  • Save essame/7229403 to your computer and use it in GitHub Desktop.
Save essame/7229403 to your computer and use it in GitHub Desktop.
module ActiveRecord
module Querying
delegate :group_date, :to => :scoped
end
module QueryMethods
def group_date(column_name = nil, group_by = nil)
return self if column_name.blank?
# Rails uses a non standard time zone naming.
# Let's get tz database standard which is used by many databases.
# Using this instead of time offset or other approaches is necessary
# in order to deal with daylight saving time automatically.
zone = Time.zone.tzinfo.name
# Rails stores timestamps without time zones by default.
# So we must first set a zone to column_name (column_name at time zone 'UTC').
# Note that even though Rails does that under the hood when you use ActiveRecord,
# that's not applicable here.
# Then, we set column to application's time zone so that the database can
# convert timestamp to proper date.
convert = "at time zone 'UTC' at time zone '#{zone}'"
date = "date(date_trunc('#{group_by}', #{table.name}.#{column_name} #{convert}))"
# Since converting date is not that straight-forward,
# this will automatically return converted date.
# We gotta convert output date to assure Rails won't convert it again.
clone.group(date).select("#{date} #{convert} as #{column_name}")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment