mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
Model must only allow values inside of array for EnsureInclusionOf
This commit is contained in:
parent
12121b8b85
commit
dc5daca58f
2 changed files with 23 additions and 2 deletions
|
@ -83,7 +83,7 @@ module Shoulda # :nodoc:
|
|||
disallows_higher_value &&
|
||||
allows_maximum_value
|
||||
elsif @array
|
||||
if allows_all_values_in_array? && allows_blank_value? && allows_nil_value?
|
||||
if allows_all_values_in_array? && allows_blank_value? && allows_nil_value? && disallows_outside_values?
|
||||
true
|
||||
else
|
||||
@failure_message = "#{@array} doesn't match array in validation"
|
||||
|
@ -138,8 +138,19 @@ module Shoulda # :nodoc:
|
|||
def allows_maximum_value
|
||||
allows_value_of(@maximum, @high_message)
|
||||
end
|
||||
end
|
||||
|
||||
def disallows_outside_values?
|
||||
disallows_value_of(value_outside_of_array(@array))
|
||||
end
|
||||
|
||||
def value_outside_of_array(array)
|
||||
not_in_array = array.last.next
|
||||
while array.include?(not_in_array)
|
||||
not_in_array.next!
|
||||
end
|
||||
not_in_array
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,16 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
|
||||
context "with no validations" do
|
||||
before do
|
||||
@model = define_model(:example, :attr => :string) do
|
||||
end.new
|
||||
end
|
||||
|
||||
it "should reject an array which does not have a validator defined" do
|
||||
@model.should_not ensure_inclusion_of(:attr).in_array(%w(Yes No))
|
||||
end
|
||||
end
|
||||
|
||||
context "an attribute which must be included in a range" do
|
||||
before do
|
||||
|
|
Loading…
Reference in a new issue