未验证 提交 eae65ac2 编写于 作者: A Andrew White 提交者: GitHub

Merge pull request #31081 from rails/allow-include-time-with-zone-range

Allow `Range#include?` on TWZ ranges
* Allow `Range#include?` on TWZ ranges
In #11474 we prevented TWZ ranges being iterated over which matched
Ruby's handling of Time ranges and as a consequence `include?`
stopped working with both Time ranges and TWZ ranges. However in
ruby/ruby@b061634 support was added for `include?` to use `cover?`
for 'linear' objects. Since we have no way of making Ruby consider
TWZ instances as 'linear' we have to override `Range#include?`.
Fixes #30799.
*Andrew White*
* Fix acronym support in `humanize`
Acronym inflections are stored with lowercase keys in the hash but
......
......@@ -2,5 +2,6 @@
require "active_support/core_ext/range/conversions"
require "active_support/core_ext/range/include_range"
require "active_support/core_ext/range/include_time_with_zone"
require "active_support/core_ext/range/overlaps"
require "active_support/core_ext/range/each"
# frozen_string_literal: true
require "active_support/time_with_zone"
module ActiveSupport
module EachTimeWithZone #:nodoc:
def each(&block)
......@@ -15,7 +17,7 @@ def step(n = 1, &block)
private
def ensure_iteration_allowed
raise TypeError, "can't iterate from #{first.class}" if first.is_a?(Time)
raise TypeError, "can't iterate from #{first.class}" if first.is_a?(TimeWithZone)
end
end
end
......
# frozen_string_literal: true
require "active_support/time_with_zone"
module ActiveSupport
module IncludeTimeWithZone #:nodoc:
# Extends the default Range#include? to support ActiveSupport::TimeWithZone.
#
# (1.hour.ago..1.hour.from_now).include?(Time.current) # => true
#
def include?(value)
if first.is_a?(TimeWithZone)
cover?(value)
elsif last.is_a?(TimeWithZone)
cover?(value)
else
super
end
end
end
end
Range.prepend(ActiveSupport::IncludeTimeWithZone)
......@@ -121,9 +121,12 @@ def test_step_on_time_with_zone
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_raises TypeError do
((twz - 1.hour)..twz).include?(twz)
end
assert ((twz - 1.hour)..twz).include?(twz)
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
end
def test_date_time_with_each
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册