Skip to content

Commit d4583af

Browse files
authored
Merge pull request rails#43529 from elfassy/enum_with_strings
Enum with strings
2 parents d8d84f0 + 3db736b commit d4583af

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

activerecord/lib/active_record/enum.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,20 @@ module ActiveRecord
5757
# conversation = Conversation.new
5858
# conversation.status # => "active"
5959
#
60-
# Finally, it's also possible to explicitly map the relation between attribute and
60+
# It's possible to explicitly map the relation between attribute and
6161
# database integer with a hash:
6262
#
6363
# class Conversation < ActiveRecord::Base
6464
# enum :status, active: 0, archived: 1
6565
# end
6666
#
67+
# Finally it's also possible to use a string column to persist the enumerated value.
68+
# Note that this will likely lead to slower database queries:
69+
#
70+
# class Conversation < ActiveRecord::Base
71+
# enum :status, active: "active", archived: "archived"
72+
# end
73+
#
6774
# Note that when an array is used, the implicit mapping from the values to database
6875
# integers is derived from the order the values appear in the array. In the example,
6976
# <tt>:active</tt> is mapped to +0+ as it's the first element, and <tt>:archived</tt>

activerecord/test/cases/enum_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class EnumTest < ActiveRecord::TestCase
7070
assert_equal "visible", @book.author_visibility
7171
assert_equal "visible", @book.illustrator_visibility
7272
assert_equal "medium", @book.difficulty
73+
assert_equal "soft", @book.cover
7374
end
7475

7576
test "find via scope" do
@@ -108,6 +109,7 @@ class EnumTest < ActiveRecord::TestCase
108109
assert_not_equal @book, Book.where(status: [written, written]).first
109110
assert_not_equal @book, Book.where.not(status: published).first
110111
assert_equal @book, Book.where.not(status: written).first
112+
assert_equal @book, Book.where(cover: Book.covers[:soft]).first
111113
end
112114

113115
test "find via where with symbols" do
@@ -119,6 +121,8 @@ class EnumTest < ActiveRecord::TestCase
119121
assert_equal @book, Book.where.not(status: :written).first
120122
assert_equal books(:ddd), Book.where(last_read: :forgotten).first
121123
assert_nil Book.where(status: :prohibited).first
124+
assert_equal @book, Book.where(cover: :soft).first
125+
assert_equal @book, Book.where.not(cover: :hard).first
122126
end
123127

124128
test "find via where with strings" do
@@ -145,6 +149,8 @@ class EnumTest < ActiveRecord::TestCase
145149

146150
enabled = Book.boolean_statuses[:enabled].to_s
147151
assert_equal book, Book.where(boolean_status: enabled).last
152+
assert_equal @book, Book.where(cover: "soft").first
153+
assert_equal @book, Book.where.not(cover: "hard").first
148154
end
149155

150156
test "build from scope" do
@@ -170,11 +176,15 @@ class EnumTest < ActiveRecord::TestCase
170176
assert_predicate @book, :in_english?
171177
@book.author_visibility_visible!
172178
assert_predicate @book, :author_visibility_visible?
179+
@book.hard!
180+
assert_predicate @book, :hard?
173181
end
174182

175183
test "update by setter" do
176184
@book.update! status: :written
177185
assert_predicate @book, :written?
186+
@book.update! cover: :hard
187+
assert_predicate @book, :hard?
178188
end
179189

180190
test "enum methods are overwritable" do
@@ -185,11 +195,15 @@ class EnumTest < ActiveRecord::TestCase
185195
test "direct assignment" do
186196
@book.status = :written
187197
assert_predicate @book, :written?
198+
@book.cover = :hard
199+
assert_predicate @book, :hard?
188200
end
189201

190202
test "assign string value" do
191203
@book.status = "written"
192204
assert_predicate @book, :written?
205+
@book.cover = "hard"
206+
assert_predicate @book, :hard?
193207
end
194208

195209
test "enum changed attributes" do

activerecord/test/fixtures/books.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ awdr:
1111
font_size: :medium
1212
difficulty: :medium
1313
boolean_status: :enabled
14+
cover: "soft"
1415

1516
rfr:
1617
author_id: 1

0 commit comments

Comments
 (0)