mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Let validates_inclusion_of accept Time and DateTime ranges
fixes 4.0.0 regression introduced in 0317b93c17
This commit is contained in:
parent
dc8a20677d
commit
68db6bc431
3 changed files with 34 additions and 6 deletions
|
@ -30,12 +30,18 @@ module ActiveModel
|
||||||
@delimiter ||= options[:in] || options[:within]
|
@delimiter ||= options[:in] || options[:within]
|
||||||
end
|
end
|
||||||
|
|
||||||
# In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the
|
# In Ruby 1.9 <tt>Range#include?</tt> on non-number-or-time-ish ranges checks all
|
||||||
# range for equality, which is slower but more accurate. <tt>Range#cover?</tt> uses
|
# possible values in the range for equality, which is slower but more accurate.
|
||||||
# the previous logic of comparing a value with the range endpoints, which is fast
|
# <tt>Range#cover?</tt> uses the previous logic of comparing a value with the range
|
||||||
# but is only accurate on numeric ranges.
|
# endpoints, which is fast but is only accurate on Numeric, Time, or DateTime ranges.
|
||||||
def inclusion_method(enumerable)
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# encoding: utf-8
|
# encoding: utf-8
|
||||||
require 'cases/helper'
|
require 'cases/helper'
|
||||||
|
require 'active_support/all'
|
||||||
|
|
||||||
require 'models/topic'
|
require 'models/topic'
|
||||||
require 'models/person'
|
require 'models/person'
|
||||||
|
@ -20,6 +21,27 @@ class InclusionValidationTest < ActiveModel::TestCase
|
||||||
assert Topic.new("title" => "bbb", "content" => "abc").valid?
|
assert Topic.new("title" => "bbb", "content" => "abc").valid?
|
||||||
end
|
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
|
def test_validates_inclusion_of
|
||||||
Topic.validates_inclusion_of(:title, in: %w( a b c d e f g ))
|
Topic.validates_inclusion_of(:title, in: %w( a b c d e f g ))
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ class Topic
|
||||||
super | [ :message ]
|
super | [ :message ]
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :title, :author_name, :content, :approved
|
attr_accessor :title, :author_name, :content, :approved, :created_at
|
||||||
attr_accessor :after_validation_performed
|
attr_accessor :after_validation_performed
|
||||||
|
|
||||||
after_validation :perform_after_validation
|
after_validation :perform_after_validation
|
||||||
|
|
Loading…
Reference in a new issue