Permalink
Browse files

Add #allow_blank and #allow_nil options to #ensure_inclusion_of .

  • Loading branch information...
1 parent 782f70a commit 8995f6080338c4113ad4b09aa54c73be0a6d9e4b @drapergeek drapergeek committed with mike-burns May 10, 2012
View
@@ -4,6 +4,8 @@ HEAD
`ensure_inclusion_of(:attr).in_array(['foo', 'bar'])`. It cannot be used with
the `.in_range` option. (vpereira)
+* `ensure_in_inclusion_of` with `in_array` will accept `allow_blank(bool)` and `allow_nil(false)`
+
* Test against Rails 3.2.
* Fix `ensure_length_of` to use all possible I18n error messages.
@@ -1,5 +1,5 @@
PATH
- remote: /Users/gabe/thoughtbot/shoulda-matchers
+ remote: /home/mike/thoughtbot/shoulda-matchers
specs:
shoulda-matchers (1.1.0)
activesupport (>= 3.0.0)
@@ -61,7 +61,7 @@ GEM
gherkin (2.9.3)
json (>= 1.4.6)
i18n (0.5.0)
- json (1.7.1)
+ json (1.7.3)
mail (2.2.19)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
@@ -1,5 +1,5 @@
PATH
- remote: /Users/gabe/thoughtbot/shoulda-matchers
+ remote: /home/mike/thoughtbot/shoulda-matchers
specs:
shoulda-matchers (1.1.0)
activesupport (>= 3.0.0)
@@ -65,7 +65,7 @@ GEM
jquery-rails (1.0.19)
railties (~> 3.0)
thor (~> 0.14)
- json (1.7.1)
+ json (1.7.3)
mail (2.3.3)
i18n (>= 0.4.0)
mime-types (~> 1.16)
@@ -74,7 +74,7 @@ GEM
mime-types (1.18)
mocha (0.10.5)
metaclass (~> 0.0.1)
- multi_json (1.3.4)
+ multi_json (1.3.6)
polyglot (0.3.3)
rack (1.3.6)
rack-cache (1.2)
@@ -116,7 +116,7 @@ GEM
activesupport (>= 3.0)
railties (>= 3.0)
rspec (~> 2.8.0)
- sass (3.1.17)
+ sass (3.1.19)
sass-rails (3.1.6)
actionpack (~> 3.1.0)
railties (~> 3.1.0)
@@ -1,5 +1,5 @@
PATH
- remote: /Users/gabe/thoughtbot/shoulda-matchers
+ remote: /home/mike/thoughtbot/shoulda-matchers
specs:
shoulda-matchers (1.1.0)
activesupport (>= 3.0.0)
@@ -65,7 +65,7 @@ GEM
jquery-rails (2.0.2)
railties (>= 3.2.0, < 5.0)
thor (~> 0.14)
- json (1.7.1)
+ json (1.7.3)
mail (2.4.4)
i18n (>= 0.4.0)
mime-types (~> 1.16)
@@ -74,7 +74,7 @@ GEM
mime-types (1.18)
mocha (0.10.5)
metaclass (~> 0.0.1)
- multi_json (1.3.4)
+ multi_json (1.3.6)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.2)
@@ -114,7 +114,7 @@ GEM
activesupport (>= 3.0)
railties (>= 3.0)
rspec (~> 2.8.0)
- sass (3.1.17)
+ sass (3.1.19)
sass-rails (3.2.5)
railties (~> 3.2.0)
sass (>= 3.1.10)
@@ -22,6 +22,10 @@ def ensure_inclusion_of(attr)
end
class EnsureInclusionOfMatcher < ValidationMatcher # :nodoc:
+ def initialize(attribute)
+ super(attribute)
+ @options = {}
+ end
def in_array(array)
@array = array
@@ -35,6 +39,16 @@ def in_range(range)
self
end
+ def allow_blank(allow_blank = true)
+ @options[:allow_blank] = allow_blank
+ self
+ end
+
+ def allow_nil(allow_nil = true)
+ @options[:allow_nil] = allow_nil
+ self
+ end
+
def with_message(message)
if message
@low_message = message
@@ -69,7 +83,7 @@ def matches?(subject)
disallows_higher_value &&
allows_maximum_value
elsif @array
- if allows_all_values_in_array?
+ if allows_all_values_in_array? && allows_blank_value? && allows_nil_value?
true
else
@failure_message = "#{@array} doesn't match array in validation"
@@ -80,6 +94,22 @@ def matches?(subject)
private
+ def allows_blank_value?
+ if @options.key?(:allow_blank)
+ @options[:allow_blank] == allows_value_of('')
+ else
+ true
+ end
+ end
+
+ def allows_nil_value?
+ if @options.key?(:allow_nil)
+ @options[:allow_nil] == allows_value_of(nil)
+ else
+ true
+ end
+ end
+
def inspect_message
@range.nil? ? @array.inspect : @range.inspect
end
@@ -89,5 +89,35 @@ def custom_validation
it "has correct description" do
ensure_inclusion_of(:attr).in_array([true, 'dog']).description.should == 'ensure inclusion of attr in [true, "dog"]'
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
+
+ it "rejects allow_nil" do
+ @model.should_not ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_nil(true)
+ @model.should ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_nil(false)
+ end
+ end
+
+ context "allowed blank and allowed nil" do
+ before do
+ @model = define_model(:example, :attr => :string) do
+ validates_inclusion_of :attr, :in => %w(one two), :allow_blank => true, :allow_nil => true
+ end.new
+ end
+
+ it "allows allow_blank" do
+ @model.should ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_blank(true)
+ @model.should ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_blank()
+ @model.should_not ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_blank(false)
+ end
+
+ it "allows allow_nil" do
+ @model.should ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_nil(true)
+ @model.should ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_nil()
+ @model.should_not ensure_inclusion_of(:attr).in_array(['one', 'two']).allow_nil(false)
+ end
end
end

0 comments on commit 8995f60

Please sign in to comment.