mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fixed multiple enums declaration with _prefix/_suffix
enum is broken when multiple values and `_prefix/_suffix: true` options are passed. Because the `enum_prefix/enum_suffix` variables are overwritten from `true` to `name` during the first loop, and one should return `true` at the second loop, got already overwritten. Introduced by https://github.com/rails/rails/pull/40992
This commit is contained in:
parent
f250208dd4
commit
f96de8bb66
2 changed files with 42 additions and 4 deletions
|
@ -187,11 +187,17 @@ module ActiveRecord
|
||||||
|
|
||||||
value_method_names = []
|
value_method_names = []
|
||||||
_enum_methods_module.module_eval do
|
_enum_methods_module.module_eval do
|
||||||
enum_prefix = name if enum_prefix == true
|
prefix = if enum_prefix == true
|
||||||
prefix = "#{enum_prefix}_" if enum_prefix
|
"#{name}_"
|
||||||
|
elsif enum_prefix
|
||||||
|
"#{enum_prefix}_"
|
||||||
|
end
|
||||||
|
|
||||||
enum_suffix = name if enum_suffix == true
|
suffix = if enum_suffix == true
|
||||||
suffix = "_#{enum_suffix}" if enum_suffix
|
"_#{name}"
|
||||||
|
elsif enum_suffix
|
||||||
|
"_#{enum_suffix}"
|
||||||
|
end
|
||||||
|
|
||||||
pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
|
pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index
|
||||||
pairs.each do |label, value|
|
pairs.each do |label, value|
|
||||||
|
|
|
@ -511,6 +511,38 @@ class EnumTest < ActiveRecord::TestCase
|
||||||
assert_predicate book2, :single?
|
assert_predicate book2, :single?
|
||||||
end
|
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
|
test "enum with alias_attribute" do
|
||||||
klass = Class.new(ActiveRecord::Base) do
|
klass = Class.new(ActiveRecord::Base) do
|
||||||
self.table_name = "books"
|
self.table_name = "books"
|
||||||
|
|
Loading…
Reference in a new issue