Project

General

Profile

Feature #29208 » support-idn-email-addresses-v4.diff

Go MAEDA, 2018-07-08 07:40

View differences:

Gemfile (working copy)
14 14
gem "mimemagic"
15 15
gem "mail", "~> 2.6.4"
16 16
gem "csv", "~> 1.0.2" if RUBY_VERSION >= "2.3"
17
gem "simpleidn"
17 18

  
18 19
gem "nokogiri", "~> 1.8.0"
19 20
gem "i18n", "~> 0.7.0"
app/models/email_address.rb (working copy)
25 25
  after_destroy :destroy_tokens, :deliver_security_notification_destroy
26 26

  
27 27
  validates_presence_of :address
28
  validates_format_of :address, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, :allow_blank => true
28
  validates_format_of :address, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+(xn--)?[a-z0-9]{2,})\z/i, :allow_blank => true
29 29
  validates_length_of :address, :maximum => User::MAIL_LENGTH_LIMIT, :allow_nil => true
30 30
  validates_uniqueness_of :address, :case_sensitive => false,
31 31
    :if => Proc.new {|email| email.address_changed? && email.address.present?}
......
33 33
  safe_attributes 'address'
34 34

  
35 35
  def address=(arg)
36
    write_attribute(:address, arg.to_s.strip)
36
    local_part, at, domain = arg.to_s.strip.partition('@')
37
    ascii_address = local_part + at + SimpleIDN.to_ascii(domain)
38
    write_attribute(:address, ascii_address)
37 39
  end
38 40

  
39 41
  def destroy
test/unit/user_test.rb (working copy)
84 84
    assert_equal "foo@bar.com", u.mail
85 85
  end
86 86

  
87
  def test_mail_should_accept_idn
88
    u = User.new
89
    u.mail = "maeda@испытание.test.テスト"
90
    assert_equal "maeda@xn--80akhbyknj4f.test.xn--zckzah", u.mail
91
    # The local-part should be encoded with Punycode
92
    u.mail = "前田@example.jp"
93
    assert_equal "前田@example.jp", u.mail
94
    u.mail = "前田"
95
    assert_equal "前田", u.mail
96
  end
97

  
87 98
  def test_should_create_email_address
88 99
    u = User.new(:firstname => "new", :lastname => "user")
89 100
    u.login = "create_email_address"
(6-6/10)