Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved validates exclusion and inclusion of from MM to validatable.

  • Loading branch information...
commit 0a7666275e8d54ebf4f97e3e75ac2cf6c43ed08f 1 parent f635913
@jnunemaker authored
View
2  lib/validatable.rb
@@ -18,5 +18,7 @@
require 'validatable/validations/validates_length_of'
require 'validatable/validations/validates_true_for'
require 'validatable/validations/validates_numericality_of'
+require 'validatable/validations/validates_exclusion_of'
+require 'validatable/validations/validates_inclusion_of'
require 'validatable/validations/validates_each'
require 'validatable/validations/validates_associated'
View
8 lib/validatable/macros.rb
@@ -193,6 +193,14 @@ def validates_presence_of(*args)
def validates_true_for(*args)
add_validations(args, ValidatesTrueFor)
end
+
+ def validates_exclusion_of(*args)
+ add_validations(args, ValidatesExclusionOf)
+ end
+
+ def validates_inclusion_of(*args)
+ add_validations(args, ValidatesInclusionOf)
+ end
# call-seq: validates_associated(*args)
#
View
17 lib/validatable/validations/validates_exclusion_of.rb
@@ -0,0 +1,17 @@
+module Validatable
+ class ValidatesExclusionOf < ValidationBase #:nodoc:
+ required_option :within
+
+ def valid?(instance)
+ value = instance.send(attribute)
+ return true if allow_nil && value.nil?
+ return true if allow_blank && value.blank?
+
+ !within.include?(value)
+ end
+
+ def message(instance)
+ super || "is reserved"
+ end
+ end
+end
View
17 lib/validatable/validations/validates_inclusion_of.rb
@@ -0,0 +1,17 @@
+module Validatable
+ class ValidatesInclusionOf < ValidationBase
+ required_option :within
+
+ def valid?(instance)
+ value = instance.send(attribute)
+ return true if allow_nil && value.nil?
+ return true if allow_blank && value.blank?
+
+ within.include?(value)
+ end
+
+ def message(instance)
+ super || "is not in the list"
+ end
+ end
+end
View
29 test/functional/test_validates_exclusion_of.rb
@@ -0,0 +1,29 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+module Functional
+ class ValidatesExclusionOfTest < Test::Unit::TestCase
+ test "given excluded state, when validated, then error is in the objects error collection" do
+ klass = Class.new do
+ include Validatable
+ attr_accessor :state
+ validates_exclusion_of :state, :within => %w(in out)
+ end
+ instance = klass.new
+ instance.state = 'in'
+ instance.valid?
+ assert_equal "is reserved", instance.errors.on(:state)
+ end
+
+ test "given non-excluded state, when validated, then no error is in the objects error collection" do
+ klass = Class.new do
+ include Validatable
+ attr_accessor :state
+ validates_exclusion_of :state, :within => %w(in out)
+ end
+ instance = klass.new
+ instance.state = 'foo'
+ instance.valid?
+ assert_nil instance.errors.on(:state)
+ end
+ end
+end
View
29 test/functional/test_validates_inclusion_of.rb
@@ -0,0 +1,29 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+module Functional
+ class ValidatesInclusionOfTest < Test::Unit::TestCase
+ test "given state not in list, when validated, then error is in the objects error collection" do
+ klass = Class.new do
+ include Validatable
+ attr_accessor :state
+ validates_inclusion_of :state, :within => %w(in out)
+ end
+ instance = klass.new
+ instance.state = 'foo'
+ instance.valid?
+ assert_equal "is not in the list", instance.errors.on(:state)
+ end
+
+ test "given state in list, when validated, then no error is in the objects error collection" do
+ klass = Class.new do
+ include Validatable
+ attr_accessor :state
+ validates_inclusion_of :state, :within => %w(in out)
+ end
+ instance = klass.new
+ instance.state = 'in'
+ instance.valid?
+ assert_nil instance.errors.on(:state)
+ end
+ end
+end
View
23 test/unit/test_validates_exclusion_of.rb
@@ -0,0 +1,23 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+Expectations do
+ expect false do
+ validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out)
+ validation.valid?(stub(:state => 'in'))
+ end
+
+ expect true do
+ validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out)
+ validation.valid?(stub(:state => 'foo'))
+ end
+
+ expect true do
+ validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out), :allow_blank => true
+ validation.valid?(stub(:state => ''))
+ end
+
+ expect true do
+ validation = Validatable::ValidatesExclusionOf.new stub_everything, :state, :within => %w(in out), :allow_nil => true
+ validation.valid?(stub(:state => nil))
+ end
+end
View
23 test/unit/test_validates_inclusion_of.rb
@@ -0,0 +1,23 @@
+require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
+
+Expectations do
+ expect true do
+ validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out)
+ validation.valid?(stub(:state => 'in'))
+ end
+
+ expect false do
+ validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out)
+ validation.valid?(stub(:state => 'foo'))
+ end
+
+ expect true do
+ validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out), :allow_blank => true
+ validation.valid?(stub(:state => ''))
+ end
+
+ expect true do
+ validation = Validatable::ValidatesInclusionOf.new stub_everything, :state, :within => %w(in out), :allow_nil => true
+ validation.valid?(stub(:state => nil))
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.