OSDN Git Service

t#30220:i18n system error message
authoryasushiito <yas@pen-chan.jp>
Tue, 18 Dec 2012 10:22:46 +0000 (19:22 +0900)
committeryasushiito <yas@pen-chan.jp>
Tue, 18 Dec 2012 10:22:46 +0000 (19:22 +0900)
19 files changed:
app/models/license.rb
app/models/license_group.rb
app/models/original_picture.rb
app/models/panel.rb
app/models/resource_picture.rb
app/models/system_picture.rb
app/views/original_pictures/_form.html.erb
config/locales/pettanr.ja.yml
lib/local_picture.rb
lib/picture_io.rb
lib/s3_picture.rb
lib/test/local_picture_spec.rb
lib/test/s3_picture_spec.rb
spec/controllers/resource_pictures_controller_spec.rb
spec/models/license_spec.rb
spec/models/original_picture_spec.rb
spec/models/panel_spec.rb
spec/models/resource_picture_spec.rb
spec/models/system_picture_spec.rb

index ff138c1..9505026 100644 (file)
@@ -51,7 +51,7 @@ class License < ActiveRecord::Base
     attr['credit_pictures'] = attr['credit_pictures'].to_json
     l = License.modify_object name, attr
     if r == false
-      l.errors.add :base, 'system picture can not create'
+      l.errors.add :base, SystemPicture.model_name.human + I18n.t('errors.not_create')
     else
       l.save
     end
index 19d8dfa..eda792f 100644 (file)
@@ -53,7 +53,7 @@ class LicenseGroup < ActiveRecord::Base
     r.save
     #取っておいたライセンスデータとidでライセンス作成
     if (c = License.stores(lattr, r.id)) > 0
-      r.errors.add :base, 'licenses can not create'
+      r.errors.add :base, License.model_name.human + I18n.t('errors.not_create')
     end
     r
   end
index 2866687..cfe8540 100644 (file)
@@ -128,17 +128,18 @@ class OriginalPicture < ActiveRecord::Base
   
   def store(imager)
     unless imager
-      self.errors.add :base, 'illegal picture data'
+      self.errors.add :base, I18n.t('errors.invalid_image')
       return false
     end
     res = false
     self.attributes = {:ext => imager.ext, :width => imager.width, :height => imager.height, :filesize => imager.filesize, :md5 => imager.md5}
     OriginalPicture.transaction do
       if res = self.save
-        if res = PictureIO.original_picture_io.put(imager.binary, self.filename)
-          res = true
-        else
-          self.errors.add :base, 'original picture io does not work'
+        begin
+          res = PictureIO.original_picture_io.put(imager.binary, self.filename)
+        rescue PictureIO::Error
+          res = false
+          self.errors.add :base, I18n.t('picture_io.error')
           raise ActiveRecord::Rollback
         end
       end
index ce4c788..3e7e93c 100644 (file)
@@ -250,7 +250,7 @@ class Panel < ActiveRecord::Base
   
   def store attr, au
     if attr == false
-      self.errors.add :base, 'invalid json'
+      self.errors.add :base, I18n.t('errors.invalid_json')
       return false
     end
     self.attributes = attr
@@ -258,7 +258,7 @@ class Panel < ActiveRecord::Base
     res = false
     Panel.transaction do
       unless validate_child
-        self.errors.add :base , 'invalid time'
+        self.errors.add :base, I18n.t('errors.invalid_t')
         raise ActiveRecord::Rollback
       end
       res = self.save
index 663c78b..c8bd3f7 100644 (file)
@@ -145,7 +145,7 @@ class ResourcePicture < ActiveRecord::Base
     pc.overwrite self
     r = pc.store imager
     return pc if r
-    self.errors.add :base, 'picture does not create' + pc.errors.full_messages.join("\n")
+    self.errors.add :base, Picture.model_name.human + I18n.t('errors.not_create')
     false
   end
   
@@ -171,14 +171,17 @@ class ResourcePicture < ActiveRecord::Base
     if res = self.store_picture(imager, self.filename)
       if self.to_gif?
         if gifimager = imager.to_gif
-          res = self.store_picture(gifimager, self.gifname)
+          if res = self.store_picture(gifimager, self.gifname)
+          else
+            self.errors.add :base, I18n.t('picture_io.error')
+          end
         else
-          self.errors.add :base, 'picture data can not conv to gif'
+          self.errors.add :base, I18n.t('errors.not_convert')
           res = false
         end
       end
     else
-      self.errors.add :base, 'resource picture io does not work'
+      self.errors.add :base, I18n.t('picture_io.error')
     end
     res
   end
@@ -187,9 +190,14 @@ class ResourcePicture < ActiveRecord::Base
     res = false
     thumbnail_imager = self.flag_thumbnail >= 0 ? imager.to_thumbnail : imager
     return false unless thumbnail_imager
-    return false unless PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
-    return false unless PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
-    true
+    begin
+      PictureIO.resource_picture_io.put(thumbnail_imager.binary, fn)
+      PictureIO.resource_picture_io.put(imager.binary, fn, 'full')
+      res = true
+    rescue PictureIO::Error
+      res = false
+    end
+    res
   end
   
   def restore(subdir = nil)
index cd4809b..631b3c5 100644 (file)
@@ -111,10 +111,11 @@ class SystemPicture < ActiveRecord::Base
     res = false
     SystemPicture.transaction do
       if res = self.save
-        if res = PictureIO.system_picture_io.put(imager.binary, self.filename)
-          res = true
-        else
-          self.errors.add :base, 'system picture io does not work'
+        begin
+          res = PictureIO.system_picture_io.put(imager.binary, self.filename)
+        rescue PictureIO::Error
+          res = false
+          self.errors.add :base, I18n.t('picture_io.error')
           raise ActiveRecord::Rollback
         end
       end
index 8ccc26a..b6295da 100644 (file)
@@ -1,5 +1,8 @@
 <% if @artist -%>
   <%= form_tag( original_pictures_path , { :multipart => true, :method => :post }) do %>
+    <% if @original_picture -%>
+      <%= render 'system/error_explanation', :obj => @original_picture %>
+    <% end -%>
     <%= file_field_tag "original_picture[file]" %>
     <%= submit_tag t 'original_pictures.upload' -%>
   <% end -%>
index 3b1e072..7422996 100644 (file)
@@ -1,4 +1,10 @@
 ja:
+  errors:
+    not_create: が作成できません。
+    invalid_json: 不正なjsonデータです。
+    invalid_t: コマ要素の話順が不正です。
+    not_convert: Gif画像へのコンバートに失敗しました。
+    invalid_image: 画像として読み込めませんでした。
   activerecord:
     models:
       user: ユーザ
@@ -270,6 +276,8 @@ ja:
       provider_original_picture:
         created_at: 作成
         updated_at: 更新
+  picture_io:
+    error: 画像保管庫との手続きで異常が発生しました。
   select_items:
     comic:
       visible:
index 262b9bd..bdaf976 100644 (file)
@@ -33,7 +33,7 @@ class PictureIO
       begin
         res = File.exist?(dir(subdir) + filename)
       rescue
-        res = false
+        raise PictureIO::Error
       end
       res
     end
@@ -46,7 +46,7 @@ class PictureIO
           f.write bindata
         end
       rescue
-        res = false
+        raise PictureIO::Error
       end
       res
     end
@@ -58,7 +58,7 @@ class PictureIO
           bindata += f.read
         end
       rescue
-        bindata = false
+        raise PictureIO::Error
       end
       bindata
     end
@@ -68,7 +68,7 @@ class PictureIO
       begin
         File.delete(dir(subdir) + filename)
       rescue
-        res = false
+        raise PictureIO::Error
       end
       res
     end
index 4b96477..2b323e4 100644 (file)
@@ -1,5 +1,7 @@
 class PictureIO
-  
+  class Error < StandardError
+  end
+
   def self.original_picture_io
     @@original_picture_io
   end
index 4985a54..67bb377 100644 (file)
@@ -32,7 +32,7 @@ class PictureIO
       begin
         res = AWS::S3::S3Object.exist?(dir(subdir) + filename, base)
       rescue
-        res = false
+        raise PictureIO::Error
       end
       res
     end
@@ -42,7 +42,7 @@ class PictureIO
       begin
         AWS::S3::S3Object.store(dir(subdir) + filename, bindata, base)
       rescue
-        res = false
+        raise PictureIO::Error
       end
       res
     end
@@ -54,7 +54,7 @@ class PictureIO
           bindata += st if st
         end
       rescue
-        bindata = false
+        raise PictureIO::Error
       end
       bindata
     end
@@ -64,7 +64,7 @@ class PictureIO
       begin
         AWS::S3::S3Object.delete(dir(subdir) + filename, base)
       rescue
-        res = false
+        raise PictureIO::Error
       end
       res
   end
index acb4733..6f5a610 100644 (file)
@@ -32,8 +32,11 @@ describe PictureIO::LocalPicture do
       end
     end
     context 'ファイルがないとき' do
-      it 'falseが返る' do
-        @io.exist?('not.exist').should be_false
+      it 'PictureIO::Error例外が発生する' do
+        File.stub(:exist?).with(any_args()).and_raise(StandardError)
+        lambda {
+          @io.exist?('not.exist')
+        }.should raise_error(PictureIO::Error)
       end
     end
   end
@@ -58,9 +61,11 @@ describe PictureIO::LocalPicture do
       end
     end
     context '例外が発生するとき' do
-      it 'falseが返る' do
+      it 'PictureIO::Error例外が発生する' do
         File.stub(:open).with(any_args()).and_raise(StandardError)
-        @io.get('test.get').should be_false
+        lambda {
+          r = @io.get('test.get')
+        }.should raise_error(PictureIO::Error)
       end
     end
   end
@@ -109,9 +114,11 @@ describe PictureIO::LocalPicture do
       end
     end
     context '例外が発生するとき' do
-      it 'falseが返る' do
+      it 'PictureIO::Error例外が発生する' do
         File.stub(:open).with(any_args()).and_raise(StandardError)
-        @io.put('bindata', 'test.put').should be_false
+        lambda {
+          @io.put('bindata', 'test.put')
+        }.should raise_error(PictureIO::Error)
       end
     end
   end
@@ -138,9 +145,11 @@ describe PictureIO::LocalPicture do
       end
     end
     context '例外が発生するとき' do
-      it 'falseが返る' do
+      it 'PictureIO::Error例外が発生する' do
         File.stub(:delete).with(any_args()).and_raise(StandardError)
-        @io.delete('test.del.not.exist').should be_false
+        lambda {
+          @io.delete('test.del.not.exist')
+        }.should raise_error(PictureIO::Error)
       end
     end
   end
index 5d8eee7..b6fcfac 100644 (file)
@@ -55,9 +55,11 @@ describe PictureIO::S3Picture do
       end
     end
     context '例外が発生するとき' do
-      it 'falseが返る' do
+      it 'PictureIO::Error例外が発生する' do
         AWS::S3::S3Object.stub(:exist?).with(any_args()).and_raise(StandardError)
-        @io.exist?('test.get').should be_false
+        lambda {
+          @io.exist?('test.get')
+        }.should raise_error(PictureIO::Error)
       end
     end
   end
@@ -90,9 +92,11 @@ describe PictureIO::S3Picture do
       end
     end
     context '例外が発生するとき' do
-      it 'falseが返る' do
+      it 'PictureIO::Error例外が発生する' do
         AWS::S3::S3Object.stub(:get).with(any_args()).and_raise(StandardError)
-        @io.get('test.get').should be_false
+        lambda {
+          @io.get('test.get')
+        }.should raise_error(PictureIO::Error)
       end
     end
   end
@@ -116,9 +120,11 @@ describe PictureIO::S3Picture do
       end
     end
     context '例外が発生するとき' do
-      it 'falseが返る' do
+      it 'PictureIO::Error例外が発生する' do
         AWS::S3::S3Object.stub(:store).with(any_args()).and_raise(StandardError)
-        @io.put('bindata', 'test.put').should be_false
+        lambda {
+          @io.put('bindata', 'test.put')
+        }.should raise_error(PictureIO::Error)
       end
     end
   end
@@ -142,9 +148,11 @@ describe PictureIO::S3Picture do
       end
     end
     context '例外が発生するとき' do
-      it 'falseが返る' do
+      it 'PictureIO::Error例外が発生する' do
         AWS::S3::S3Object.stub(:delete).with(any_args()).and_raise(StandardError)
-        @io.delete('test.del.not.exist').should be_false
+        lambda {
+          @io.delete('test.del.not.exist')
+        }.should raise_error(PictureIO::Error)
       end
     end
   end
index 0854814..86ce8a1 100644 (file)
@@ -840,6 +840,8 @@ describe ResourcePicturesController do
     end
     context '検証、保存に失敗した' do
       before do
+        OriginalPicture.any_instance.stub(:restore).with(any_args()).and_return(@imager.binary)
+        PettanImager.stub(:load).with(@imager.binary).and_return(@imager)
         ResourcePicture.any_instance.stub(:store).and_return(false)
       end
       it "未保存の素材を保持している" do
index a81390f..c30123c 100644 (file)
@@ -32,14 +32,14 @@ describe License do
         @l.license_group_id = ''
         @l.should_not be_valid
       end
-      it '数値でなければ失敗する' do\r
-        @l.license_group_id = 'a'\r
-        @l.should_not be_valid\r
-      end\r
-      it '存在するライセンスグループでなければ失敗する' do\r
-        @l.license_group_id = 0\r
-        @l.should_not be_valid\r
-      end\r
+      it '数値でなければ失敗する' do
+        @l.license_group_id = 'a'
+        @l.should_not be_valid
+      end
+      it '存在するライセンスグループでなければ失敗する' do
+        @l.license_group_id = 0
+        @l.should_not be_valid
+      end
     end
     context 'nameを検証するとき' do
       it 'nullなら失敗する' do
@@ -75,8 +75,8 @@ describe License do
         @l.should_not be_valid
       end
       it 'url形式でないなら失敗する' do
-        @l.url = 'a'*200\r
-        @l.should_not be_valid\r
+        @l.url = 'a'*200
+        @l.should_not be_valid
       end
     end
     context 'system_picture_idを検証するとき' do
@@ -84,14 +84,14 @@ describe License do
         @l.system_picture_id = ''
         @l.should_not be_valid
       end
-      it '数値でなければ失敗する' do\r
-        @l.system_picture_id = 'a'\r
-        @l.should_not be_valid\r
-      end\r
-      it '存在するシステム画像でなければ失敗する' do\r
-        @l.system_picture_id = 0\r
-        @l.should_not be_valid\r
-      end\r
+      it '数値でなければ失敗する' do
+        @l.system_picture_id = 'a'
+        @l.should_not be_valid
+      end
+      it '存在するシステム画像でなければ失敗する' do
+        @l.system_picture_id = 0
+        @l.should_not be_valid
+      end
     end
   end
   
@@ -124,13 +124,13 @@ describe License do
       @l = FactoryGirl.create :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
       @lg2 = FactoryGirl.create :license_group, :name => 'pubdm'
     end
-    context 'つつがなく終わるとき' do\r
-      it '一覧取得オプションを利用している' do\r
-        License.stub(:list_opt).with(any_args).and_return({})\r
-        License.should_receive(:list_opt).with(any_args).exactly(1)\r
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        License.stub(:list_opt).with(any_args).and_return({})
+        License.should_receive(:list_opt).with(any_args).exactly(1)
         r = License.list
-      end\r
-    end\r
+      end
+    end
     it 'リストを返す' do
       l = License.list 
       l.should eq [@l]
@@ -176,24 +176,24 @@ describe License do
       @lg = FactoryGirl.create :license_group
       @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
     end
-    context 'つつがなく終わるとき' do\r
-      it '単体取得オプションを利用している' do\r
-        License.stub(:show_opt).with(any_args).and_return({})\r
-        License.should_receive(:show_opt).with(any_args).exactly(1)\r
+    context 'つつがなく終わるとき' do
+      it '単体取得オプションを利用している' do
+        License.stub(:show_opt).with(any_args).and_return({})
+        License.should_receive(:show_opt).with(any_args).exactly(1)
         r = License.show @l.id
-      end\r
-    end\r
+      end
+    end
     it '指定のライセンスを返す' do
       l = License.show @l.id
       l.should eq @l
     end
-    context '存在しないライセンスを開こうとしたとき' do\r
-      it '404RecordNotFound例外を返す' do\r
-        lambda{\r
-          License.show 110\r
-        }.should raise_error(ActiveRecord::RecordNotFound)\r
-      end\r
-    end\r
+    context '存在しないライセンスを開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          License.show 110
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
   end
   describe '単体取得オプションに於いて' do
     it 'includeキーを含んでいる' do
index 18ed1c4..28b8119 100644 (file)
@@ -655,7 +655,7 @@ describe OriginalPicture do
     context '原画の保存に失敗したとき' do
       before do
         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
-        PictureIO.original_picture_io.stub(:put).with(any_args).and_return(false)
+        PictureIO.original_picture_io.stub(:put).with(any_args).and_raise(PictureIO::Error)
       end
       it 'falseを返す' do
         @op.store(@imager).should be_false
index 15e256b..ea6374b 100644 (file)
@@ -1136,13 +1136,21 @@ describe Panel do
         }.should change(Panel, :count)\r
       end\r
     end\r
+    context '不正なjsonデータのとき' do\r
+      before do\r
+      end\r
+      it 'エラーメッセージがセットされている' do\r
+        r = @panel.store false, @author\r
+        @panel.errors[:base].should_not be_blank\r
+      end\r
+    end\r
     context '従属データの検証に失敗したとき' do\r
       before do\r
         Panel.any_instance.stub(:validate_child).with(any_args).and_return(false)\r
       end\r
       it 'エラーメッセージがセットされている' do\r
         r = @panel.store @attr, @author\r
-        @panel.errors.should_not be_empty\r
+        @panel.errors[:base].should_not be_blank\r
       end\r
     end\r
     context 'カラム値がFalseしたとき' do\r
index e39a6ac..f614348 100644 (file)
@@ -824,6 +824,11 @@ describe ResourcePicture do
         r = @rp.store_picture_with_gif @imager\r
         r.should be_false\r
       end\r
+      it '全体エラーメッセージがセットされている' do\r
+        lambda {\r
+          r = @rp.store_picture_with_gif @imager\r
+        }.should change(@rp.errors[:base], :count)\r
+      end\r
       it 'gif変換判定は呼ばれていない' do\r
         ResourcePicture.any_instance.should_not_receive(:to_gif?).with(any_args)\r
         r = @rp.store_picture_with_gif @imager\r
@@ -839,6 +844,11 @@ describe ResourcePicture do
         r = @rp.store_picture_with_gif @imager\r
         r.should be_false\r
       end\r
+      it '全体エラーメッセージがセットされている' do\r
+        lambda {\r
+          r = @rp.store_picture_with_gif @imager\r
+        }.should change(@rp.errors[:base], :count)\r
+      end\r
       it 'gif画像の保存は呼ばれていない' do\r
         #本画像の保存があるので、一度は呼ばれる\r
         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)\r
@@ -859,6 +869,11 @@ describe ResourcePicture do
         r = @rp.store_picture_with_gif @imager\r
         r.should be_false\r
       end\r
+      it '全体エラーメッセージがセットされている' do\r
+        lambda {\r
+          r = @rp.store_picture_with_gif @imager\r
+        }.should change(@rp.errors[:base], :count)\r
+      end\r
     end\r
   end\r
   \r
@@ -926,7 +941,7 @@ describe ResourcePicture do
         ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)\r
         @tmbimager = @imager.to_thumbnail\r
         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(@tmbimager)\r
-        PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_return(false)\r
+        PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_raise(PictureIO::Error)\r
       end\r
       it 'Falseを返す' do\r
         r = @rp.store_picture(@imager, '1.gif')\r
@@ -943,7 +958,7 @@ describe ResourcePicture do
         @tmbimager = @imager.to_thumbnail\r
         ImagerTest.any_instance.stub(:to_thumbnail).with(any_args).and_return(@tmbimager)\r
         PictureIO.resource_picture_io.stub(:put).with(@tmbimager.binary, '1.gif').and_return(true)\r
-        PictureIO.resource_picture_io.stub(:put).with(@imager.binary, '1.gif', 'full').and_return(false)\r
+        PictureIO.resource_picture_io.stub(:put).with(@imager.binary, '1.gif', 'full').and_raise(PictureIO::Error)\r
       end\r
       it 'Falseを返す' do\r
         r = @rp.store_picture(@imager, '1.gif')\r
index 41523ba..de784bd 100644 (file)
@@ -233,7 +233,7 @@ describe SystemPicture do
         @sp3 = FactoryGirl.create :system_picture, :updated_at => Time.now + 200
         @sp4 = FactoryGirl.create :system_picture, :updated_at => Time.now + 300
         @sp5 = FactoryGirl.create :system_picture, :updated_at => Time.now + 400
-        SystemPicture.stub(:default_page_size).and_return(2)\r
+        SystemPicture.stub(:default_page_size).and_return(2)
       end
       it '通常は2件を返す' do
         r = SystemPicture.list
@@ -259,7 +259,7 @@ describe SystemPicture do
         @sp3 = FactoryGirl.create :system_picture, :updated_at => Time.now + 200
         @sp4 = FactoryGirl.create :system_picture, :updated_at => Time.now + 300
         @sp5 = FactoryGirl.create :system_picture, :updated_at => Time.now + 400
-        SystemPicture.stub(:default_page_size).and_return(2)\r
+        SystemPicture.stub(:default_page_size).and_return(2)
       end
       it '件数0は全件(5件)を返す' do
         r = SystemPicture.list 5, 0
@@ -415,6 +415,23 @@ describe SystemPicture do
         r = @sp.store @imager
       end
     end
+    context '画像データの保存に失敗したとき' do
+      before do
+        PictureIO.system_picture_io.stub(:put).with(any_args).and_raise(PictureIO::Error)
+      end
+      it 'falseを返す' do
+        r = @sp.store @imager
+        r.should be_false
+      end
+      it '更新されていない' do
+        r = @sp.store @imager
+        @sp.should be_a_new SystemPicture
+      end
+      it '全体エラーメッセージがセットされている' do
+        r = @sp.store @imager
+        @sp.errors[:base].should_not be_blank
+      end
+    end
   end
   
   describe '置換に於いて' do