From 63b71e8877287f1392ce62086f3c4edea5e7ced2 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Thu, 12 Jul 2012 18:26:48 +0900 Subject: [PATCH] pass test:resource picture create --- app/controllers/resource_pictures_controller.rb | 2 + app/models/picture.rb | 2 +- app/models/resource_picture.rb | 48 ++++-- app/views/resource_pictures/_form.html.erb | 10 ++ app/views/standard_licenses/_confirm.html.erb | 0 app/views/standard_licenses/_credit.html.erb | 7 + .../resource_pictures_controller_spec.rb | 6 +- spec/models/resource_picture_spec.rb | 182 ++++++++++++++------- 8 files changed, 180 insertions(+), 77 deletions(-) create mode 100644 app/views/standard_licenses/_confirm.html.erb create mode 100644 app/views/standard_licenses/_credit.html.erb diff --git a/app/controllers/resource_pictures_controller.rb b/app/controllers/resource_pictures_controller.rb index ce413b23..e7af0eae 100644 --- a/app/controllers/resource_pictures_controller.rb +++ b/app/controllers/resource_pictures_controller.rb @@ -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 diff --git a/app/models/picture.rb b/app/models/picture.rb index 09a59403..214b1c8b 100644 --- a/app/models/picture.rb +++ b/app/models/picture.rb @@ -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 diff --git a/app/models/resource_picture.rb b/app/models/resource_picture.rb index 9c42ca1d..2735db6c 100644 --- a/app/models/resource_picture.rb +++ b/app/models/resource_picture.rb @@ -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 diff --git a/app/views/resource_pictures/_form.html.erb b/app/views/resource_pictures/_form.html.erb index d3ca608e..ccbe94e6 100644 --- a/app/views/resource_pictures/_form.html.erb +++ b/app/views/resource_pictures/_form.html.erb @@ -11,6 +11,16 @@ <%= f.hidden_field :credit %> <%= f.hidden_field :settings %> +
+ <%= link_to h(@license_group.caption), license_group_path(@license_group) %> + <%= link_to h(@license.caption), license_path(@license) %> +
+
+ artist name:<%= @resource_picture.artist_name %> +
+ + <%= render @license_group.classname.tableize + '/confirm' %> +
<%= f.submit %>
diff --git a/app/views/standard_licenses/_confirm.html.erb b/app/views/standard_licenses/_confirm.html.erb new file mode 100644 index 00000000..e69de29b diff --git a/app/views/standard_licenses/_credit.html.erb b/app/views/standard_licenses/_credit.html.erb new file mode 100644 index 00000000..ce7986fc --- /dev/null +++ b/app/views/standard_licenses/_credit.html.erb @@ -0,0 +1,7 @@ +
+<%= 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 ) +%> +
+著作者:<%= link_to h(@picture.artist_name), :controller => 'artists', :action => :show, :id => @picture.artist.id %> diff --git a/spec/controllers/resource_pictures_controller_spec.rb b/spec/controllers/resource_pictures_controller_spec.rb index 26927d65..f5f7547f 100644 --- a/spec/controllers/resource_pictures_controller_spec.rb +++ b/spec/controllers/resource_pictures_controller_spec.rb @@ -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 diff --git a/spec/models/resource_picture_spec.rb b/spec/models/resource_picture_spec.rb index 3b58e565..820a630d 100644 --- a/spec/models/resource_picture_spec.rb +++ b/spec/models/resource_picture_spec.rb @@ -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']) + 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 describe '検証に於いて' do @@ -425,6 +429,32 @@ describe ResourcePicture do end 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} + @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') - 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 end - 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 end end context 'つつがなく終わるとき' do #原画から画像データを取得する。 - #原画に原画データのRMagick変換を依頼し、画像オブジェクトを返す。 + #素材に原画データのRMagick変換を依頼し、画像オブジェクトを返す。 before do @mgk = Mgk.new OriginalPicture.any_instance.stub(:restore).with(any_args).and_return('data') - 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 @@ -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} + attr = {:original_picture_id => @op.id, :license_id => @license.id, :artist_name => 'tester', + :credit => '{}', :settings => {:reverse => 1, :gif_convert => 1}.to_json.to_s} @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) ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p) - ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true) - ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true) - class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために - @gifmgk = GifMgk.new #パラメータを二種類用意する。 - 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) end it '素材に原画からのコピーデータをセットしている' do ResourcePicture.any_instance.stub(:copy_data).with(any_args).and_return(true) @@ -538,20 +565,7 @@ describe ResourcePicture do res = @rp.store end it '画像ファイルの作成・更新機能で画像を保存している' do - ResourcePicture.any_instance.should_receive(:store_picture).with(@mgk).exactly(1) - res = @rp.store - end - it '自身にフォーマット変換対象かを問い合わせている' do - ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1) - res = @rp.store - end - it '自身にGifフォーマット変換を依頼している' do - ResourcePicture.should_receive(:png_to_gif).with(any_args).exactly(1) - res = @rp.store - end - it '画像ファイルの作成・更新機能でgif画像を保存している' do - #二回目の保存は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 end end @@ -561,11 +575,7 @@ describe ResourcePicture do @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。 ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk) ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p) - ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true) - ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true) - class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために - @gifmgk = GifMgk.new #パラメータを二種類用意する。 - 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) end it 'Trueを返す' do res = @rp.store @@ -585,24 +595,6 @@ describe ResourcePicture do }.should change ResourcePicture, :count end end - context 'gif変換なしで、つつがなく終わるとき' do - before do - #すべての処理を正常パターンで通過させ、保存機能をチェックする。 - @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。 - ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk) - ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p) - ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true) - ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(false) - end - it 'Trueを返す' do - res = @rp.store - res.should be_true - end - it 'gif保存は呼ばれていない' do - ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1) - res = @rp.store - end - end #以下から例外ケース。処理先頭から失敗させていく context '画像オブジェクトの取得に失敗したとき' do before do @@ -648,50 +640,116 @@ describe ResourcePicture do @rp.store @rp.should be_a_new ResourcePicture end + it '処理を中断してロールバックする' do + lambda { + res = @rp.store + }.should_not change Picture, :count + end end + end + + describe '画像ファイルの作成・更新に於いて' 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} + @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) + ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true) + class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために + @gifmgk = GifMgk.new #パラメータを二種類用意する。 + ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk) + end + it '画像ファイルの作成・更新機能で画像を保存している' do + ResourcePicture.any_instance.should_receive(:store_picture).with(@mgk).exactly(1) + res = @rp.store_picture_with_gif @mgk + end + it '自身にフォーマット変換対象かを問い合わせている' do + ResourcePicture.any_instance.should_receive(:to_gif?).with(any_args).exactly(1) + res = @rp.store_picture_with_gif @mgk + end + it '自身にGifフォーマット変換を依頼している' do + ResourcePicture.should_receive(:png_to_gif).with(any_args).exactly(1) + res = @rp.store_picture_with_gif @mgk + end + it '画像ファイルの作成・更新機能でgif画像を保存している' do + #二回目の保存はgif変換の結果を渡す。 + ResourcePicture.any_instance.should_receive(:store_picture).with(@gifmgk).exactly(1) + res = @rp.store_picture_with_gif @mgk + end + end + context 'つつがなく終わるとき' do + before do + #すべての処理を正常パターンで通過させ、保存機能をチェックする。 + @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。 + ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true) + ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true) + class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために + @gifmgk = GifMgk.new #パラメータを二種類用意する。 + ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk) + end + it 'Trueを返す' do + res = @rp.store_picture_with_gif @mgk + res.should be_true + end + end + context 'gif変換なしで、つつがなく終わるとき' do + before do + #すべての処理を正常パターンで通過させ、保存機能をチェックする。 + @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。 + ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true) + ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(false) + end + it 'Trueを返す' do + res = @rp.store_picture_with_gif @mgk + res.should be_true + end + it 'gif保存は呼ばれていない' do + ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1) + res = @rp.store_picture_with_gif @mgk + end + end + #以下から例外ケース。処理先頭から失敗させていく context '画像の保存に失敗したとき' do before do @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。 - ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk) - ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p) - ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true) ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(false) end it 'Falseを返す' do - res = @rp.store + res = @rp.store_picture_with_gif @mgk res.should be_false end it 'gif変換判定は呼ばれていない' do ResourcePicture.any_instance.should_not_receive(:to_gif?).with(any_args) - res = @rp.store + res = @rp.store_picture_with_gif @mgk end end context 'gif変換に失敗したとき' do before do @mgk = Mgk.new #一回目の本画像保存は与えたオブジェクトを使って保存する。 - ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk) - ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p) - ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true) ResourcePicture.any_instance.stub(:store_picture).with(any_args).and_return(true) ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true) ResourcePicture.stub(:png_to_gif).with(any_args).and_return(false) end it 'Falseを返す' do - res = @rp.store + res = @rp.store_picture_with_gif @mgk res.should be_false end it 'gif画像の保存は呼ばれていない' do #本画像の保存があるので、一度は呼ばれる ResourcePicture.any_instance.should_receive(:store_picture).with(any_args).exactly(1) - res = @rp.store + res = @rp.store_picture_with_gif @mgk end end context 'gif画像の保存に失敗したとき' do before do @mgk = Mgk.new - ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(@mgk) - ResourcePicture.any_instance.stub(:new_picture).with(any_args).and_return(@p) - ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true) ResourcePicture.any_instance.stub(:store_picture).with(@mgk).and_return(true) ResourcePicture.any_instance.stub(:to_gif?).with(any_args).and_return(true) class GifMgk < Mgk ; end #store_pictureは二回呼び出される。区別をつけるために @@ -700,7 +758,7 @@ describe ResourcePicture do ResourcePicture.any_instance.stub(:store_picture).with(@gifmgk).and_return(false) end it 'Falseを返す' do - res = @rp.store + res = @rp.store_picture_with_gif @mgk res.should be_false end end -- 2.11.0