1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #41164 from alpaca-tc/fixed_enum

Fixed multiple enums declaration with _prefix/_suffix
This commit is contained in:
Ryuta Kamizono 2021-01-19 15:38:20 +09:00 committed by GitHub
commit 098fd7f9b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 4 deletions

View file

@ -190,11 +190,17 @@ module ActiveRecord
value_method_names = []
_enum_methods_module.module_eval do
enum_prefix = name if enum_prefix == true
prefix = "#{enum_prefix}_" if enum_prefix
prefix = if enum_prefix == true
"#{name}_"
elsif enum_prefix
"#{enum_prefix}_"
end
enum_suffix = name if enum_suffix == true
suffix = "_#{enum_suffix}" if enum_suffix
suffix = if enum_suffix == true
"_#{name}"
elsif enum_suffix
"_#{enum_suffix}"
end
pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
pairs.each do |label, value|

View file

@ -511,6 +511,38 @@ class EnumTest < ActiveRecord::TestCase
assert_predicate book2, :single?
end
test "declare multiple enums with { _prefix: true }" do
klass = Class.new(ActiveRecord::Base) do
self.table_name = "books"
enum(
status: [:value_1],
last_read: [:value_1],
_prefix: true
)
end
instance = klass.new
assert_respond_to instance, :status_value_1?
assert_respond_to instance, :last_read_value_1?
end
test "declare multiple enums with { _suffix: true }" do
klass = Class.new(ActiveRecord::Base) do
self.table_name = "books"
enum(
status: [:value_1],
last_read: [:value_1],
_suffix: true
)
end
instance = klass.new
assert_respond_to instance, :value_1_status?
assert_respond_to instance, :value_1_last_read?
end
test "enum with alias_attribute" do
klass = Class.new(ActiveRecord::Base) do
self.table_name = "books"