Skip to content
Browse files

[#120] Test more blank values with allow_blank

  • Loading branch information...
1 parent 2c214e4 commit 12121b8b852c22ad7e483a4b855162e1ac63e9e8 @grantovich grantovich committed Sep 21, 2012
View
5 lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb
@@ -96,7 +96,10 @@ def matches?(subject)
def allows_blank_value?
if @options.key?(:allow_blank)
- @options[:allow_blank] == allows_value_of('')
+ blank_values = ['', ' ', "\n", "\r", "\t", "\f"]
+ @options[:allow_blank] == blank_values.inject(true) do |memo, value|
+ memo &&= allows_value_of(value)
+ end
else
true
end
View
13 spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb
@@ -121,6 +121,19 @@ def custom_validation
end
end
+ context "an attribute allowing some blank values but not others" do
+ before do
+ @model = define_model(:example, :attr => :string) do
+ validates_inclusion_of :attr, :in => ['one', 'two', '']
+ end.new
+ end
+
+ it "rejects allow_blank" do
+ @model.should_not ensure_inclusion_of(:attr).in_array(['one', 'two', '']).allow_blank(true)
+ @model.should ensure_inclusion_of(:attr).in_array(['one', 'two', '']).allow_blank(false)
+ end
+ end
+
if Rails::VERSION::STRING.to_f >= 3.2
context "a strict attribute which must be included in a range" do
before do

0 comments on commit 12121b8

Please sign in to comment.
Something went wrong with that request. Please try again.