Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize enum value to original hash key #41278

Merged
merged 1 commit into from Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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