aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-02-11 14:56:26 -0700
committerSean Griffin <sean@thoughtbot.com>2015-02-11 15:02:36 -0700
commitc51f9b61ce1e167f5f58f07441adcfa117694301 (patch)
tree70a72c3c0f1d2c80bb47af483a4a56bbbf3ffd88 /activerecord/test/cases
parent5e0b555b453ea2ca36986c111512627d806101e7 (diff)
downloadrails-c51f9b61ce1e167f5f58f07441adcfa117694301.tar.gz
rails-c51f9b61ce1e167f5f58f07441adcfa117694301.tar.bz2
rails-c51f9b61ce1e167f5f58f07441adcfa117694301.zip
Refactor enum to be defined in terms of the attributes API
In addition to cleaning up the implementation, this allows type casting behavior to be applied consistently everywhere. (#where for example). A good example of this was the previous need for handling value to key conversion in the setter, because the number had to be passed to `where` directly. This is no longer required, since we can just pass the string along to where. (It's left around for backwards compat) Fixes #18387
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r--activerecord/test/cases/enum_test.rb17
1 files changed, 16 insertions, 1 deletions
diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb
index 346fcab6ea..ed568413a2 100644
--- a/activerecord/test/cases/enum_test.rb
+++ b/activerecord/test/cases/enum_test.rb
@@ -26,6 +26,17 @@ class EnumTest < ActiveRecord::TestCase
assert_equal @book, Book.unread.first
end
+ test "build from scope" do
+ assert Book.proposed.build.proposed?
+ refute Book.proposed.build.written?
+ assert Book.where(status: Book.statuses[:proposed]).build.proposed?
+ end
+
+ test "find via where" do
+ assert_equal @book, Book.where(status: "proposed").first
+ refute_equal @book, Book.where(status: "written").first
+ end
+
test "update by declaration" do
@book.written!
assert @book.written?
@@ -161,7 +172,11 @@ class EnumTest < ActiveRecord::TestCase
end
test "_before_type_cast returns the enum label (required for form fields)" do
- assert_equal "proposed", @book.status_before_type_cast
+ if @book.status_came_from_user?
+ assert_equal "proposed", @book.status_before_type_cast
+ else
+ assert_equal "proposed", @book.status
+ end
end
test "reserved enum names" do