Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Try next values in array only.

If we can't find a valid next value outside of each item
in the array, throw a custom exception.
  • Loading branch information...
commit f621667903205840bcc928c7f4fbe5af6b438f3b 1 parent 7f208e7
@drapergeek drapergeek authored
View
1  lib/shoulda/matchers/active_model.rb
@@ -13,6 +13,7 @@
require 'shoulda/matchers/active_model/validate_confirmation_of_matcher'
require 'shoulda/matchers/active_model/validate_numericality_of_matcher'
require 'shoulda/matchers/active_model/allow_mass_assignment_of_matcher'
+require 'shoulda/matchers/active_model/errors'
module Shoulda
View
22 lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb
@@ -83,7 +83,7 @@ def matches?(subject)
disallows_higher_value &&
allows_maximum_value
elsif @array
- if allows_all_values_in_array? && allows_blank_value? && allows_nil_value? && disallows_outside_values?
+ if allows_all_values_in_array? && allows_blank_value? && allows_nil_value? && disallows_value_outside_of_array?
true
else
@failure_message = "#{@array} doesn't match array in validation"
@@ -137,16 +137,22 @@ def allows_maximum_value
allows_value_of(@maximum, @high_message)
end
- def disallows_outside_values?
- disallows_value_of(value_outside_of_array(@array))
+ def disallows_value_outside_of_array?
+ if value_outside_of_array
+ disallows_value_of(value_outside_of_array)
+ else
+ raise CouldNotDetermineValueOutsideOfArray
+ end
end
- def value_outside_of_array(array)
- not_in_array = array.last.next
- while array.include?(not_in_array)
- not_in_array.next!
+ def value_outside_of_array
+ found = @array.detect do |item|
+ !@array.include?(item.next)
+ end
+
+ if found
+ found.next
end
- not_in_array
end
end
end
View
7 lib/shoulda/matchers/active_model/errors.rb
@@ -0,0 +1,7 @@
+module Shoulda # :nodoc:
+ module Matchers
+ module ActiveModel # :nodoc:
+ class CouldNotDetermineValueOutsideOfArray < RuntimeError; end
+ end
+ end
+end
View
8 spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb
@@ -12,6 +12,14 @@
end
end
+ context "where we cannot determine a value outside the array" do
+ it "should raise a custom exception" do
+ @model = define_model(:example, :attr => :string).new
+
+ expect { @model.should ensure_inclusion_of(:attr).in_array([""]) }.to raise_error Shoulda::Matchers::ActiveModel::CouldNotDetermineValueOutsideOfArray
+ end
+ end
+
context "an attribute which must be included in a range" do
before do
@model = define_model(:example, :attr => :integer) do
Please sign in to comment.
Something went wrong with that request. Please try again.