OSDN Git Service

pass test:resource picture create
authoryasushiito <yas@pen-chan.jp>
Thu, 12 Jul 2012 09:26:48 +0000 (18:26 +0900)
committeryasushiito <yas@pen-chan.jp>
Thu, 12 Jul 2012 09:26:48 +0000 (18:26 +0900)
app/controllers/resource_pictures_controller.rb
app/models/picture.rb
app/models/resource_picture.rb
app/views/resource_pictures/_form.html.erb
app/views/standard_licenses/_confirm.html.erb [new file with mode: 0644]
app/views/standard_licenses/_credit.html.erb [new file with mode: 0644]
spec/controllers/resource_pictures_controller_spec.rb
spec/models/resource_picture_spec.rb

index ce413b2..e7af0ea 100644 (file)
@@ -35,6 +35,7 @@ class ResourcePicturesController < ApplicationController
     @original_picture_license_group = OriginalPictureLicenseGroup.new params[:original_picture_license_group]
     @license_group = LicenseGroup.show @original_picture_license_group.license_group_id
     @resource_picture = ResourcePicture.new params[:resource_picture]
+    @license = @resource_picture.license
     
     respond_to do |format|
       format.html # new.html.erb
@@ -47,6 +48,7 @@ class ResourcePicturesController < ApplicationController
     @original_picture_license_group = OriginalPictureLicenseGroup.new params[:original_picture_license_group]
     @license_group = LicenseGroup.show @original_picture_license_group.license_group_id
     @resource_picture = ResourcePicture.new params[:resource_picture]
+    @license = @resource_picture.license
 
     respond_to do |format|
       if @resource_picture.store
index 09a5940..214b1c8 100644 (file)
@@ -64,7 +64,7 @@ class Picture < ActiveRecord::Base
   
   def copy_data(rp)
     attr = {:width => rp.width, :height => rp.height, :ext => rp.ext, :filesize => rp.filesize, 
-      :original_picture_id => rp.original_picture_id, :artist_id => rp.artist_id, 
+      :original_picture_id => rp.original_picture_id, :license_id => rp.license_id, :artist_id => rp.artist_id, 
       :artist_name => rp.artist_name, :credit => rp.credit, :settings => rp.settings
     }
     self.attributes = attr
index 9c42ca1..2735db6 100644 (file)
@@ -79,10 +79,20 @@ class ResourcePicture < ActiveRecord::Base
     self.attributes = attr
   end
   
+  def data_to_mgk picture_data
+    begin
+      mgk = Magick::Image.from_blob(picture_data).shift
+    rescue 
+      self.errors.add :base, 'magick failed'
+      return false
+    end
+    mgk
+  end
+  
   def op_mgk
     d = self.original_picture.restore
     return false unless d
-    self.original_picture.data_to_mgk d
+    self.data_to_mgk d
   end
   
   def new_picture mgk
@@ -114,19 +124,33 @@ class ResourcePicture < ActiveRecord::Base
     self.copy_data self.original_picture
     mgk = self.op_mgk
     return false unless mgk
-    pc = self.new_picture mgk
-    self.picture_id = pc.id
-    return false unless pc.valid?
-    if res = self.save
-      if res = self.store_picture(mgk)
-        if self.to_gif?
-          if gifmgk = ResourcePicture.png_to_gif(mgk.to_blob)
-            res = self.store_picture(gifmgk)
-          else
-            res = false
-          end
+    OriginalPicture.transaction do
+      pc = self.new_picture mgk
+      if res = pc.valid?
+        self.picture_id = pc.id
+        if res = self.save
+          res = self.store_picture_with_gif(mgk)
+        end
+      else
+        self.errors.add :base, 'picture does not create'
+      end
+      raise ActiveRecord::Rollback unless res
+    end
+    res
+  end
+  
+  def store_picture_with_gif(mgk)
+    if res = self.store_picture(mgk)
+      if self.to_gif?
+        if gifmgk = ResourcePicture.png_to_gif(mgk.to_blob)
+          res = self.store_picture(gifmgk)
+        else
+          self.errors.add :base, 'picture data can not conv to gif'
+          res = false
         end
       end
+    else
+      self.errors.add :base, 'resource picture io does not work'
     end
     res
   end
index d3ca608..ccbe94e 100644 (file)
   <%= f.hidden_field :credit %>
   <%= f.hidden_field :settings %>
 
+  <div>
+    <%= link_to h(@license_group.caption), license_group_path(@license_group) %>
+    <%= link_to h(@license.caption), license_path(@license) %>
+  </div>
+  <div>
+    artist name:<%= @resource_picture.artist_name %>
+  </div>
+
+  <%= render @license_group.classname.tableize + '/confirm' %>
+
   <div class="actions">
     <%= f.submit %>
   </div>
diff --git a/app/views/standard_licenses/_confirm.html.erb b/app/views/standard_licenses/_confirm.html.erb
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/app/views/standard_licenses/_credit.html.erb b/app/views/standard_licenses/_credit.html.erb
new file mode 100644 (file)
index 0000000..ce7986f
--- /dev/null
@@ -0,0 +1,7 @@
+<div>
+<%= content_tag(:a, 
+  tag(:img, :src => @picture.license.system_picture.url, :alt => h(@picture.license_group.caption.to_s + '[' + @picture.license.caption.to_s + ']')
+  , :href => @picture.license.url )
+%>
+</div>
+著作者:<%= link_to h(@picture.artist_name), :controller => 'artists', :action => :show, :id => @picture.artist.id %>
index 26927d6..f5f7547 100644 (file)
@@ -16,7 +16,8 @@ describe ResourcePicturesController do
 
   describe '一覧表示に於いて' do
     before do
-      @rp = Factory :resource_picture, :artist_id => @artist.id , :license_id => @license.id, :original_picture_id => @op.id
+      @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+      @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
       sign_in @user
       ResourcePicture.stub(:list).and_return([@rp, @rp, @rp])
     end
@@ -111,7 +112,8 @@ describe ResourcePicturesController do
   
   describe '単体表示に於いて' do
     before do
-      @pic = Factory :resource_picture, :artist_id => @artist.id , :license_id => @license.id, :original_picture_id => @op.id
+      @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+      @pic = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
       sign_in @user
       ResourcePicture.stub(:show).and_return(@pic)
     end
index 3b58e56..820a630 100644 (file)
@@ -25,6 +25,10 @@ describe ResourcePicture do
     Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
     #原画ファイル削除だけは必ず成功するものとしておく
     PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
+    Picture.any_instance.stub(:subdirs).with(any_args).and_return(['', 'v', 'h', 'vh'])\r
+    Picture.any_instance.stub(:h).with(any_args).and_return('data')
+    Picture.any_instance.stub(:v).with(any_args).and_return('data')
+    Picture.any_instance.stub(:vh).with(any_args).and_return('data')
   end\r
   
   describe '検証に於いて' do
@@ -425,6 +429,32 @@ describe ResourcePicture do
     end\r
   end
   
+  describe 'RMagick変換に於いて' do
+    before do
+      @op = Factory :original_picture, :artist_id => @artist.id
+      @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+      attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}\r
+      @rp = Factory.build :resource_picture, attr
+    end
+    context 'つつがなく終わるとき' do
+      it '画像データをオブジェクト化している' do
+        Magick::Image.stub(:from_blob).and_return(['mgkobj'])
+        @rp.data_to_mgk('mgkbin').should eq 'mgkobj'
+      end
+    end
+    context '失敗するとき' do
+      it 'Falseを返す' do
+        Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
+        @rp.data_to_mgk('mgkbin').should be_false
+      end
+      it '全体エラーがセットされている' do
+        Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
+        lambda {
+          @rp.data_to_mgk('mgkbin')
+        }.should change(@rp.errors[:base], :count)
+      end
+    end
+  end
   describe '画像オブジェクトの取得に於いて' do
     before do
       @op = Factory :original_picture, :artist_id => @artist.id
@@ -435,24 +465,24 @@ describe ResourcePicture do
     context '事前チェック' do
       before do
         OriginalPicture.any_instance.stub(:restore).with(any_args).and_return('data')\r
-        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+        ResourcePicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
       end
       it '原画から画像データを取得している' do
         OriginalPicture.any_instance.should_receive(:restore).with(any_args).exactly(1)
         res = @rp.op_mgk\r
       end\r
-      it '原画に原画データのRMagick変換を依頼している' do
-        OriginalPicture.any_instance.should_receive(:data_to_mgk).with(any_args).exactly(1)
+      it '素材に原画データのRMagick変換を依頼している' do
+        ResourcePicture.any_instance.should_receive(:data_to_mgk).with(any_args).exactly(1)
         res = @rp.op_mgk\r
       end\r
     end\r
     context 'つつがなく終わるとき' do
       #原画から画像データを取得する。\r
-      #原画に原画データのRMagick変換を依頼し、画像オブジェクトを返す。 \r
+      #素材に原画データのRMagick変換を依頼し、画像オブジェクトを返す。 \r
       before do
         @mgk = Mgk.new\r
         OriginalPicture.any_instance.stub(:restore).with(any_args).and_return('data')\r
-        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(@mgk)
+        ResourcePicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(@mgk)
       end
       it '画像オブジェクトを返す' do
         res = @rp.op_mgk\r
@@ -465,7 +495,8 @@ describe ResourcePicture do
     before do
       @op = Factory :original_picture, :artist_id => @artist.id
       @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
-      attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}\r
+      attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', \r
+        :credit => '{}', :settings => {:reverse => 1, :gif_convert => 1}.to_json.to_s}\r
       @rp = Factory.build :resource_picture, attr
       @mgk = Mgk.new
     end
@@ -514,11 +545,7 @@ describe ResourcePicture do
         @mgk = Mgk.new   #一回目の本画像保存は与えたオブジェクトを使って保存する。
         ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk)\r
         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
-        ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
-        ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
-        class GifMgk < Mgk ; end  #store_pictureは二回呼び出される。区別をつけるために\r
-        @gifmgk = GifMgk.new      #パラメータを二種類用意する。\r
-        ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
+        ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(true)\r
       end
       it '素材に原画からのコピーデータをセットしている' do
         ResourcePicture.any_instance.stub(:copy_data).with(any_args).and_return(true)\r
@@ -538,20 +565,7 @@ describe ResourcePicture do
         res = @rp.store\r
       end\r
       it '画像ファイルの作成・更新機能で画像を保存している' do\r
-        ResourcePicture.any_instance.should_receive(:store_picture).with(@mgk).exactly(1)
-        res = @rp.store\r
-      end\r
-      it '自身にフォーマット変換対象かを問い合わせている' do
-        ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1)
-        res = @rp.store\r
-      end\r
-      it '自身にGifフォーマット変換を依頼している' do
-        ResourcePicture.should_receive(:png_to_gif).with(any_args).exactly(1)
-        res = @rp.store\r
-      end\r
-      it '画像ファイルの作成・更新機能でgif画像を保存している' do\r
-        #二回目の保存はgif変換の結果を渡す。
-        ResourcePicture.any_instance.should_receive(:store_picture).with(@gifmgk).exactly(1)
+        ResourcePicture.any_instance.should_receive(:store_picture_with_gif).with(@mgk).exactly(1)
         res = @rp.store\r
       end\r
     end
@@ -561,11 +575,7 @@ describe ResourcePicture do
         @mgk = Mgk.new   #一回目の本画像保存は与えたオブジェクトを使って保存する。
         ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk)\r
         ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
-        ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
-        ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
-        class GifMgk < Mgk ; end  #store_pictureは二回呼び出される。区別をつけるために\r
-        @gifmgk = GifMgk.new      #パラメータを二種類用意する。\r
-        ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
+        ResourcePicture.any_instance.stub(:store_picture_with_gif).with(any_args).and_return(true)\r
       end
       it 'Trueを返す' do
         res = @rp.store\r
@@ -585,24 +595,6 @@ describe ResourcePicture do
         }.should change ResourcePicture, :count\r
       end\r
     end
-    context 'gif変換なしで、つつがなく終わるとき' do
-      before do
-        #すべての処理を正常パターンで通過させ、保存機能をチェックする。
-        @mgk = Mgk.new   #一回目の本画像保存は与えたオブジェクトを使って保存する。
-        ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk)\r
-        ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
-        ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
-        ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(false)\r
-      end
-      it 'Trueを返す' do
-        res = @rp.store\r
-        res.should be_true\r
-      end\r
-      it 'gif保存は呼ばれていない' do\r
-        ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)
-        res = @rp.store\r
-      end\r
-    end
     #以下から例外ケース。処理先頭から失敗させていく
     context '画像オブジェクトの取得に失敗したとき' do
       before do
@@ -648,50 +640,116 @@ describe ResourcePicture do
         @rp.store
         @rp.should be_a_new ResourcePicture\r
       end
+      it '処理を中断してロールバックする' do
+        lambda {
+          res = @rp.store\r
+        }.should_not change Picture, :count\r
+      end
     end
+  end\r
+  
+  describe '画像ファイルの作成・更新に於いて' do\r
+    before do
+      @op = Factory :original_picture, :artist_id => @artist.id
+      @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+      attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', :credit => '{}', :settings => {}.to_json.to_s}\r
+      @rp = Factory.build :resource_picture, attr
+    end
+    context '事前チェック' do
+      before do
+        #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
+        #それで外部のメソッド呼び出しだけに着目してテストする。
+        @mgk = Mgk.new   #一回目の本画像保存は与えたオブジェクトを使って保存する。
+        ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
+        ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
+        class GifMgk < Mgk ; end  #store_pictureは二回呼び出される。区別をつけるために\r
+        @gifmgk = GifMgk.new      #パラメータを二種類用意する。\r
+        ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
+      end
+      it '画像ファイルの作成・更新機能で画像を保存している' do\r
+        ResourcePicture.any_instance.should_receive(:store_picture).with(@mgk).exactly(1)
+        res = @rp.store_picture_with_gif @mgk\r
+      end\r
+      it '自身にフォーマット変換対象かを問い合わせている' do
+        ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1)
+        res = @rp.store_picture_with_gif @mgk\r
+      end\r
+      it '自身にGifフォーマット変換を依頼している' do
+        ResourcePicture.should_receive(:png_to_gif).with(any_args).exactly(1)
+        res = @rp.store_picture_with_gif @mgk\r
+      end\r
+      it '画像ファイルの作成・更新機能でgif画像を保存している' do\r
+        #二回目の保存はgif変換の結果を渡す。
+        ResourcePicture.any_instance.should_receive(:store_picture).with(@gifmgk).exactly(1)
+        res = @rp.store_picture_with_gif @mgk\r
+      end\r
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        #すべての処理を正常パターンで通過させ、保存機能をチェックする。
+        @mgk = Mgk.new   #一回目の本画像保存は与えたオブジェクトを使って保存する。
+        ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
+        ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
+        class GifMgk < Mgk ; end  #store_pictureは二回呼び出される。区別をつけるために\r
+        @gifmgk = GifMgk.new      #パラメータを二種類用意する。\r
+        ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
+      end
+      it 'Trueを返す' do
+        res = @rp.store_picture_with_gif @mgk\r
+        res.should be_true\r
+      end\r
+    end
+    context 'gif変換なしで、つつがなく終わるとき' do
+      before do
+        #すべての処理を正常パターンで通過させ、保存機能をチェックする。
+        @mgk = Mgk.new   #一回目の本画像保存は与えたオブジェクトを使って保存する。
+        ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
+        ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(false)\r
+      end
+      it 'Trueを返す' do
+        res = @rp.store_picture_with_gif @mgk\r
+        res.should be_true\r
+      end\r
+      it 'gif保存は呼ばれていない' do\r
+        ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)
+        res = @rp.store_picture_with_gif @mgk\r
+      end\r
+    end
+    #以下から例外ケース。処理先頭から失敗させていく
     context '画像の保存に失敗したとき' do
       before do
         @mgk = Mgk.new   #一回目の本画像保存は与えたオブジェクトを使って保存する。
-        ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk)\r
-        ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
-        ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)\r
         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(false)\r
       end
       it 'Falseを返す' do
-        res = @rp.store\r
+        res = @rp.store_picture_with_gif @mgk\r
         res.should be_false\r
       end\r
       it 'gif変換判定は呼ばれていない' do\r
         ResourcePicture.any_instance.should_not_receive(:to_gif?).with(any_args)
-        res = @rp.store\r
+        res = @rp.store_picture_with_gif @mgk\r
       end\r
     end
     context 'gif変換に失敗したとき' do
       before do
         @mgk = Mgk.new   #一回目の本画像保存は与えたオブジェクトを使って保存する。
-        ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk)\r
-        ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
-        ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)\r
         ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true)\r
         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
         ResourcePicture.stub(:png_to_gif).with(any_args).and_return(false)
       end
       it 'Falseを返す' do
-        res = @rp.store\r
+        res = @rp.store_picture_with_gif @mgk\r
         res.should be_false\r
       end\r
       it 'gif画像の保存は呼ばれていない' do\r
         #本画像の保存があるので、一度は呼ばれる\r
         ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1)
-        res = @rp.store\r
+        res = @rp.store_picture_with_gif @mgk\r
       end\r
     end
     context 'gif画像の保存に失敗したとき' do
       before do\r
         @mgk = Mgk.new
-        ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk)\r
-        ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p)\r
-        ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)\r
         ResourcePicture.any_instance.stub(:store_picture).with(@mgk).and_return(true)\r
         ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true)\r
         class GifMgk < Mgk ; end  #store_pictureは二回呼び出される。区別をつけるために\r
@@ -700,7 +758,7 @@ describe ResourcePicture do
         ResourcePicture.any_instance.stub(:store_picture).with(@gifmgk).and_return(false)\r
       end
       it 'Falseを返す' do
-        res = @rp.store\r
+        res = @rp.store_picture_with_gif @mgk\r
         res.should be_false\r
       end\r
     end