mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
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.
This commit is contained in:
parent
b50ce8d6cf
commit
740bb178d9
3 changed files with 27 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue