diff --git a/lib/shoulda/matchers/active_model.rb b/lib/shoulda/matchers/active_model.rb index 8c8f32fa..5121fe34 100644 --- a/lib/shoulda/matchers/active_model.rb +++ b/lib/shoulda/matchers/active_model.rb @@ -13,6 +13,7 @@ require 'shoulda/matchers/active_model/validate_acceptance_of_matcher' 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 diff --git a/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb b/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb index 02bb293f..8687eb01 100644 --- a/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb @@ -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? && 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 @@ module Shoulda # :nodoc: 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 diff --git a/lib/shoulda/matchers/active_model/errors.rb b/lib/shoulda/matchers/active_model/errors.rb new file mode 100644 index 00000000..e3b9f234 --- /dev/null +++ b/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 diff --git a/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb b/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb index 832400db..f44723d0 100644 --- a/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb +++ b/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb @@ -12,6 +12,14 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do 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