Skip to content

Commit

Permalink
Merge pull request #41278 from fatkodima/enum-original-key
Browse files Browse the repository at this point in the history
Deserialize enum value to original hash key
  • Loading branch information
kamipo committed Jan 30, 2021
2 parents 65979c6 + c12df0d commit d5386cf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/enum.rb
Expand Up @@ -204,8 +204,8 @@ def enum(definitions)

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
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/cases/enum_test.rb
Expand Up @@ -700,6 +700,18 @@ def self.name; "Book"; end
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
Expand Down

0 comments on commit d5386cf

Please sign in to comment.