aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/enum_test.rb
diff options
context:
space:
mode:
authorTheMonster <alwahsh.ahmed@gmail.com>2014-02-26 03:24:44 +0200
committerTheMonster <alwahsh.ahmed@gmail.com>2014-02-27 23:54:49 +0200
commit6e53d92498ada27cea6c7bae85708fcf5579223c (patch)
treed2f95b5c979111c68c738a102d0ff610a4f7ea32 /activerecord/test/cases/enum_test.rb
parent14e697cf2a1f2a4d223463839f6748fca4658bcf (diff)
downloadrails-6e53d92498ada27cea6c7bae85708fcf5579223c.tar.gz
rails-6e53d92498ada27cea6c7bae85708fcf5579223c.tar.bz2
rails-6e53d92498ada27cea6c7bae85708fcf5579223c.zip
Fix a bug affecting validations of enum attributes
This fixes a bug where any enum attribute of a model would be evaluated always as 0 when calling the database on validations. This fix converts the value of the enum attribute to its integer value rather than the string before building the relation as the bug occured when the string finally gets converted to integer using string.to_i which converts it to 0. [Vilius Luneckas, Ahmed AbouElhamayed]
Diffstat (limited to 'activerecord/test/cases/enum_test.rb')
-rw-r--r--activerecord/test/cases/enum_test.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb
index 1b95708cb3..f8ebd7caee 100644
--- a/activerecord/test/cases/enum_test.rb
+++ b/activerecord/test/cases/enum_test.rb
@@ -222,4 +222,31 @@ class EnumTest < ActiveRecord::TestCase
end
end
end
+
+ test "validate uniqueness" do
+ klass = Class.new(ActiveRecord::Base) do
+ def self.name; 'Book'; end
+ enum status: [:proposed, :written]
+ validates_uniqueness_of :status
+ end
+ klass.delete_all
+ klass.create!(status: "proposed")
+ book = klass.new(status: "written")
+ assert book.valid?
+ book.status = "proposed"
+ assert_not book.valid?
+ end
+
+ test "validate inclusion of value in array" do
+ klass = Class.new(ActiveRecord::Base) do
+ def self.name; 'Book'; end
+ enum status: [:proposed, :written]
+ validates_inclusion_of :status, in: ["written"]
+ end
+ klass.delete_all
+ invalid_book = klass.new(status: "proposed")
+ assert_not invalid_book.valid?
+ valid_book = klass.new(status: "written")
+ assert valid_book.valid?
+ end
end