From 740bb178d96643aed2a780e107df4d9d826b3b04 Mon Sep 17 00:00:00 2001 From: Vishal Telangre Date: Wed, 8 Jan 2020 17:15:10 +0530 Subject: [PATCH] Deprecate using `Range#include?` to check the inclusion of a value in a date time range Use `Range#cover?` instead of `Range#include?` to check the inclusion of a value in a date time range. --- activesupport/CHANGELOG.md | 7 +++++++ .../core_ext/range/include_time_with_zone.rb | 11 ++++++++--- activesupport/test/core_ext/range_ext_test.rb | 12 ++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 2254db2e69..5f36cfcbd7 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,10 @@ +* Deprecate using `Range#include?` method to check the inclusion of a value + in a date time range. It is recommended to use `Range#cover?` method + instead of `Range#include?` to check the inclusion of a value + in a date time range. + + *Vishal Telangre* + * Support added for a `round_mode` parameter, in all number helpers. (See: `BigDecimal::mode`.) ```ruby diff --git a/activesupport/lib/active_support/core_ext/range/include_time_with_zone.rb b/activesupport/lib/active_support/core_ext/range/include_time_with_zone.rb index 2b458717ec..89e911b11d 100644 --- a/activesupport/lib/active_support/core_ext/range/include_time_with_zone.rb +++ b/activesupport/lib/active_support/core_ext/range/include_time_with_zone.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require "active_support/time_with_zone" +require "active_support/deprecation" module ActiveSupport module IncludeTimeWithZone #:nodoc: @@ -9,9 +10,13 @@ module ActiveSupport # (1.hour.ago..1.hour.from_now).include?(Time.current) # => true # def include?(value) - if self.begin.is_a?(TimeWithZone) - cover?(value) - elsif self.end.is_a?(TimeWithZone) + if self.begin.is_a?(TimeWithZone) || self.end.is_a?(TimeWithZone) + ActiveSupport::Deprecation.warn(<<-MSG.squish) + Using `Range#include?` to check the inclusion of a value in + a date time range is deprecated. + It is recommended to use `Range#cover?` instead of `Range#include?` to + check the inclusion of a value in a date time range. + MSG cover?(value) else super diff --git a/activesupport/test/core_ext/range_ext_test.rb b/activesupport/test/core_ext/range_ext_test.rb index f29bfdcf14..bae96dfa92 100644 --- a/activesupport/test/core_ext/range_ext_test.rb +++ b/activesupport/test/core_ext/range_ext_test.rb @@ -191,11 +191,23 @@ class RangeTest < ActiveSupport::TestCase end end + def test_cover_on_time_with_zone + twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30)) + assert ((twz - 1.hour)..twz).cover?(twz) + end + def test_include_on_time_with_zone twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30)) assert ((twz - 1.hour)..twz).include?(twz) end + def test_include_on_time_with_zone_deprecation + twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30)) + assert_deprecated do + ((twz - 1.hour)..twz).include?(twz) + end + end + def test_case_equals_on_time_with_zone twz = ActiveSupport::TimeWithZone.new(nil, ActiveSupport::TimeZone["Eastern Time (US & Canada)"], Time.utc(2006, 11, 28, 10, 30)) assert ((twz - 1.hour)..twz) === twz