From e82aadcd0974b6647da63ce744d8a01b77e19307 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Tue, 29 Sep 2015 00:07:26 -0700 Subject: [PATCH] Added current option --- CHANGELOG.md | 1 + README.md | 6 ++++++ lib/groupdate/magic.rb | 1 + test/test_helper.rb | 10 ++++++++++ 4 files changed, 18 insertions(+), 0 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 658a853..4fa76d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 2.5.0 [unreleased] - Added `group_by_period` method +- Added `current` option - Raise `ArgumentError` if no block given to enumerable ## 2.4.0 diff --git a/README.md b/README.md index 4bb8716..766a6cb 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,12 @@ To get the most recent time periods, use: User.group_by_week(:created_at, last: 8).count # last 8 weeks ``` +To exclude the current period, use: [master] + +```ruby +User.group_by_week(:created_at, last: 8, current: false).count +``` + ### Order You can order in descending order with: diff --git a/lib/groupdate/magic.rb b/lib/groupdate/magic.rb index d80ac58..fef9873 100644 --- a/lib/groupdate/magic.rb +++ b/lib/groupdate/magic.rb @@ -167,6 +167,7 @@ module Groupdate step = 1.send(field) if 1.respond_to?(field) if step now = Time.now + now -= step if options[:current] == false time_range = round_time(now - (options[:last].to_i - 1).send(field))..now end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 7de437e..9b554c2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -698,6 +698,16 @@ module TestGroupdate assert_equal expected, User.group_by_year(:created_at, last: 3).count end + def test_current + create_user "2012-05-01 00:00:00 UTC" + create_user "2014-05-01 00:00:00 UTC" + expected = { + utc.parse("2013-01-01 00:00:00 UTC") => 0, + utc.parse("2014-01-01 00:00:00 UTC") => 1 + } + assert_equal expected, User.group_by_year(:created_at, last: 2, current: false).count + end + def test_format_day create_user "2014-03-01 00:00:00 UTC" assert_format :day, "March 1, 2014", "%B %-e, %Y" -- libgit2 0.21.0