Commit 4edaed72b972fa534020152af27f54470392e679
Exists in
sqlite
Merge branch 'master' into sqlite
Showing
5 changed files
with
31 additions
and
15 deletions
Show diff stats
README.md
@@ -234,6 +234,14 @@ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql | @@ -234,6 +234,14 @@ mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql | ||
234 | 234 | ||
235 | or copy and paste [these statements](https://gist.githubusercontent.com/ankane/1d6b0022173186accbf0/raw/time_zone_support.sql) into a SQL console. | 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 | ## Upgrading | 245 | ## Upgrading |
238 | 246 | ||
239 | ### 3.0 | 247 | ### 3.0 |
Rakefile
@@ -5,6 +5,7 @@ task default: :test | @@ -5,6 +5,7 @@ task default: :test | ||
5 | Rake::TestTask.new do |t| | 5 | Rake::TestTask.new do |t| |
6 | t.libs << "test" | 6 | t.libs << "test" |
7 | t.test_files = FileList["test/**/*_test.rb"].exclude(/redshift/) | 7 | t.test_files = FileList["test/**/*_test.rb"].exclude(/redshift/) |
8 | + t.warning = false | ||
8 | end | 9 | end |
9 | 10 | ||
10 | namespace :test do | 11 | namespace :test do |
lib/groupdate.rb
@@ -4,6 +4,8 @@ require "groupdate/version" | @@ -4,6 +4,8 @@ require "groupdate/version" | ||
4 | require "groupdate/magic" | 4 | require "groupdate/magic" |
5 | 5 | ||
6 | module Groupdate | 6 | module Groupdate |
7 | + class Error < StandardError; end | ||
8 | + | ||
7 | PERIODS = [:second, :minute, :hour, :day, :week, :month, :quarter, :year, :day_of_week, :hour_of_day, :day_of_month, :month_of_year] | 9 | PERIODS = [:second, :minute, :hour, :day, :week, :month, :quarter, :year, :day_of_week, :hour_of_day, :day_of_month, :month_of_year] |
8 | # backwards compatibility for anyone who happened to use it | 10 | # backwards compatibility for anyone who happened to use it |
9 | FIELDS = PERIODS | 11 | FIELDS = PERIODS |
lib/groupdate/magic.rb
@@ -178,14 +178,14 @@ module Groupdate | @@ -178,14 +178,14 @@ module Groupdate | ||
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) } | 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 | end | 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 | end | 189 | end |
190 | 190 | ||
191 | protected | 191 | protected |
test/test_helper.rb
@@ -227,10 +227,13 @@ module TestDatabase | @@ -227,10 +227,13 @@ module TestDatabase | ||
227 | 227 | ||
228 | def test_last_date | 228 | def test_last_date |
229 | Time.zone = pt | 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 | expected = { | 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 | assert_equal expected, User.group_by_month(:created_on, last: 2).count | 238 | assert_equal expected, User.group_by_month(:created_on, last: 2).count |
236 | ensure | 239 | ensure |
@@ -253,11 +256,13 @@ module TestDatabase | @@ -253,11 +256,13 @@ module TestDatabase | ||
253 | end | 256 | end |
254 | 257 | ||
255 | def test_quarter_and_last | 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 | expected = { | 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 | assert_equal expected, User.group_by_quarter(:created_at, last: 2).count | 267 | assert_equal expected, User.group_by_quarter(:created_at, last: 2).count |
263 | end | 268 | end |
@@ -403,7 +408,7 @@ module TestDatabase | @@ -403,7 +408,7 @@ module TestDatabase | ||
403 | end | 408 | end |
404 | 409 | ||
405 | def test_using_listed_but_undefined_custom_calculation_method_raises_error | 410 | def test_using_listed_but_undefined_custom_calculation_method_raises_error |
406 | - assert_raises(RuntimeError) do | 411 | + assert_raises(NoMethodError) do |
407 | User.group_by_day(:created_at).undefined_calculation | 412 | User.group_by_day(:created_at).undefined_calculation |
408 | end | 413 | end |
409 | end | 414 | end |