looks like its working

This commit is contained in:
Victor Pereira 2012-03-13 15:06:55 +01:00 committed by Gabe Berke-Williams
parent e1330dc8c7
commit e40e2cb1ac
3 changed files with 74 additions and 9 deletions

View File

@ -5,6 +5,7 @@ module Shoulda # :nodoc:
# Ensure that the attribute's value is in the range specified
#
# Options:
# * <tt>in_array</tt> - the range of allowed values for this attribute
# * <tt>in_range</tt> - the range of allowed values for this attribute
# * <tt>with_low_message</tt> - value the test expects to find in
# <tt>errors.on(:attribute)</tt>. Regexp or string. Defaults to the
@ -21,7 +22,12 @@ module Shoulda # :nodoc:
end
class EnsureInclusionOfMatcher < ValidationMatcher # :nodoc:
def in_array(array)
@array = array
self
end
def in_range(range)
@range = range
@minimum = range.first
@ -47,24 +53,43 @@ module Shoulda # :nodoc:
self
end
def description
"ensure inclusion of #{@attribute} in #{@range.inspect}"
"ensure inclusion of #{@attribute} in #{inspect_message}"
end
def matches?(subject)
super(subject)
@low_message ||= :inclusion
@high_message ||= :inclusion
if @range
@low_message ||= :inclusion
@high_message ||= :inclusion
disallows_lower_value &&
allows_minimum_value &&
disallows_higher_value &&
allows_maximum_value
disallows_lower_value &&
allows_minimum_value &&
disallows_higher_value &&
allows_maximum_value
elsif @array
unless allows_all_values_in_array
@failure_message = "#{@array} doesn't include #{@attribute}"
return false
end
true
end
end
private
def inspect_message
@range.nil? ? @array.inspect : @range.inspect
end
def allows_all_values_in_array
@array.any? do |value|
allows_value_of(@attribute, :inclusion)
end
end
def disallows_lower_value
@minimum == 0 || disallows_value_of(@minimum - 1, @low_message)
end

View File

@ -18,7 +18,7 @@ module Shoulda # :nodoc:
end
private
def allows_value_of(value, message = nil)
allow = AllowValueMatcher.
new(value).

View File

@ -68,4 +68,44 @@ describe Shoulda::Matchers::ActiveModel::EnsureInclusionOfMatcher do
end
context "an attribute which must be included in a array" do
before do
@model = define_model(:example_foo, :attr => :boolean) do
validates_inclusion_of :attr, :in => [true,false]
end.new(:attr=>true)
end
it "should accept ensuring the correct array" do
@model.should ensure_inclusion_of(:attr).in_array [true,false]
end
it "should have the array in the description" do
ensure_inclusion_of(:attr).in_array([true,false]).description.should == "ensure inclusion of attr in [true, false]"
end
end
context "an attribute not included in the array" do
before do
@model = define_model(:example_foo, :attr => :boolean) do
validate :custom_validation
def custom_validation
unless [true,false].include? attr
errors.add(:attr, 'not boolean')
end
end
end.new
end
it "should have an error on attr" do
@model.should ensure_inclusion_of(:attr).in_array([true,false]).with_message(/boolean/)
end
context "should not accept other value then specified" do
before do
@model.attr = "foo"
end
it "should not be valid" do
@model.should ensure_inclusion_of(:attr).in_array([true,false]).with_message(/inclusion/)
end
end
end
end