OSDN Git Service

pass test: ResourcePicture.store
authoryasushiito <yas@pen-chan.jp>
Tue, 10 Jul 2012 23:18:45 +0000 (08:18 +0900)
committeryasushiito <yas@pen-chan.jp>
Tue, 10 Jul 2012 23:18:45 +0000 (08:18 +0900)
app/models/picture.rb
app/models/resource_picture.rb
db/migrate/20120703091854_rp_join_rp_license.rb
spec/models/picture_spec.rb
spec/models/resource_picture_spec.rb

index 33d4bf4..09a5940 100644 (file)
@@ -2,6 +2,7 @@ class Picture < ActiveRecord::Base
   belongs_to :original_picture
   belongs_to :license
   belongs_to :artist
+  has_one :resource_picture
   
   validates :original_picture_id, :presence => true, :numericality => true, :existence => true
   validates :revision, :presence => true, :numericality => true
@@ -61,6 +62,14 @@ class Picture < ActiveRecord::Base
     flag_reverse < 0 ? [''] : ['', 'v', 'h', 'vh']
   end
   
+  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, 
+      :artist_name => rp.artist_name, :credit => rp.credit, :settings => rp.settings
+    }
+    self.attributes = attr
+  end
+  
   def store(mgk)
     res = false
     self.revision = self.new_revision
index 5f5c9e6..9c42ca1 100644 (file)
@@ -1,6 +1,7 @@
 class ResourcePicture < ActiveRecord::Base
   belongs_to :artist
   belongs_to :license
+  belongs_to :picture
   has_many :panel_pictures
   belongs_to :original_picture
   
@@ -11,6 +12,8 @@ class ResourcePicture < ActiveRecord::Base
   validates :artist_id, :presence => true, :numericality => true, :existence => true
   validates :license_id, :presence => true, :numericality => true, :existence => true
   validates :original_picture_id, :presence => true, :numericality => true, :existence => true
+  validates :artist_name, :presence => true
+  validates :picture_id, :presence => true, :numericality => true, :existence => true
   
   before_destroy :destroy_with_file
   
@@ -69,16 +72,29 @@ class ResourcePicture < ActiveRecord::Base
     ResourcePicture.resize(rimg.to_blob, tw, th).to_blob
   end
   
-  def self.update_picture(op)
-    res = op.resource_picture || ResourcePicture.new
-    res.attributes = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize, 
-      :original_picture_id => op.id, :artist_id => op.artist_id, :license_id => op.license_id
+  def copy_data(op)
+    attr = {:width => op.width, :height => op.height, :ext => op.ext, :filesize => op.filesize, 
+      :original_picture_id => op.id, :artist_id => op.artist_id
     }
-    res
+    self.attributes = attr
+  end
+  
+  def op_mgk
+    d = self.original_picture.restore
+    return false unless d
+    self.original_picture.data_to_mgk d
+  end
+  
+  def new_picture mgk
+    pc = Picture.new
+    pc.copy_data self
+    pc.store mgk
+    pc
   end
   
+  
   def to_gif?
-    self.dext == 'png' and self.license.no_convert == 0
+    self.dext == 'png' and self.flag_gif_convert >= 0
   end
   
   def self.png_to_gif(data)
@@ -93,8 +109,14 @@ class ResourcePicture < ActiveRecord::Base
     res
   end
   
-  def store(mgk)
+  def store
     res = false
+    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?
@@ -111,12 +133,10 @@ class ResourcePicture < ActiveRecord::Base
   
   def store_picture(mgk)
     res = false
-    PictureIO.resource_picture_io.class.subdirs.each do |d|
-      picdata = d.empty? ? mgk.to_blob : self.__send__(d, mgk)
-      res = PictureIO.resource_picture_io.put(picdata, "#{self.id}.#{mgk.format}", d)
-      break unless res
-    end
-    res
+    tdata = self.flag_thumbnail >= 0 ? thumbnail(mgk) : mgk.to_blob
+    fdata = mgk.to_blob
+    return false unless PictureIO.resource_picture_io.put(tdata, "#{self.id}.#{mgk.format}")
+    PictureIO.resource_picture_io.put(fdata, "#{self.id}.#{mgk.format}", 'full')
   end
   
   def restore(subdir = nil)
@@ -186,4 +206,71 @@ class ResourcePicture < ActiveRecord::Base
     ResourcePicture.count
   end
   
+  def flags
+    begin
+      @flags = JSON.parse(self.settings) unless @flags
+    rescue 
+    end
+    @flags
+  end
+  
+  def flags=(s)
+    @flags = s
+  end
+  
+  def flag_open
+    @flag_open = flags["open"] unless @flag_open
+    @flag_open
+  end
+  
+  def flag_commercial
+    @flag_commercial = flags["commercial"] unless @flag_commercial
+    @flag_commercial
+  end
+  
+  def flag_official
+    @flag_official = flags["official"] unless @flag_official
+    @flag_official
+  end
+  
+  def flag_attribution
+    @flag_attribution = flags["attribution"] unless @flag_attribution
+    @flag_attribution
+  end
+  
+  def flag_derive
+    @flag_derive = flags["derive"] unless @flag_derive
+    @flag_derive
+  end
+  
+  def flag_thumbnail
+    @flag_thumbnail = flags["thumbnail"] unless @flag_thumbnail
+    @flag_thumbnail
+  end
+  
+  def flag_gif_convert
+    @flag_gif_convert = flags["gif_convert"] unless @flag_gif_convert
+    @flag_gif_convert
+  end
+  
+  def flag_reverse
+    @flag_reverse = flags["reverse"] unless @flag_reverse
+    @flag_reverse
+  end
+  
+  def flag_resize
+    @flag_resize = flags["resize"] unless @flag_resize
+    @flag_resize
+  end
+  
+  def flag_sync_vh
+    @flag_sync_vh = flags["sync_vh"] unless @flag_sync_vh
+    @flag_sync_vh
+  end
+  
+  def flag_overlap
+    @flag_overlap = flags["overlap"] unless @flag_overlap
+    @flag_overlap
+  end
+  
 end
index 488eff4..ca38b68 100644 (file)
@@ -3,11 +3,13 @@ class RpJoinRpLicense < ActiveRecord::Migration
     add_column :resource_pictures, :artist_name, :string, :null => false, :default => 'unknown'
     add_column :resource_pictures, :credit, :string
     add_column :resource_pictures, :settings, :string
+    add_column :resource_pictures, :picture_id, :integer, :null => false, :default => 0
   end
 
   def down
-    remove_column :resource_pictures, :artist_name, :string, :null => false, :default => 'unknown'
-    remove_column :resource_pictures, :credit, :string
-    remove_column :resource_pictures, :settings, :string
+    remove_column :resource_pictures, :artist_name
+    remove_column :resource_pictures, :credit
+    remove_column :resource_pictures, :settings
+    remove_column :resource_pictures, :picture_id 
   end
 end
index 465838b..bb5bf3d 100644 (file)
@@ -265,6 +265,30 @@ describe Picture do
       end
     end
   end
+  describe '素材からのコピーデータセットに於いて' do
+    before do\r
+      @op = Factory :original_picture, :artist_id => @artist.id
+      attr = {:ext => 'jpeg', :width => 264, :height => 265, :filesize => 266, 
+        :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, 
+        :artist_name => 'tester', :credit => {:title => 'cap'}.to_json.to_s, :settings => {:set => 1}.to_json.to_s}\r
+      @rp = Factory.build :resource_picture, attr
+      @p = Factory.build :picture
+    end
+    it '素材オブジェクトから属性を取り出して対象実素材にセットしている' do
+      res = @p.copy_data(@rp)\r
+      @p.ext.should eq 'jpeg'\r
+      @p.width.should eq 264\r
+      @p.height.should eq 265\r
+      @p.filesize.should eq 266\r
+      @p.artist_id.should eq @artist.id\r
+      @p.license_id.should eq @license.id\r
+      @p.original_picture_id.should eq @op.id\r
+      @p.artist_name.should eq 'tester'\r
+      @p.credit.should match /title/\r
+      @p.settings.should match /set/\r
+    end\r
+  end
+  
   describe '作成に於いて' do
     before do
       #RMagickのスタブをおいておく
index d1278a2..3b58e56 100644 (file)
@@ -29,18 +29,15 @@ describe ResourcePicture do
   
   describe '検証に於いて' do
     before do\r
+      @p = Factory :picture, :original_picture_id => @original_picture.id, :license_id => @license.id, :artist_id => @artist.id
+      @rp = Factory.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id, :picture_id => @p.id
     end
     
     it 'オーソドックスなデータなら通る' do
-      @rp = Factory.build :resource_picture, :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
       @rp.should be_valid
     end
     
     context 'extを検証するとき' do
-      before do
-        @rp = Factory.build :resource_picture, \r
-          :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
-      end
       it 'テストデータの確認' do
         @rp.ext = 'jpeg'
         @rp.should be_valid
@@ -59,10 +56,6 @@ describe ResourcePicture do
       end
     end
     context 'widthを検証するとき' do
-      before do
-        @rp = Factory.build :resource_picture, \r
-          :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
-      end
       it 'テストデータの確認' do
         @rp.width = 1
         @rp.should be_valid
@@ -85,10 +78,6 @@ describe ResourcePicture do
       end
     end
     context 'heightを検証するとき' do
-      before do
-        @rp = Factory.build :resource_picture, \r
-          :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
-      end
       it 'テストデータの確認' do
         @rp.height = '1'
         @rp.should be_valid
@@ -111,10 +100,6 @@ describe ResourcePicture do
       end
     end
     context 'filesizeを検証するとき' do
-      before do
-        @rp = Factory.build :resource_picture, \r
-          :artist_id => @artist.id, :original_picture_id => @original_picture.id, :license_id => @license.id
-      end
       it 'テストデータの確認' do
         @rp.filesize = '1'
         @rp.should be_valid
@@ -137,10 +122,6 @@ describe ResourcePicture do
       end
     end
     context 'artist_idを検証するとき' do
-      before do
-        @rp = Factory.build :resource_picture, \r
-          :original_picture_id => @original_picture.id, :license_id => @license.id
-      end
       it 'テストデータの確認' do
         @rp.artist_id = @artist.id
         @rp.should be_valid
@@ -159,10 +140,6 @@ describe ResourcePicture do
       end
     end
     context 'license_idを検証するとき' do
-      before do
-        @rp = Factory.build :resource_picture, \r
-          :artist_id => @artist.id, :original_picture_id => @original_picture.id
-      end
       it 'テストデータの確認' do
         @rp.license_id = @license.id
         @rp.should be_valid
@@ -181,10 +158,6 @@ describe ResourcePicture do
       end
     end
     context 'original_picture_idを検証するとき' do
-      before do
-        @rp = Factory.build :resource_picture, \r
-          :artist_id => @artist.id, :license_id => @license.id
-      end
       it 'テストデータの確認' do
         @rp.original_picture_id = @original_picture.id
         @rp.should be_valid
@@ -202,6 +175,46 @@ describe ResourcePicture do
         @rp.should_not be_valid
       end
     end
+    context 'artist_nameを検証するとき' do
+      it 'テストデータの確認' do
+        @rp.artist_name = 'a'
+        @rp.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @rp.artist_name = nil
+        @rp.should_not be_valid
+      end
+    end
+    context 'creditを検証するとき' do
+      it 'テストデータの確認' do
+        @rp.credit = 'a'
+        @rp.should be_valid
+      end
+    end
+    context 'settingsを検証するとき' do
+      it 'テストデータの確認' do
+        @rp.settings = 'a'
+        @rp.should be_valid
+      end
+    end
+    context 'picture_idを検証するとき' do
+      it 'テストデータの確認' do
+        @rp.picture_id = @p.id
+        @rp.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @rp.picture_id = nil
+        @rp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @rp.picture_id = 'a'
+        @rp.should_not be_valid
+      end
+      it '存在する実素材でなければ失敗する' do
+        @rp.picture_id = 0
+        @rp.should_not be_valid
+      end
+    end
   end
   
   describe 'データ補充に於いて' do
@@ -213,7 +226,8 @@ describe ResourcePicture do
   describe '単体取得に於いて' do
     before do
       @op = Factory :original_picture, :artist_id => @artist.id
-      @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, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
     end
     it '指定の素材を返す' do
       pic = ResourcePicture.show @rp.id
@@ -248,7 +262,8 @@ describe ResourcePicture do
   describe '一覧取得に於いて' do
     before do
       @op = Factory :original_picture, :artist_id => @artist.id
-      @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, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
     end
     context 'page補正について' do
       it '文字列から数値に変換される' do
@@ -281,20 +296,20 @@ describe ResourcePicture do
     end
     it '時系列で並んでいる' do
       nop = Factory :original_picture, :artist_id => @artist.id
-      nrp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop.id, :updated_at => Time.now + 100
+      nrp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop.id, :picture_id => @p.id, :updated_at => Time.now + 100
       pic = ResourcePicture.list
       pic.should eq [nrp, @rp]
     end
     context 'DBに5件あって1ページの件数を2件に変えたとして' do
       before do
         nop2 = Factory :original_picture, :artist_id => @artist.id
-        @nrp2 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop2.id, :updated_at => Time.now + 100
+        @nrp2 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop2.id, :picture_id => @p.id, :updated_at => Time.now + 100
         nop3 = Factory :original_picture, :artist_id => @artist.id
-        @nrp3 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop3.id, :updated_at => Time.now + 200
+        @nrp3 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop3.id, :picture_id => @p.id, :updated_at => Time.now + 200
         nop4 = Factory :original_picture, :artist_id => @artist.id
-        @nrp4 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop4.id, :updated_at => Time.now + 300
+        @nrp4 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop4.id, :picture_id => @p.id, :updated_at => Time.now + 300
         nop5 = Factory :original_picture, :artist_id => @artist.id
-        @nrp5 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop5.id, :updated_at => Time.now + 400
+        @nrp5 = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop5.id, :picture_id => @p.id, :updated_at => Time.now + 400
         ResourcePicture.stub(:default_page_size).and_return(2)
       end
       it '通常は2件を返す' do
@@ -348,19 +363,19 @@ describe ResourcePicture do
     end
     context '変換するケース' do\r
       it '画像フォーマットがpngかつライセンスの変換禁止フラグが無効のときTrue' do\r
-        License.any_instance.stub(:no_convert).with(any_args).and_return(0)
+        ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)
         @rp.ext = 'png'\r
         @rp.to_gif?.should be_true
       end
     end
     context '変換しないケース' do
       it '画像フォーマットがでない' do\r
-        License.any_instance.stub(:no_convert).with(any_args).and_return(0)
+        ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(0)
         @rp.ext = 'gif'\r
         @rp.to_gif?.should be_false
       end
-      it 'ライセンスの変換禁止フラグが無効' do\r
-        License.any_instance.stub(:no_convert).with(any_args).and_return(1)
+      it '変換禁止フラグが無効' do\r
+        ResourcePicture.any_instance.stub(:flag_gif_convert).with(any_args).and_return(-1)
         @rp.ext = 'png'\r
         @rp.to_gif?.should be_false
       end
@@ -393,86 +408,159 @@ describe ResourcePicture do
     end
   end
   
-  describe '対象素æ\9d\90ã\81®å\8f\96å¾\97に於いて' do
+  describe 'å\8e\9fç\94»ã\81\8bã\82\89ã\81®ã\82³ã\83\94ã\83¼ã\83\87ã\83¼ã\82¿ã\82»ã\83\83ã\83\88に於いて' do
     before do\r
-      @op = Factory.build :original_picture, :ext => 'jpeg', :width => 264, :height => 265, :filesize => 266, \r
+      @op = Factory :original_picture, :ext => 'jpeg', :width => 264, :height => 265, :filesize => 266, \r
         :artist_id => @artist.id
       @rp = Factory.build :resource_picture, \r
         :artist_id => @artist.id
     end
-    context '原画オブジェクトが素材を持っている(更新ケース)' do
-      before do\r
-        OriginalPicture.any_instance.stub(:resource_picture).with(any_args).and_return(@rp)\r
+    it '原画オブジェクトから属性を取り出して対象素材にセットしている' do
+      res = @rp.copy_data(@op)\r
+      @rp.ext.should eq 'jpeg'\r
+      @rp.width.should eq 264\r
+      @rp.height.should eq 265\r
+      @rp.filesize.should eq 266\r
+      @rp.artist_id.should eq @artist.id\r
+    end\r
+  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}\r
+      @rp = Factory.build :resource_picture, attr
+    end
+    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)
       end
-      it 'それを対象素材として返す' do
-        res = ResourcePicture.update_picture(@op)\r
-        res.should eq @rp\r
+      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)
+        res = @rp.op_mgk\r
+      end\r
+    end\r
+    context 'つつがなく終わるとき' do
+      #原画から画像データを取得する。\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)
       end
+      it '画像オブジェクトを返す' do
+        res = @rp.op_mgk\r
+        res.should eq @mgk\r
+      end\r
+    end\r
+  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}\r
+      @rp = Factory.build :resource_picture, attr
+      @mgk = Mgk.new
     end
-    context '持っていない(新規作成ケース)' do
-      before do\r
-        OriginalPicture.any_instance.stub(:resource_picture).with(any_args).and_return(nil)\r
+    context '事前チェック' do
+      before do
+        Picture.any_instance.stub(:copy_data).with(any_args).and_return(true)\r
+        Picture.any_instance.stub(:store).with(any_args).and_return(true)\r
       end
-      it '新規素材モデルを用意し、それを対象素材として返す' do
-        res = ResourcePicture.update_picture(@op)\r
-        res.should be_a_new ResourcePicture\r
+      it '実素材に原画からのコピーデータを依頼している' do
+        Picture.any_instance.should_receive(:copy_data).with(any_args).exactly(1)
+        res = @rp.new_picture @mgk\r
+      end\r
+      it '実素材を保存している' do
+        Picture.any_instance.should_receive(:store).with(any_args).exactly(1)
+        res = @rp.new_picture @mgk\r
+      end\r
+    end
+    context 'つつがなく終わるとき' do
+      it '実素材に原画からのコピーデータをセットしている' do
+        res = @rp.new_picture @mgk\r
+        @rp.ext.should eq @op.ext\r
+        @rp.width.should eq @op.width\r
+        @rp.height.should eq @op.height\r
+        @rp.filesize.should eq @op.filesize\r
+        @rp.artist_id.should eq @op.artist_id\r
+        @rp.original_picture_id.should eq @op.id\r
       end\r
-      it '原画オブジェクトから属性を取り出して対象素材にセットしている' do
-        res = ResourcePicture.update_picture(@op)\r
-        res.ext.should eq 'jpeg'\r
-        res.width.should eq 264\r
-        res.height.should eq 265\r
-        res.filesize.should eq 266\r
-        res.artist_id.should eq @artist.id\r
-        res.license_id.should eq @license.id\r
+      it '実素材を返す' do
+        res = @rp.new_picture @mgk\r
+        res.is_a?(Picture).should be_true\r
       end\r
-      
     end
   end
   
   describe '作成・更新に於いて' do
     before do
       @op = Factory :original_picture, :artist_id => @artist.id
-      @rp = Factory.build :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
+      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
         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
         #それで外部のメソッド呼び出しだけに着目してテストする。
-        ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)\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(:store_picture).with(any_args).and_return(true)\r
-        ResourcePicture.stub(:to_gif?).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
+        ResourcePicture.any_instance.stub(:copy_data).with(any_args).and_return(true)\r
+        ResourcePicture.any_instance.should_receive(:copy_data).with(any_args).exactly(1)
+        res = @rp.store\r
+      end\r
+      it '画像オブジェクトの取得を依頼している' do
+        ResourcePicture.any_instance.should_receive(:op_mgk).with(any_args).exactly(1)
+        res = @rp.store\r
+      end\r
+      it '新規実素材の取得を依頼している' do
+        ResourcePicture.any_instance.should_receive(:new_picture).with(any_args).exactly(1)
+        res = @rp.store\r
+      end\r
       it '自身を保存している' do
         ResourcePicture.any_instance.should_receive(:save).with(any_args).exactly(1)
-        res = @rp.store(Mgk.new)\r
+        res = @rp.store\r
       end\r
       it '画像ファイルの作成・更新機能で画像を保存している' do\r
-        mgk = Mgk.new   #一回目の本画像保存は与えたオブジェクトを使って保存する。
-        ResourcePicture.any_instance.should_receive(:store_picture).with(mgk).exactly(1)
-        res = @rp.store(mgk)\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(Mgk.new)\r
+        res = @rp.store\r
       end\r
       it '自身にGifフォーマット変換を依頼している' do
         ResourcePicture.should_receive(:png_to_gif).with(any_args).exactly(1)
-        res = @rp.store(Mgk.new)\r
+        res = @rp.store\r
       end\r
       it '画像ファイルの作成・更新機能でgif画像を保存している' do\r
         #二回目の保存はgif変換の結果を渡す。
         ResourcePicture.any_instance.should_receive(:store_picture).with(@gifmgk).exactly(1)
-        res = @rp.store(Mgk.new)\r
+        res = @rp.store\r
       end\r
     end
     context 'つつがなく終わるとき' do
       before do
         #すべての処理を正常パターンで通過させ、保存機能をチェックする。
-#        ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)\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(: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
@@ -480,79 +568,129 @@ describe ResourcePicture do
         ResourcePicture.stub(:png_to_gif).with(any_args).and_return(@gifmgk)
       end
       it 'Trueを返す' do
-        res = @rp.store(Mgk.new)\r
+        res = @rp.store\r
         res.should be_true\r
       end\r
+      it '実素材idをセットしている' do
+        res = @rp.store\r
+        @rp.picture_id.should_not be_nil\r
+      end\r
+      it '実素材idから最新画像idを取得してセットしている' do
+        res = @rp.store\r
+        @rp.picture_id.should eq @p.id\r
+      end\r
       it '自身が保存されている' do\r
         lambda {
-          res = @rp.store(Mgk.new)\r
+          res = @rp.store\r
         }.should change ResourcePicture, :count\r
       end\r
     end
     context 'gif変換なしで、つつがなく終わるとき' do
       before do
         #すべての処理を正常パターンで通過させ、保存機能をチェックする。
-        ResourcePicture.any_instance.stub(:save).with(any_args).and_return(true)\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(: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(Mgk.new)\r
+        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(Mgk.new)\r
+        res = @rp.store\r
       end\r
     end
     #以下から例外ケース。処理先頭から失敗させていく
+    context '画像オブジェクトの取得に失敗したとき' do
+      before do
+        ResourcePicture.any_instance.stub(:op_mgk).with(any_args).and_return(false)\r
+      end
+      it 'Falseを返す' do
+        res = @rp.store\r
+        res.should be_false\r
+      end\r
+      it '更新されていない' do
+        @rp.store
+        @rp.should be_a_new ResourcePicture\r
+      end
+    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
+        Picture.any_instance.stub(:valid?).with(any_args).and_return(false)\r
+      end
+      it 'Falseを返す' do
+        res = @rp.store\r
+        res.should be_false\r
+      end\r
+      it '更新されていない' do
+        @rp.store
+        @rp.should be_a_new ResourcePicture\r
+      end
+    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(false)\r
       end
       it 'Falseを返す' do
-        res = @rp.store(Mgk.new)\r
+        res = @rp.store\r
         res.should be_false\r
       end\r
       it '更新されていない' do
-        @rp.store(Mgk.new)
+        @rp.store
         @rp.should be_a_new ResourcePicture\r
       end
     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(Mgk.new)\r
+        res = @rp.store\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(Mgk.new)\r
+        res = @rp.store\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(Mgk.new)\r
+        res = @rp.store\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(Mgk.new)\r
+        res = @rp.store\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
@@ -562,7 +700,7 @@ describe ResourcePicture do
         ResourcePicture.any_instance.stub(:store_picture).with(@gifmgk).and_return(false)\r
       end
       it 'Falseを返す' do
-        res = @rp.store(@mgk)\r
+        res = @rp.store\r
         res.should be_false\r
       end\r
     end
@@ -571,57 +709,33 @@ describe ResourcePicture do
   describe '画像ファイルの作成・更新に於いて' do\r
     #PictureIo経由で画像を保存するための機能。ファイル名に自身のidを使うので事前に自身の保存が必要。\r
     before do
-      PictureIO.resource_picture_io.class.stub(:subdirs).with(any_args).and_return(['', 'v', 'h', 'vh', 'thumbnail'])\r
-      ResourcePicture.any_instance.stub(:h).with(any_args).and_return('data')
-      ResourcePicture.any_instance.stub(:v).with(any_args).and_return('data')
-      ResourcePicture.any_instance.stub(:vh).with(any_args).and_return('data')
-      ResourcePicture.any_instance.stub(:thumbnail).with(any_args).and_return('data')
       @op = Factory :original_picture, :artist_id => @artist.id
-      @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
+      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\r
+      @rp.copy_data @op
     end
     context '事前チェック' do
       before do
         #すべての処理を正常パターンで通過させ、保存機能をチェックする。
         PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)
+        ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)
+        ResourcePicture.any_instance.stub(:thumbnail).with(any_args).and_return('tmbdata')
       end
-      it '画像ファイルの保存が5回呼ばれる' do\r
-        PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(5)
-        res = @rp.store_picture(Mgk.new)\r
-      end\r
-      it '画像ファイルのベースへの保存が1回呼ばれる' do\r
-        PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, '').exactly(1)
-        res = @rp.store_picture(Mgk.new)\r
-      end\r
-      it '画像ファイルの垂直反転への保存が1回呼ばれる' do\r
-        PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'v').exactly(1)
-        res = @rp.store_picture(Mgk.new)\r
-      end\r
-      it '画像ファイルの水平反転への保存が1回呼ばれる' do\r
-        PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'h').exactly(1)
-        res = @rp.store_picture(Mgk.new)\r
-      end\r
-      it '画像ファイルの垂直水平反転への保存が1回呼ばれる' do\r
-        PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'vh').exactly(1)
-        res = @rp.store_picture(Mgk.new)\r
-      end\r
-      it '画像ファイルのサムネイルへの保存が1回呼ばれる' do\r
-        PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'thumbnail').exactly(1)
-        res = @rp.store_picture(Mgk.new)\r
-      end\r
-      it '垂直反転が1回呼ばれる' do\r
-        ResourcePicture.any_instance.should_receive(:v).with(any_args).exactly(1)
+      it 'サムネイル化が1回呼ばれる' do\r
+        ResourcePicture.any_instance.should_receive(:thumbnail).with(any_args).exactly(1)
         res = @rp.store_picture(Mgk.new)\r
       end\r
-      it '水平反転が1回呼ばれる' do\r
-        ResourcePicture.any_instance.should_receive(:h).with(any_args).exactly(1)
+      it '画像ファイルの保存が2回呼ばれる' do\r
+        PictureIO.resource_picture_io.should_receive(:put).with(any_args).exactly(2)
         res = @rp.store_picture(Mgk.new)\r
       end\r
-      it '垂直水平反転が1回呼ばれる' do\r
-        ResourcePicture.any_instance.should_receive(:vh).with(any_args).exactly(1)
+      it '画像ファイルのベースへのサムネイル保存が1回呼ばれる' do\r
+        PictureIO.resource_picture_io.should_receive(:put).with('tmbdata', @rp.filename).exactly(1)
         res = @rp.store_picture(Mgk.new)\r
       end\r
-      it 'サムネイル化が1回呼ばれる' do\r
-        ResourcePicture.any_instance.should_receive(:thumbnail).with(any_args).exactly(1)
+      it '画像ファイルのfullへの保存が1回呼ばれる' do\r
+        PictureIO.resource_picture_io.should_receive(:put).with('data', @rp.filename, 'full').exactly(1)
         res = @rp.store_picture(Mgk.new)\r
       end\r
     end
@@ -629,6 +743,7 @@ describe ResourcePicture do
       before do
         #すべての処理を正常パターンで通過させ、保存機能をチェックする。\r
         PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(true)
+        ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)
       end
       it 'Trueを返す' do
         res = @rp.store_picture(Mgk.new)\r
@@ -637,6 +752,7 @@ describe ResourcePicture do
     end
     context '例外ケース' do
       before do
+        ResourcePicture.any_instance.stub(:flag_thumbnail).with(any_args).and_return(1)
         PictureIO.resource_picture_io.stub(:put).with(any_args).and_return(false)
       end
       it 'Falseを返す' do
@@ -647,11 +763,14 @@ describe ResourcePicture do
     
   end
   
-describe 'サムネイル変換に於いて' do
+  describe 'サムネイル変換に於いて' do
     #サムネイル化した画像データを返すが、スタブをおくので、リサイズと画像データ化を試みるかをチェックする
     before do
       ResourcePicture.stub(:resize).with(any_args).and_return(Mgk.new)
-      @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id
+      @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\r
     end
     it 'サムネイルサイズに縮小した画像データを返す' do\r
       ResourcePicture.should_receive(:resize).with(any_args).exactly(1)
@@ -663,70 +782,5 @@ describe 'サムネイル変換に於いて' do
     end\r
   end
   
-  describe '垂直反転変換に於いて' do
-    #垂直反転した画像データを返すが、スタブをおくので、反転と画像データ化を試みるかをチェックする
-    before do
-      Mgk.any_instance.stub(:flip).with(any_args).and_return(Mgk.new)
-      @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id
-    end\r
-    it '垂直反転する' do\r
-      Mgk.any_instance.should_receive(:flip).exactly(1)
-      @rp.v(Mgk.new)\r
-    end\r
-    it '画像データ化する' do\r
-      Mgk.any_instance.should_receive(:to_blob).exactly(1)
-      @rp.v(Mgk.new)\r
-    end\r
-    it 'データが返る' do\r
-      #全体スタブより\r
-      @rp.v(Mgk.new).should eq 'data'\r
-    end\r
-  end
-  
-  describe '水平反転変換に於いて' do
-    #水平反転した画像データを返すが、スタブをおくので、反転と画像データ化を試みるかをチェックする
-    before do
-      Mgk.any_instance.stub(:flop).with(any_args).and_return(Mgk.new)
-      @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id
-    end
-    it '水平反転する' do\r
-      Mgk.any_instance.should_receive(:flop).exactly(1)
-      @rp.h(Mgk.new)\r
-    end\r
-    it '画像データ化する' do\r
-      Mgk.any_instance.should_receive(:to_blob).exactly(1)
-      @rp.h(Mgk.new)\r
-    end\r
-    it 'データが返る' do\r
-      #全体スタブより\r
-      @rp.h(Mgk.new).should eq 'data'\r
-    end\r
-  end
-  
-  describe '垂直水平反転変換に於いて' do
-    #垂直水平反転した画像データを返すが、スタブをおくので、反転と画像データ化を試みるかをチェックする
-    before do
-      Mgk.any_instance.stub(:flip).with(any_args).and_return(Mgk.new)
-      Mgk.any_instance.stub(:flop).with(any_args).and_return(Mgk.new)
-      @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id
-    end
-    it '垂直反転する' do\r
-      Mgk.any_instance.should_receive(:flip).exactly(1)
-      @rp.vh(Mgk.new)\r
-    end\r
-    it '水平反転する' do\r
-      Mgk.any_instance.should_receive(:flop).exactly(1)
-      @rp.vh(Mgk.new)\r
-    end\r
-    it '画像データ化する' do\r
-      Mgk.any_instance.should_receive(:to_blob).exactly(1)
-      @rp.vh(Mgk.new)\r
-    end\r
-    it 'データが返る' do\r
-      #全体スタブより\r
-      @rp.vh(Mgk.new).should eq 'data'\r
-    end\r
-  end
-  
 end
 \r