OSDN Git Service

t#31060:add export
authoryasushiito <yas@pen-chan.jp>
Sat, 30 Mar 2013 02:47:39 +0000 (11:47 +0900)
committeryasushiito <yas@pen-chan.jp>
Sat, 30 Mar 2013 02:47:39 +0000 (11:47 +0900)
app/controllers/demanders_controller.rb
app/models/demander.rb
config/routes.rb
spec/controllers/demanders_controller_spec.rb
spec/models/demander_spec.rb

index 405000f..ccd5284 100644 (file)
@@ -1,6 +1,6 @@
 class DemandersController < ApplicationController
   layout 'test' if MagicNumber['test_layout']
-  before_filter :authenticate_demand_user!, :only => [:index, :show, :new, :create, :edit, :update, :destroy, :req, :licenses_export, :artists_export, :original_pictures_export, :pictures_export]
+  before_filter :authenticate_demand_user!, :only => [:index, :show, :new, :create, :edit, :update, :destroy, :req, :licenses_export, :artists_export, :original_pictures_export, :pictures_export, :export]
   
   def index
     @demander = @demand_user.demander
@@ -143,4 +143,21 @@ class DemandersController < ApplicationController
     end
   end
   
+  def export
+    @demander = @demand_user.demander
+    date = ymd_to_time params[:date]
+    @exports = @demander.export(date)
+    respond_to do |format|
+      format.html { render :text  }
+      format.json { render :json => 
+        "{" +
+        "\"licenses\":" + @exports[:licenses].to_json + ", " + 
+        "\"artists\":" + @exports[:artists].to_json + ", " + 
+        "\"original_pictures\":" + OriginalPicture.list_as_json_text(@exports[:original_pictures]) + ", " + 
+        "\"pictures\":" + Picture.list_as_json_text(@exports[:pictures]) + 
+        "}" 
+      }
+    end
+  end
+  
 end
index 0e0afe7..c1d6fd0 100644 (file)
@@ -118,24 +118,34 @@ class Demander < ActiveRecord::Base
     self.demander_status.stop
   end
   
-  def licenses_export date_str = nil
+  def licenses_export date = nil
     raise ActiveRecord::Forbidden unless self.status == 3
-    License.export date_str
+    License.export date
   end
   
-  def artists_export date_str = nil
+  def artists_export date = nil
     raise ActiveRecord::Forbidden unless self.status == 3
-    Artist.export date_str
+    Artist.export date
   end
   
-  def original_pictures_export date_str = nil
+  def original_pictures_export date = nil
     raise ActiveRecord::Forbidden unless self.status == 3
-    OriginalPicture.export date_str
+    OriginalPicture.export date
   end
   
-  def pictures_export date_str = nil
+  def pictures_export date = nil
     raise ActiveRecord::Forbidden unless self.status == 3
-    Picture.export date_str
+    Picture.export date
+  end
+  
+  def export date
+    raise ActiveRecord::Forbidden unless self.status == 3
+    {
+      :licenses => self.licenses_export(date), 
+      :artists => self.artists_export(date), 
+      :original_pictures => self.original_pictures_export(date), 
+      :pictures => self.pictures_export(date)
+    }
   end
   
 end
index 2d628a7..5be543c 100644 (file)
@@ -320,6 +320,7 @@ Pettanr::Application.routes.draw do
       get :artists_export
       get :original_pictures_export
       get :pictures_export
+      get :export
     end
     member do
       get :edit
index 8b1ae8f..a7326ca 100644 (file)
@@ -653,17 +653,21 @@ describe DemandersController do
       before do
         sign_out @demand_user
       end
-      it 'ステータスコード302 Foundを返す' do
-        get :licenses_export
-        response.status.should eq 302
-      end
       context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :licenses_export
+          response.status.should eq 302
+        end
         it 'サインインページへ遷移する' do
           get :licenses_export
           response.body.should redirect_to '/demand_users/sign_in'
         end
       end
       context 'json形式' do
+        it 'ステータスコード401を返す' do
+          get :licenses_export, :format => :json
+          response.status.should eq 401
+        end
         it '応答メッセージにUnauthorizedを返す' do
           get :licenses_export, :format => :json
           response.message.should match(/Unauthorized/)
@@ -737,17 +741,21 @@ describe DemandersController do
       before do
         sign_out @demand_user
       end
-      it 'ステータスコード302 Foundを返す' do
-        get :artists_export
-        response.status.should eq 302
-      end
       context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :artists_export
+          response.status.should eq 302
+        end
         it 'サインインページへ遷移する' do
           get :artists_export
           response.body.should redirect_to '/demand_users/sign_in'
         end
       end
       context 'json形式' do
+        it 'ステータスコード401を返す' do
+          get :artists_export, :format => :json
+          response.status.should eq 401
+        end
         it '応答メッセージにUnauthorizedを返す' do
           get :artists_export, :format => :json
           response.message.should match(/Unauthorized/)
@@ -827,17 +835,21 @@ describe DemandersController do
       before do
         sign_out @demand_user
       end
-      it 'ステータスコード302 Foundを返す' do
-        get :original_pictures_export
-        response.status.should eq 302
-      end
       context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :original_pictures_export
+          response.status.should eq 302
+        end
         it 'サインインページへ遷移する' do
           get :original_pictures_export
           response.body.should redirect_to '/demand_users/sign_in'
         end
       end
       context 'json形式' do
+        it 'ステータスコード401を返す' do
+          get :original_pictures_export, :format => :json
+          response.status.should eq 401
+        end
         it '応答メッセージにUnauthorizedを返す' do
           get :original_pictures_export, :format => :json
           response.message.should match(/Unauthorized/)
@@ -920,17 +932,21 @@ describe DemandersController do
       before do
         sign_out @demand_user
       end
-      it 'ステータスコード302 Foundを返す' do
-        get :pictures_export
-        response.status.should eq 302
-      end
       context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :pictures_export
+          response.status.should eq 302
+        end
         it 'サインインページへ遷移する' do
           get :pictures_export
           response.body.should redirect_to '/demand_users/sign_in'
         end
       end
       context 'json形式' do
+        it 'ステータスコード401を返す' do
+          get :pictures_export, :format => :json
+          response.status.should eq 401
+        end
         it '応答メッセージにUnauthorizedを返す' do
           get :pictures_export, :format => :json
           response.message.should match(/Unauthorized/)
@@ -953,4 +969,109 @@ describe DemandersController do
     end
   end
   
+  describe 'エクスポートに於いて' do
+    before do
+      @ds = FactoryGirl.create :demander_status
+      @demander = FactoryGirl.create :demander, :demander_status_id => @ds.id, :demand_user_id => @demand_user.id
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
+      sign_in @demand_user
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        Demander.any_instance.stub(:export).with(any_args).and_return({:licenses => [@license], :artists => [@artist], :original_pictures => [@op], :pictures => [@p]})
+        OriginalPicture.stub(:list_as_json_text).with(any_args).and_return('[]')
+        Picture.stub(:list_as_json_text).with(any_args).and_return('[]')
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :export, :format => :json
+        response.should be_success 
+      end
+      it '日付文字列変換を依頼している' do
+        DemandersController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1)
+        get :export, :date => '20111010', :format => :json
+      end
+      it '借手モデルにエクスポートを問い合わせている' do
+        Demander.any_instance.should_receive(:export).with(Time.parse('2011/10/10')).exactly(1)
+        get :export, :date => '20111010', :format => :json
+      end
+      it '@exportsにリストを取得している' do
+        get :export, :format => :json
+        assigns(:exports).is_a?(Hash).should be_true
+      end
+      context 'html形式' do
+      end
+      context 'json形式' do
+        it '原画モデルにリストのjson化を依頼している' do
+          OriginalPicture.should_receive(:list_as_json_text).with(any_args).exactly(1)
+          OriginalPicture.stub(:list_as_json_text).with(any_args).and_return('[]')
+          get :export, :format => :json
+        end
+        it '実素材モデルにリストのjson化を依頼している' do
+          Picture.should_receive(:list_as_json_text).with(any_args).exactly(1)
+          Picture.stub(:list_as_json_text).with(any_args).and_return('[]')
+          get :export, :format => :json
+        end
+        it 'jsonデータを返す' do
+          get :export, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがHash構造になっている' do
+          get :export, :format => :json
+          json = JSON.parse response.body
+          json.is_a?(Hash).should be_true
+        end
+        it 'Hashっぽいものであって欲しい' do
+          get :export, :format => :json
+          json = JSON.parse response.body
+          json.has_key?("licenses").should be_true
+          json.has_key?("artists").should be_true
+          json.has_key?("original_pictures").should be_true
+          json.has_key?("pictures").should be_true
+        end
+      end
+    end
+    context '借手権限がないとき' do
+      before do
+        sign_out @demand_user
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :export
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :export
+          response.body.should redirect_to '/demand_users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401を返す' do
+          get :export, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :export, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context '借受権限がないとき' do
+      before do
+        Demander.any_instance.stub(:status).and_return(1)
+      end
+      context 'html形式' do
+      end
+      context 'json形式' do
+        it 'ステータスコード403 forbiddenを返す' do
+          lambda{
+            get :export, :format => :json
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+    end
+  end
+  
 end
index b416544..7cb0bb4 100644 (file)
@@ -688,4 +688,65 @@ describe Demander do
     end
   end
   
+  describe 'エクスポートに於いて' do
+    before do
+      @ds = FactoryGirl.create :demander_status, :requested_at => Time.now, :approved_at => Time.now, :rejected_at => Time.now, :receive_hour1 => 0, :receive_hour2 => 0
+      @demander = FactoryGirl.create :demander, :demander_status_id => @ds.id
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
+    end
+    context '事前チェックする' do
+      before do
+        Demander.any_instance.stub(:status).and_return(3)
+        Demander.any_instance.stub(:licenses_export).and_return([@license])
+        Demander.any_instance.stub(:artists_export).and_return([@artist])
+        Demander.any_instance.stub(:original_pictures_export).and_return([@op])
+        Demander.any_instance.stub(:pictures_export).and_return([@p])
+      end
+      it 'ライセンスエクスポートを依頼している' do
+        Demander.any_instance.should_receive(:licenses_export).exactly(1)
+        r = @demander.export Time.now
+      end
+      it '絵師エクスポートを依頼している' do
+        Demander.any_instance.should_receive(:artists_export).exactly(1)
+        r = @demander.export Time.now
+      end
+      it '原画エクスポートを依頼している' do
+        Demander.any_instance.should_receive(:original_pictures_export).exactly(1)
+        r = @demander.export Time.now
+      end
+      it '実素材エクスポートを依頼している' do
+        Demander.any_instance.should_receive(:pictures_export).exactly(1)
+        r = @demander.export Time.now
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        Demander.any_instance.stub(:status).and_return(3)
+        Demander.any_instance.stub(:licenses_export).and_return([@license])
+        Demander.any_instance.stub(:artists_export).and_return([@artist])
+        Demander.any_instance.stub(:original_pictures_export).and_return([@op])
+        Demander.any_instance.stub(:pictures_export).and_return([@p])
+      end
+      it 'Hashを返す' do
+        r = @demander.export Time.now
+        r.is_a?(Hash).should be_true
+        r.has_key?(:licenses).should be_true
+        r.has_key?(:artists).should be_true
+        r.has_key?(:original_pictures).should be_true
+        r.has_key?(:pictures).should be_true
+      end
+    end
+    #例外ケース
+    context '状態が貸与中でないとき' do
+      it '例外403 ActiveRecord::Forbiddenを発生させる' do
+        Demander.any_instance.stub(:status).and_return(2)
+        lambda{
+          r = @demander.export Time.now
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+  end
+  
 end