mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #41278 from fatkodima/enum-original-key
Deserialize enum value to original hash key
This commit is contained in:
commit
d5386cfba2
2 changed files with 13 additions and 1 deletions
|
@ -204,8 +204,8 @@ module ActiveRecord
|
|||
|
||||
pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
|
||||
pairs.each do |label, value|
|
||||
label = label.to_s
|
||||
enum_values[label] = value
|
||||
label = label.to_s
|
||||
|
||||
value_method_name = "#{prefix}#{label}#{suffix}"
|
||||
value_method_names << value_method_name
|
||||
|
|
|
@ -700,6 +700,18 @@ class EnumTest < ActiveRecord::TestCase
|
|||
assert_not_predicate computer, :"Etc/GMT-1?"
|
||||
end
|
||||
|
||||
test "deserialize enum value to original hash key" do
|
||||
proposed = Class.new
|
||||
written = Class.new
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
self.table_name = "books"
|
||||
enum status: { proposed => 0, written => 1 }
|
||||
end
|
||||
|
||||
book = klass.create!(status: 0)
|
||||
assert_equal proposed, book.status
|
||||
end
|
||||
|
||||
test "enum logs a warning if auto-generated negative scopes would clash with other enum names" do
|
||||
old_logger = ActiveRecord::Base.logger
|
||||
logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
|
||||
|
|
Loading…
Reference in a new issue