From 268c63305d5066830eb031c8affce967ac9ff77a Mon Sep 17 00:00:00 2001 From: MAEDA Go Date: Sat, 15 Sep 2018 17:44:28 +0900 Subject: [PATCH 2/2] Support IDN email addresses. --- Gemfile | 1 + app/models/email_address.rb | 4 +++- test/unit/user_test.rb | 11 +++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 69a86c716..3f0bd9abc 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,7 @@ gem "roadie", "~> 3.2.1" gem "mimemagic" gem "mail", "~> 2.6.4" gem "csv", "~> 1.0.2" if RUBY_VERSION >= "2.3" +gem "simpleidn" gem "nokogiri", "~> 1.8.0" gem "i18n", "~> 0.7.0" diff --git a/app/models/email_address.rb b/app/models/email_address.rb index 52e31c744..1ce58ade8 100644 --- a/app/models/email_address.rb +++ b/app/models/email_address.rb @@ -33,7 +33,9 @@ class EmailAddress < ActiveRecord::Base safe_attributes 'address' def address=(arg) - write_attribute(:address, arg.to_s.strip) + local_part, at, domain = arg.to_s.strip.partition('@') + ascii_address = local_part + at + SimpleIDN.to_ascii(domain) + write_attribute(:address, ascii_address) end def destroy diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 95198c148..1c9a44edb 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -84,6 +84,17 @@ class UserTest < ActiveSupport::TestCase assert_equal "foo@bar.com", u.mail end + def test_mail_should_accept_idn + u = User.new + u.mail = "maeda@испытание.test.テスト" + assert_equal "maeda@xn--80akhbyknj4f.test.xn--zckzah", u.mail + # The local-part should be encoded with Punycode + u.mail = "前田@example.jp" + assert_equal "前田@example.jp", u.mail + u.mail = "前田" + assert_equal "前田", u.mail + end + def test_should_create_email_address u = User.new(:firstname => "new", :lastname => "user") u.login = "create_email_address" -- 2.14.3 (Apple Git-98)