diff options
author | Andrew White <pixeltrix@users.noreply.github.com> | 2017-11-09 13:50:50 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-09 13:50:50 +0000 |
commit | eae65ac2ab7e7a8155fa5a76c15f21cee09499c2 (patch) | |
tree | b6ed0736858d7b68c68bff3d12f00293402d6d7e /activesupport/lib/active_support/core_ext/range/include_time_with_zone.rb | |
parent | 518959d0127aac81020ccd50f3dc425f857e6715 (diff) | |
parent | 2b434d6f79813dcad162b158fd2b60e34a725ba1 (diff) | |
download | rails-eae65ac2ab7e7a8155fa5a76c15f21cee09499c2.tar.gz rails-eae65ac2ab7e7a8155fa5a76c15f21cee09499c2.tar.bz2 rails-eae65ac2ab7e7a8155fa5a76c15f21cee09499c2.zip |
Merge pull request #31081 from rails/allow-include-time-with-zone-range
Allow `Range#include?` on TWZ ranges
Diffstat (limited to 'activesupport/lib/active_support/core_ext/range/include_time_with_zone.rb')
-rw-r--r-- | activesupport/lib/active_support/core_ext/range/include_time_with_zone.rb | 23 |
1 files changed, 23 insertions, 0 deletions
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 new file mode 100644 index 0000000000..5f80acf68e --- /dev/null +++ b/activesupport/lib/active_support/core_ext/range/include_time_with_zone.rb @@ -0,0 +1,23 @@ +# 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) |