Commit e82aadcd0974b6647da63ce744d8a01b77e19307

Authored by Andrew Kane
1 parent 4a842ecd

Added current option

CHANGELOG.md
1 1 ## 2.5.0 [unreleased]
2 2  
3 3 - Added `group_by_period` method
  4 +- Added `current` option
4 5 - Raise `ArgumentError` if no block given to enumerable
5 6  
6 7 ## 2.4.0
... ...
README.md
... ... @@ -115,6 +115,12 @@ To get the most recent time periods, use:
115 115 User.group_by_week(:created_at, last: 8).count # last 8 weeks
116 116 ```
117 117  
  118 +To exclude the current period, use: [master]
  119 +
  120 +```ruby
  121 +User.group_by_week(:created_at, last: 8, current: false).count
  122 +```
  123 +
118 124 ### Order
119 125  
120 126 You can order in descending order with:
... ...
lib/groupdate/magic.rb
... ... @@ -167,6 +167,7 @@ module Groupdate
167 167 step = 1.send(field) if 1.respond_to?(field)
168 168 if step
169 169 now = Time.now
  170 + now -= step if options[:current] == false
170 171 time_range = round_time(now - (options[:last].to_i - 1).send(field))..now
171 172 end
172 173 end
... ...
test/test_helper.rb
... ... @@ -698,6 +698,16 @@ module TestGroupdate
698 698 assert_equal expected, User.group_by_year(:created_at, last: 3).count
699 699 end
700 700  
  701 + def test_current
  702 + create_user "2012-05-01 00:00:00 UTC"
  703 + create_user "2014-05-01 00:00:00 UTC"
  704 + expected = {
  705 + utc.parse("2013-01-01 00:00:00 UTC") => 0,
  706 + utc.parse("2014-01-01 00:00:00 UTC") => 1
  707 + }
  708 + assert_equal expected, User.group_by_year(:created_at, last: 2, current: false).count
  709 + end
  710 +
701 711 def test_format_day
702 712 create_user "2014-03-01 00:00:00 UTC"
703 713 assert_format :day, "March 1, 2014", "%B %-e, %Y"
... ...