Skip to content

Commit 35ecdf0

Browse files
authored
Merge pull request rails#40974 from kamipo/dont_mangle_enum_names_for_unicode_letters
Don't mangle callable enum names
2 parents 0cd0467 + af02185 commit 35ecdf0

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

activerecord/lib/active_record/enum.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def enum(definitions)
202202
suffix = "_#{enum_suffix}"
203203
end
204204

205-
method_friendly_label = label.to_s.gsub(/\W+/, "_")
205+
method_friendly_label = label.to_s.gsub(/[\W&&[:ascii:]]+/, "_")
206206
value_method_name = "#{prefix}#{method_friendly_label}#{suffix}"
207207
value_method_names << value_method_name
208208
enum_values[label] = value

activerecord/test/cases/enum_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,17 @@ def self.name; "Book"; end
646646
assert_not_predicate computer, :extendedGold?
647647
end
648648

649+
test "unicode characters for enum names" do
650+
klass = Class.new(ActiveRecord::Base) do
651+
self.table_name = "books"
652+
enum language: [:🇺🇸, :🇪🇸, :🇫🇷]
653+
end
654+
655+
book = klass.🇺🇸.build
656+
assert_predicate book, :🇺🇸?
657+
assert_not_predicate book, :🇪🇸?
658+
end
659+
649660
test "enum logs a warning if auto-generated negative scopes would clash with other enum names" do
650661
old_logger = ActiveRecord::Base.logger
651662
logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new

0 commit comments

Comments
 (0)