Return an integer for fixnum columns for inclusion

* Fixes https://github.com/thoughtbot/shoulda-matchers/issues/179
This commit is contained in:
Jason Draper 2013-08-14 21:40:07 -04:00
parent 9cd3f8502d
commit 87b80e2ca6
3 changed files with 28 additions and 1 deletions

View File

@ -1,5 +1,8 @@
# HEAD
* Fix a bug in `ensure_inclusion_of` that would cause issues with using
`in_array` with an integer value.
* Add support for PostgreSQL UUID columns to `validates_uniqueness_of` (#334).
* Fix `validates_numericality_of` so that `is_equal_to` submatcher works

View File

@ -23,6 +23,7 @@ module Shoulda # :nodoc:
class EnsureInclusionOfMatcher < ValidationMatcher # :nodoc:
ARBITRARY_OUTSIDE_STRING = 'shouldamatchersteststring'
ARBITRARY_OUTSIDE_FIXNUM = 123456789
def initialize(attribute)
super(attribute)
@ -144,8 +145,21 @@ module Shoulda # :nodoc:
end
def value_outside_of_array
if @array.include?(ARBITRARY_OUTSIDE_STRING)
if @array.include?(outside_value)
raise CouldNotDetermineValueOutsideOfArray
else
outside_value
end
end
def outside_value
@outside_value ||= find_outside_value
end
def find_outside_value
case @subject.send(@attribute.to_s)
when Fixnum
ARBITRARY_OUTSIDE_FIXNUM
else
ARBITRARY_OUTSIDE_STRING
end

View File

@ -8,6 +8,16 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
end
end
context 'with an integer column' do
it 'can verify a zero in the array' do
model = define_model(:example, :attr => :integer) do
validates_inclusion_of :attr, :in => [0, 1, 2]
end.new
model.should ensure_inclusion_of(:attr).in_array([0,1,2])
end
end
context 'with true/false values' do
it 'can verify outside values to ensure the negative case' do
define_model(:example, :attr => :string).new.