Skip to content

Commit

Permalink
[#120] Test more blank values with allow_blank
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalcora committed Sep 21, 2012
1 parent 2c214e4 commit 12121b8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit 12121b8

Please sign in to comment.