Browse files

looks like its working

  • Loading branch information...
1 parent e1330dc commit e40e2cb1acf33c8716db9fecf37131aa625282bf Victor Pereira committed with gabebw Mar 13, 2012
View
43 lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb
@@ -5,6 +5,7 @@ module ActiveModel # :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 @@ def ensure_inclusion_of(attr)
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 @@ def with_high_message(message)
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
-
- disallows_lower_value &&
- allows_minimum_value &&
- disallows_higher_value &&
- allows_maximum_value
+ if @range
+ @low_message ||= :inclusion
+ @high_message ||= :inclusion
+
+ 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
2 lib/shoulda/matchers/active_model/validation_matcher.rb
@@ -18,7 +18,7 @@ def matches?(subject)
end
private
-
+
def allows_value_of(value, message = nil)
allow = AllowValueMatcher.
new(value).
View
40 spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb
@@ -68,4 +68,44 @@ def custom_validation
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

0 comments on commit e40e2cb

Please sign in to comment.