Commit 547943156a58e9581b6e6261ece6a57865db230b

Authored by Leonel Galán
Committed by Andrew Kane
1 parent d2e2596a

Fixes time_range for quarter/last combination (#151)

`time_range` was nil when grouping by quarter.
Showing 2 changed files with 20 additions and 2 deletions   Show diff stats
lib/groupdate/magic.rb
... ... @@ -182,11 +182,15 @@ module Groupdate
182 182 last += 1.day unless time_range.exclude_end?
183 183 time_range = Range.new(time_zone.parse(time_range.first.to_s), last, true)
184 184 elsif !time_range && options[:last]
185   - step = 1.send(field) if 1.respond_to?(field)
  185 + if field == :quarter
  186 + step = 3.months
  187 + else
  188 + step = 1.send(field) if 1.respond_to?(field)
  189 + end
186 190 if step
187 191 now = Time.now
188 192 now -= step if options[:current] == false
189   - time_range = round_time(now - (options[:last].to_i - 1).send(field))..now
  193 + time_range = round_time(now - (options[:last].to_i - 1) * step)..now
190 194 end
191 195 end
192 196 time_range
... ...
test/test_helper.rb
... ... @@ -235,6 +235,16 @@ module TestDatabase
235 235 assert_equal expected, User.group_by_year(:created_at, last: 2, current: false).count
236 236 end
237 237  
  238 + def test_quarter_and_last
  239 + create_user "#{this_year}-#{this_quarters_month}-01"
  240 + create_user "#{this_year}-#{this_quarters_month - 6}-01"
  241 + expected = {
  242 + Date.parse("#{this_year}-#{this_quarters_month - 3}-01") => 0,
  243 + Date.parse("#{this_year}-#{this_quarters_month}-01") => 1
  244 + }
  245 + assert_equal expected, User.group_by_quarter(:created_at, last: 2).count
  246 + end
  247 +
238 248 def test_format_locale
239 249 create_user "2014-10-01"
240 250 assert_equal ({"Okt" => 1}), User.group_by_day(:created_at, format: "%b", locale: :de).count
... ... @@ -1147,6 +1157,10 @@ module TestGroupdate
1147 1157 assert_equal expected, call_method(method, :created_at, options.merge(series: true, time_zone: time_zone ? "Pacific Time (US & Canada)" : nil, range: Time.parse(range_start)..Time.parse(range_end)))
1148 1158 end
1149 1159  
  1160 + def this_quarters_month
  1161 + Time.now.utc.beginning_of_quarter.month
  1162 + end
  1163 +
1150 1164 def this_year
1151 1165 Time.now.utc.year
1152 1166 end
... ...