Skip to content

Commit

Permalink
Merge pull request #12612 from amatsuda/amo_validates_inclusion_of_ti…
Browse files Browse the repository at this point in the history
…me_range_error

Let validates_inclusion_of accept Time and DateTime ranges
  • Loading branch information
rafaelfranca committed Oct 23, 2013
1 parent 86db9bc commit fb49bca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
16 changes: 11 additions & 5 deletions activemodel/lib/active_model/validations/clusivity.rb
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions activemodel/test/cases/validations/inclusion_validation_test.rb
@@ -1,5 +1,6 @@
# encoding: utf-8
require 'cases/helper'
require 'active_support/all'

require 'models/topic'
require 'models/person'
Expand All @@ -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 ) )

Expand Down
2 changes: 1 addition & 1 deletion activemodel/test/models/topic.rb
Expand Up @@ -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
Expand Down

0 comments on commit fb49bca

Please sign in to comment.