Skip to content

Commit

Permalink
Model must only allow values inside of array for EnsureInclusionOf
Browse files Browse the repository at this point in the history
  • Loading branch information
drapergeek committed Sep 28, 2012
1 parent 12121b8 commit dc5daca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb
Expand Up @@ -83,7 +83,7 @@ def matches?(subject)
disallows_higher_value && disallows_higher_value &&
allows_maximum_value allows_maximum_value
elsif @array 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 true
else else
@failure_message = "#{@array} doesn't match array in validation" @failure_message = "#{@array} doesn't match array in validation"
Expand Down Expand Up @@ -138,8 +138,19 @@ def allows_minimum_value
def allows_maximum_value def allows_maximum_value
allows_value_of(@maximum, @high_message) allows_value_of(@maximum, @high_message)
end 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 end
end end
10 changes: 10 additions & 0 deletions spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb
@@ -1,6 +1,16 @@
require 'spec_helper' require 'spec_helper'


describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do 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 context "an attribute which must be included in a range" do
before do before do
Expand Down

0 comments on commit dc5daca

Please sign in to comment.