提交 d6fe9e83 编写于 作者: R Rafael Mendonça França

Merge pull request #12612 from amatsuda/amo_validates_inclusion_of_time_range_error

Let validates_inclusion_of accept Time and DateTime ranges
......@@ -30,12 +30,18 @@ def delimiter
@delimiter ||= options[:in] || options[:within]
end
# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the
# range for equality, which is slower but more accurate. <tt>Range#cover?</tt> uses
# the previous logic of comparing a value with the range endpoints, which is fast
# but is only accurate on numeric ranges.
# In Ruby 1.9 <tt>Range#include?</tt> on non-number-or-time-ish ranges checks all
# possible values in the range for equality, which is slower but more accurate.
# <tt>Range#cover?</tt> uses the previous logic of comparing a value with the range
# endpoints, which is fast but is only accurate on Numeric, Time, or DateTime ranges.
def inclusion_method(enumerable)
(enumerable.is_a?(Range) && enumerable.first.is_a?(Numeric)) ? :cover? : :include?
return :include? unless enumerable.is_a?(Range)
case enumerable.first
when Numeric, Time, DateTime
:cover?
else
:include?
end
end
end
end
......
# encoding: utf-8
require 'cases/helper'
require 'active_support/all'
require 'models/topic'
require 'models/person'
......@@ -20,6 +21,27 @@ def test_validates_inclusion_of_range
assert Topic.new("title" => "bbb", "content" => "abc").valid?
end
def test_validates_inclusion_of_time_range
Topic.validates_inclusion_of(:created_at, in: 1.year.ago..Time.now)
assert Topic.new(title: 'aaa', created_at: 2.years.ago).invalid?
assert Topic.new(title: 'aaa', created_at: 3.months.ago).valid?
assert Topic.new(title: 'aaa', created_at: 37.weeks.from_now).invalid?
end
def test_validates_inclusion_of_date_range
Topic.validates_inclusion_of(:created_at, in: 1.year.until(Date.today)..Date.today)
assert Topic.new(title: 'aaa', created_at: 2.years.until(Date.today)).invalid?
assert Topic.new(title: 'aaa', created_at: 3.months.until(Date.today)).valid?
assert Topic.new(title: 'aaa', created_at: 37.weeks.since(Date.today)).invalid?
end
def test_validates_inclusion_of_date_time_range
Topic.validates_inclusion_of(:created_at, in: 1.year.until(DateTime.current)..DateTime.current)
assert Topic.new(title: 'aaa', created_at: 2.years.until(DateTime.current)).invalid?
assert Topic.new(title: 'aaa', created_at: 3.months.until(DateTime.current)).valid?
assert Topic.new(title: 'aaa', created_at: 37.weeks.since(DateTime.current)).invalid?
end
def test_validates_inclusion_of
Topic.validates_inclusion_of(:title, in: %w( a b c d e f g ))
......
......@@ -6,7 +6,7 @@ def self._validates_default_keys
super | [ :message ]
end
attr_accessor :title, :author_name, :content, :approved
attr_accessor :title, :author_name, :content, :approved, :created_at
attr_accessor :after_validation_performed
after_validation :perform_after_validation
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册