diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 2e619b1b5f..4a207d8995 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -187,11 +187,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| diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index 833bfa82b6..849f53851d 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -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"