OSDN Git Service

refs #1590 メール送信まわりのテストが通るように修正
authorYOSHIDA Hiroki <hyoshida@appirits.com>
Wed, 29 May 2013 07:43:36 +0000 (16:43 +0900)
committerYOSHIDA Hiroki <hyoshida@appirits.com>
Wed, 29 May 2013 08:39:22 +0000 (17:39 +0900)
config/initializers/tmail_ruby_19_patch.rb

index fddd584..776b476 100644 (file)
@@ -13,11 +13,11 @@ module TMailRuby19Patch
   module ClassMethods
     def force_encoding_for(*syms)
       syms.each do |sym|
-        define_method "#{sym}_with_force_encoding" do |text,*args|
-          enc = text.encoding
-          text.force_encoding(Encoding::ASCII_8BIT)
-          result = self.send "#{sym}_without_force_encoding", text, *args
-          text.force_encoding(enc)
+        define_method "#{sym}_with_force_encoding" do |string,*args|
+          original_encoding = string.encoding
+          string.force_encoding(Encoding::ASCII_8BIT)
+          result = self.send("#{sym}_without_force_encoding", string, *args)
+          result.force_encoding(original_encoding) if result.is_a?(String) && result.encoding == Encoding::ASCII_8BIT
           result
         end
 
@@ -43,6 +43,22 @@ module TMail
 
   # Subject欄向けのパッチ
   class Decoder
+    # NKF.nkf の返り値をASCII_8BITに強制変換するようオーバライド
+    # 理由: decode メソッドの内部で NKF.nkf の返り値(ISO-2022-JP)と
+    #       第一引数(force_encoding_forでASCII_8BITに強制変換した文字列)を結合する処理があり、
+    #       例外エラー「incompatible character encodings: ASCII-8BIT and ISO-2022-JP」が発生するため
+    module NKF
+      extend ::NKF
+      class << self
+        alias nkf_org nkf
+        def nkf(option, string)
+          result = self.send(:nkf_org, option, string)
+          result.force_encoding(Encoding::ASCII_8BIT) if string.encoding == Encoding::ASCII_8BIT
+          result
+        end
+      end
+    end
+
     class << self
       include TMailRuby19Patch
       force_encoding_for :decode
@@ -50,7 +66,7 @@ module TMail
   end
 end
  
-class StringOutput#:nodoc:
+class StringOutput
   include TMailRuby19Patch
   force_encoding_for :<<
 end