Commit 4edaed72b972fa534020152af27f54470392e679

Authored by Andrew Kane
2 parents b27bd97e c3fff138
Exists in sqlite

Merge branch 'master' into sqlite

README.md
... ... @@ -234,6 +234,14 @@ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql
234 234  
235 235 or copy and paste [these statements](https://gist.githubusercontent.com/ankane/1d6b0022173186accbf0/raw/time_zone_support.sql) into a SQL console.
236 236  
  237 +You can confirm it worked with:
  238 +
  239 +```sql
  240 +SELECT CONVERT_TZ(NOW(), '+00:00', 'Etc/UTC');
  241 +```
  242 +
  243 +It should return the time instead of `NULL`.
  244 +
237 245 ## Upgrading
238 246  
239 247 ### 3.0
... ...
Rakefile
... ... @@ -5,6 +5,7 @@ task default: :test
5 5 Rake::TestTask.new do |t|
6 6 t.libs << "test"
7 7 t.test_files = FileList["test/**/*_test.rb"].exclude(/redshift/)
  8 + t.warning = false
8 9 end
9 10  
10 11 namespace :test do
... ...
lib/groupdate.rb
... ... @@ -4,6 +4,8 @@ require &quot;groupdate/version&quot;
4 4 require "groupdate/magic"
5 5  
6 6 module Groupdate
  7 + class Error < StandardError; end
  8 +
7 9 PERIODS = [:second, :minute, :hour, :day, :week, :month, :quarter, :year, :day_of_week, :hour_of_day, :day_of_month, :month_of_year]
8 10 # backwards compatibility for anyone who happened to use it
9 11 FIELDS = PERIODS
... ...
lib/groupdate/magic.rb
... ... @@ -178,14 +178,14 @@ module Groupdate
178 178 lambda { |k| (k.is_a?(String) || !k.respond_to?(:to_time) ? utc.parse(k.to_s) : k.to_time).in_time_zone(time_zone) }
179 179 end
180 180  
181   - count =
182   - begin
183   - Hash[relation.send(method, *args, &block).map { |k, v| [multiple_groups ? k[0...@group_index] + [cast_method.call(k[@group_index])] + k[(@group_index + 1)..-1] : cast_method.call(k), v] }]
184   - rescue NoMethodError
185   - raise "Be sure to install time zone support - https://github.com/ankane/groupdate#for-mysql"
186   - end
  181 + result = relation.send(method, *args, &block)
  182 + missing_time_zone_support = multiple_groups ? (result.keys.first && result.keys.first[@group_index].nil?) : result.key?(nil)
  183 + if missing_time_zone_support
  184 + raise Groupdate::Error, "Be sure to install time zone support - https://github.com/ankane/groupdate#for-mysql"
  185 + end
  186 + result = Hash[result.map { |k, v| [multiple_groups ? k[0...@group_index] + [cast_method.call(k[@group_index])] + k[(@group_index + 1)..-1] : cast_method.call(k), v] }]
187 187  
188   - series(count, (options.key?(:default_value) ? options[:default_value] : 0), multiple_groups, reverse)
  188 + series(result, (options.key?(:default_value) ? options[:default_value] : 0), multiple_groups, reverse)
189 189 end
190 190  
191 191 protected
... ...
test/test_helper.rb
... ... @@ -227,10 +227,13 @@ module TestDatabase
227 227  
228 228 def test_last_date
229 229 Time.zone = pt
230   - create_user Date.today.to_s
  230 + today = Date.today
  231 + create_user today.to_s
  232 + this_month = pt.parse(today.to_s).beginning_of_month
  233 + last_month = this_month - 1.month
231 234 expected = {
232   - Date.parse("#{this_year}-#{this_month - 1}-01") => 0,
233   - Date.parse("#{this_year}-#{this_month}-01") => 1
  235 + last_month.to_date => 0,
  236 + this_month.to_date => 1
234 237 }
235 238 assert_equal expected, User.group_by_month(:created_on, last: 2).count
236 239 ensure
... ... @@ -253,11 +256,13 @@ module TestDatabase
253 256 end
254 257  
255 258 def test_quarter_and_last
256   - create_user "#{this_year}-#{this_quarters_month}-01"
257   - create_user "#{this_year}-#{this_quarters_month - 6}-01"
  259 + today = Date.today
  260 + create_user today.to_s
  261 + this_quarter = today.to_time.beginning_of_quarter
  262 + last_quarter = this_quarter - 3.months
258 263 expected = {
259   - Date.parse("#{this_year}-#{this_quarters_month - 3}-01") => 0,
260   - Date.parse("#{this_year}-#{this_quarters_month}-01") => 1
  264 + last_quarter.to_date => 0,
  265 + this_quarter.to_date => 1
261 266 }
262 267 assert_equal expected, User.group_by_quarter(:created_at, last: 2).count
263 268 end
... ... @@ -403,7 +408,7 @@ module TestDatabase
403 408 end
404 409  
405 410 def test_using_listed_but_undefined_custom_calculation_method_raises_error
406   - assert_raises(RuntimeError) do
  411 + assert_raises(NoMethodError) do
407 412 User.group_by_day(:created_at).undefined_calculation
408 413 end
409 414 end
... ...