diff --git a/CHANGELOG.md b/CHANGELOG.md index 39adb19..8746b37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.5.2 [unreleased] + +- Added `dates` option to return dates for day, week, month, quarter, and year + ## 2.5.1 - Added `group_by_quarter` diff --git a/lib/groupdate.rb b/lib/groupdate.rb index 344f5c0..6aa5a5c 100644 --- a/lib/groupdate.rb +++ b/lib/groupdate.rb @@ -9,9 +9,10 @@ module Groupdate FIELDS = PERIODS METHODS = PERIODS.map { |v| :"group_by_#{v}" } - mattr_accessor :week_start, :day_start, :time_zone + mattr_accessor :week_start, :day_start, :time_zone, :dates self.week_start = :sun self.day_start = 0 + self.dates = false end require "groupdate/enumerable" diff --git a/lib/groupdate/magic.rb b/lib/groupdate/magic.rb index e6baa04..f41b208 100644 --- a/lib/groupdate/magic.rb +++ b/lib/groupdate/magic.rb @@ -254,6 +254,8 @@ module Groupdate I18n.localize(key, format: options[:format], locale: locale) end end + elsif (options[:dates] || (Groupdate.dates && !options.key?(:dates))) && [:day, :week, :month, :quarter, :year].include?(field) + lambda { |k| k.to_date } else lambda { |k| k } end diff --git a/test/test_helper.rb b/test/test_helper.rb index e63e300..9179622 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -906,6 +906,13 @@ module TestGroupdate assert_equal 2, User.group_by_day(:created_at, carry_forward: true).count[utc.parse("2014-05-02 00:00:00 UTC")] end + # dates + + def test_dates + create_user "2014-03-01 12:00:00 UTC" + assert_equal ({Date.parse("2014-03-01") => 1}), User.group_by_day(:created_at, dates: true).count + end + # helpers def assert_format(method, expected, format, options = {}) -- libgit2 0.21.0