OSDN Git Service

t#30326:create provider_artist
authoryasushiito <yas@pen-chan.jp>
Mon, 17 Dec 2012 05:10:30 +0000 (14:10 +0900)
committeryasushiito <yas@pen-chan.jp>
Mon, 17 Dec 2012 05:10:30 +0000 (14:10 +0900)
20 files changed:
app/controllers/application_controller.rb
app/controllers/demanders_controller.rb
app/controllers/provider_statuses_controller.rb
app/models/artist.rb
app/models/demander.rb
app/models/provider_artist.rb [new file with mode: 0644]
app/views/artists/browse.html.erb
app/views/artists/list.html.erb
app/views/provider_statuses/artists_import.html.erb [new file with mode: 0644]
app/views/provider_statuses/show.html.erb
config/locales/pettanr.ja.yml
config/routes.rb
db/migrate/20121216060750_create_provider_artists.rb [new file with mode: 0644]
spec/controllers/demanders_controller_spec.rb
spec/controllers/provider_statuses_controller_spec.rb
spec/factories.rb
spec/models/artist_spec.rb
spec/models/demander_spec.rb
spec/models/provider_artist_spec.rb [new file with mode: 0644]
spec/models/provider_license_spec.rb

index d802fbf..5bbc56b 100644 (file)
@@ -98,9 +98,9 @@ class ApplicationController < ActionController::Base
     date
   end
   
-  def export_url demander_url, token, date
+  def export_url demander_url, action, token, date
     u = demander_url + (demander_url[-1] == '/' ? '' : '/')
-    u = URI.join(u, 'licenses_export.json?auth_token=' + token)
+    u = URI.join(u, action + '.json?auth_token=' + token)
     u = URI.join(u, '&date=' + date) unless date.blank?
     u.to_s
   end
index 9549ded..22ee980 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]
+  before_filter :authenticate_demand_user!, :only => [:index, :show, :new, :create, :edit, :update, :destroy, :req, :licenses_export, :artists_export]
   
   def index
     @demander = @demand_user.demander
@@ -106,4 +106,14 @@ class DemandersController < ApplicationController
     end
   end
   
+  def artists_export
+    @demander = @demand_user.demander
+    date = ymd_to_time params[:date]
+    @artists = @demander.artists_export(date)
+    respond_to do |format|
+      format.html { render :text  }
+      format.json { render :json => @artists.to_json }
+    end
+  end
+  
 end
index 866fc14..9395eea 100644 (file)
@@ -1,6 +1,6 @@
 class ProviderStatusesController < ApplicationController
   layout 'test' if MagicNumber['test_layout']
-  before_filter :authenticate_admin!, :only => [:index, :show, :edit, :update, :destroy, :licenses_import]
+  before_filter :authenticate_admin!, :only => [:index, :show, :edit, :update, :destroy, :licenses_import, :artists_import]
 
   def index
     @page = ProviderStatus.page params[:page]
@@ -63,11 +63,23 @@ class ProviderStatusesController < ApplicationController
     @provider_status = ProviderStatus.show(params[:id], @admin)
     raise ActiveRecord::Forbidden if @provider_status.status == 0
     t = ymd_to_time params[:date]
-    url = export_url @provider_status.provider.demander_url, @provider_status.token, t
+    url = export_url @provider_status.provider.demander_url, 'licenses_export', @provider_status.token, t
     @failures = ProviderLicense.import @provider_status.provider.id, export_from_provider(url)
     respond_to do |format|
       format.html # show.html.erb
-      format.json { render :json => @provider_status.to_json(ProviderStatus.show_json_opt) }
+      format.json { render :json => @failures.to_json() }
+    end
+  end
+  
+  def artists_import
+    @provider_status = ProviderStatus.show(params[:id], @admin)
+    raise ActiveRecord::Forbidden if @provider_status.status == 0
+    t = ymd_to_time params[:date]
+    url = export_url @provider_status.provider.demander_url, 'artists_export', @provider_status.token, t
+    @failures = ProviderArtist.import @provider_status.provider.id, export_from_provider(url)
+    respond_to do |format|
+      format.html # show.html.erb
+      format.json { render :json => @failures.to_json() }
     end
   end
   
index 4dc1e50..93c6716 100644 (file)
@@ -114,4 +114,16 @@ class Artist < ActiveRecord::Base
     Artist.count :conditions => ['artists.author_id is not null']
   end
   
+  def self.export(dt = nil)
+    opt = {}
+    cond = if dt
+      ['artists.author_id is not null and artists.updated_at >= ?', dt]
+    else
+      'artists.author_id is not null'
+    end
+    opt.merge!({:conditions => cond}) 
+    opt.merge!({:order => 'id'})
+    Artist.find(:all, opt)
+  end
+  
 end
index 7a882e0..61bc408 100644 (file)
@@ -123,4 +123,9 @@ class Demander < ActiveRecord::Base
     License.export date_str
   end
   
+  def artists_export date_str = nil
+    raise ActiveRecord::Forbidden unless self.status == 3
+    Artist.export date_str
+  end
+  
 end
diff --git a/app/models/provider_artist.rb b/app/models/provider_artist.rb
new file mode 100644 (file)
index 0000000..6f2d6d1
--- /dev/null
@@ -0,0 +1,45 @@
+class ProviderArtist < ActiveRecord::Base
+  belongs_to :provider
+  belongs_to :artist, :foreign_key => :demanders_artist_id
+  
+  validates :provider_id, :presence => true, :numericality => true, :existence => true
+  validates :providers_artist_id, :presence => true, :numericality => true
+  validates :demanders_artist_id, :presence => true, :numericality => true
+  
+  def self.get_one pid, paid
+    ProviderArtist.find_by_provider_id_and_providers_artist_id(pid, paid) || ProviderArtist.new
+  end
+  
+  def modify_artist attr
+    ar = if self.artist
+      self.artist
+    else
+      Artist.new
+    end
+    ar.attributes = attr
+    ar.author_id = nil
+    ar.save
+    ar
+  end
+  
+  def self.import pid, providers_artists
+    res = []
+    ProviderArtist.transaction do
+      providers_artists.each do |providers_artist_attr|
+        provider_artist = ProviderArtist.get_one(pid, providers_artist_attr['id'])
+        demander_artist = provider_artist.modify_artist providers_artist_attr
+        if demander_artist.valid?
+          provider_artist.attributes = {:provider_id => pid, :providers_artist_id => providers_artist_attr['id'], :demanders_artist_id => demander_artist.id}
+          unless provider_artist.save
+            res << providers_artist_attr
+          end
+        else
+          res << providers_artist_attr
+        end
+      end
+      raise ActiveRecord::Rollback if res.any?
+    end
+    res
+  end
+  
+end
index 2602e3a..17c920e 100644 (file)
 </p>
 
 <p>
+  <b><%= t_m 'Artist.author_id' -%>:</b>
+  <% if @ar.author_id %>
+    <%= link_to @ar.author_id, browse_author_path(@ar.author) %>
+  <% end %>
+</p>
+
+<p>
   <b><%= t_m 'Artist.created_at' -%>:</b>
   <%= l @ar.created_at %>
 </p>
index abca085..99947d0 100644 (file)
@@ -4,14 +4,20 @@
   <tr>
     <th><%= t_m 'Artist.id' -%></th>
     <th><%= t_m 'Artist.name' -%></th>
+    <th><%= t_m 'Artist.author_id' -%></th>
     <th><%= t_m 'Artist.created_at' -%></th>
     <th><%= t_m 'Artist.updated_at' -%></th>
   </tr>
-
+  
   <% @artists.each do |artist| %>
     <tr>
       <td><%= link_to artist.id, browse_artist_path(artist) %></td>
       <td><%= h(truncate(artist.name, :length => 12)) %></td>
+      <td>
+        <% if artist.author_id %>
+          <%= link_to artist.author_id, browse_author_path(artist.author) %>
+        <% end %>
+      </td>
       <td><%= l artist.created_at %></td>
       <td><%= l artist.updated_at %></td>
     </tr>
diff --git a/app/views/provider_statuses/artists_import.html.erb b/app/views/provider_statuses/artists_import.html.erb
new file mode 100644 (file)
index 0000000..0f96f7b
--- /dev/null
@@ -0,0 +1,29 @@
+<h1><%= t('.title') %></h1>
+<p id="notice"><%= notice %></p>
+
+<% if @failures.empty? %>
+  <h2>
+    <%= t('provider_statuses.artists_import.success') %>
+  </h2>
+<% else %>
+  <h2>
+    <%= t('provider_statuses.artists_import.fail', :size => @failures.size) %>
+  </h2>
+  <table>
+    <tr>
+      <th><%= t_m 'License.caption' -%></th>
+      <th><%= t_m 'License.name' -%></th>
+    </tr>
+    <% @failures.each do |artist| %>
+      <tr>
+        <td>
+          <%= link_to h(artist['caption']), artist['url'] %>
+        </td>
+        <td>
+          <%= h(artist['name']) %>
+        </td>
+      </tr>
+    <% end %>
+  </table>
+  <%= link_to t('provider_statuses.artists_import.to_show'), provider_status_path(@provider_status) %>
+<% end %>
index 8d772e9..4baeac9 100644 (file)
@@ -37,6 +37,9 @@
     <%= link_to t('provider_statuses.show.licenses_import'), licenses_import_provider_status_path(@provider_status), :method => :post %>
   </p>
   <p>
+    <%= link_to t('provider_statuses.show.artists_import'), artists_import_provider_status_path(@provider_status), :method => :post %>
+  </p>
+  <p>
     <%= link_to t('provider_statuses.show.receive_stop'), provider_status_path(@provider_status), :method => :delete %>
   </p>
 <% end %>
index 9ab7888..9828386 100644 (file)
@@ -263,6 +263,9 @@ ja:
         created_at: 作成
         updated_at: 更新
       provider_artist:
+        provider_id: 貸手
+        providers_artist_id: 貸手側絵師
+        demanders_artist_id: 借手側絵師
         created_at: 作成
         updated_at: 更新
       provider_original_picture:
@@ -635,6 +638,11 @@ ja:
       success: インポートが完了しました
       fail: 次のライセンス(%{size}件)でエラーがありました。ライセンスのインストール状況を確認してください。
       to_show: 借受状況を表示する
+    artists_import:
+      title: 絵師インポート
+      success: インポートが完了しました
+      fail: 次の絵師(%{size}件)でエラーがありました。
+      to_show: 借受状況を表示する
     status: 状態
     open: 開く
     to_provider: 貸手に切り替える
index f0b5e89..d00e523 100644 (file)
@@ -299,6 +299,7 @@ Pettanr::Application.routes.draw do
       put :update
       delete :destroy
       post :licenses_import
+      post :artists_import
     end
   end
   resources :demanders do
@@ -313,6 +314,7 @@ Pettanr::Application.routes.draw do
       get :browse
       post :req
       get :licenses_export
+      get :artists_export
     end
     member do
       get :edit
diff --git a/db/migrate/20121216060750_create_provider_artists.rb b/db/migrate/20121216060750_create_provider_artists.rb
new file mode 100644 (file)
index 0000000..0bf3525
--- /dev/null
@@ -0,0 +1,11 @@
+class CreateProviderArtists < ActiveRecord::Migration
+  def change
+    create_table :provider_artists do |t|
+      t.integer :provider_id, :null => false, :default => 0
+      t.integer :providers_artist_id, :null => false, :default => 0
+      t.integer :demanders_artist_id, :null => false, :default => 0
+
+      t.timestamps
+    end
+  end
+end
index ed7b6f0..08af954 100644 (file)
@@ -9,7 +9,7 @@ describe DemandersController do
     @lg = FactoryGirl.create :license_group
     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
     @user = FactoryGirl.create :user_yas
-    @author = @user.author    #ユーザ作成時に連動して作成される
+    @author = FactoryGirl.create :author, :user_id => @user.id
     @demand_user = FactoryGirl.create :demand_user
   end
   
@@ -685,4 +685,89 @@ describe DemandersController do
       end
     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
+      sign_in @demand_user
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        Demander.any_instance.stub(:artists_export).with(any_args).and_return([@artist, @artist, @artist])
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :artists_export, :format => :json
+        response.should be_success 
+      end
+      it '日付文字列変換を依頼している' do
+        DemandersController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1)
+        get :artists_export, :date => '20111010', :format => :json
+      end
+      it '借手モデルに絵師エクスポートを問い合わせている' do
+        Demander.any_instance.should_receive(:artists_export).with(Time.parse('2011/10/10')).exactly(1)
+        get :artists_export, :date => '20111010', :format => :json
+      end
+      it '@artistsにリストを取得している' do
+        get :artists_export, :format => :json
+        assigns(:artists).should have_at_least(3).items
+      end
+      context 'html形式' do
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :artists_export, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+        it 'データがリスト構造になっている' do
+          get :artists_export, :format => :json
+          json = JSON.parse response.body
+          json.should have_at_least(3).items
+        end
+        it 'リストの先頭くらいは絵師っぽいものであって欲しい' do
+          get :artists_export, :format => :json
+          json = JSON.parse response.body
+          json.first.has_key?("name").should be_true
+          json.first.has_key?("author_id").should be_true
+        end
+      end
+    end
+    context '借手権限がないとき' 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 'サインインページへ遷移する' do
+          get :artists_export
+          response.body.should redirect_to '/demand_users/sign_in'
+        end
+      end
+      context 'json形式' do
+        it '応答メッセージにUnauthorizedを返す' do
+          get :artists_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 :artists_export, :format => :json
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+    end
+  end
+  
 end
index ed08f0e..db2c7b3 100644 (file)
@@ -9,7 +9,8 @@ describe ProviderStatusesController do
     @lg = FactoryGirl.create :license_group
     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
     @user = FactoryGirl.create :user_yas
-    @author = @user.author    #ユーザ作成時に連動して作成される
+    @author = FactoryGirl.create :author, :user_id => @user.id
+    @artist = FactoryGirl.create :artist, :author_id => @author.id
   end
   
   describe '一覧表示に於いて' do
@@ -369,7 +370,7 @@ describe ProviderStatusesController do
       before do
         ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
         ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
-        ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return(@ps)
+        ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/licenses_export/1.json')
         ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
         ProviderLicense.stub(:import).with(any_args).and_return([])
       end
@@ -383,7 +384,7 @@ describe ProviderStatusesController do
       end
       it 'エクスポートurl取得を依頼している' do
         ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
-        ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, @ps.token, Time.parse('2011/10/10')).exactly(1)
+        ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, 'licenses_export', @ps.token, Time.parse('2011/10/10')).exactly(1)
         get :licenses_import, :id => @ps.id, :date => '20111010'
       end
       it '貸手からのエクスポートを依頼している' do
@@ -400,7 +401,7 @@ describe ProviderStatusesController do
         ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
         ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
         ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
-        ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return(@ps)
+        ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/licenses_export/1.json')
         ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
         ProviderLicense.stub(:import).with(any_args).and_return([])
       end
@@ -475,4 +476,121 @@ describe ProviderStatusesController do
     end
   end
   
+  describe '絵師インポートに於いて' do
+    before do
+      @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+      @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+      ProviderStatus.stub(:show).and_return(@ps)
+      sign_in @admin
+    end
+    context '事前チェックしておく' do
+      before do
+        ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
+        ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
+        ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/artists_export/1.json')
+        ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@artist.attributes])
+        ProviderArtist.stub(:import).with(any_args).and_return([])
+      end
+      it '借受状況モデルに単体取得を問い合わせている' do
+        ProviderStatus.should_receive(:show).exactly(1)
+        get :artists_import, :id => @ps.id
+      end
+      it '日付文字列変換を依頼している' do
+        ProviderStatusesController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1)
+        get :licenses_import, :id => @ps.id, :date => '20111010'
+      end
+      it 'エクスポートurl取得を依頼している' do
+        ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
+        ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, 'artists_export', @ps.token, Time.parse('2011/10/10')).exactly(1)
+        get :artists_import, :id => @ps.id, :date => '20111010'
+      end
+      it '貸手からのエクスポートを依頼している' do
+        ProviderStatusesController.any_instance.should_receive(:export_from_provider).exactly(1)
+        get :artists_import, :id => @ps.id
+      end
+      it '絵師対照表モデルにインポートを依頼している' do
+        ProviderArtist.should_receive(:import).exactly(1)
+        get :artists_import, :id => @ps.id
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
+        ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
+        ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
+        ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/artists_export/1.json')
+        ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@artist.attributes])
+        ProviderLicense.stub(:import).with(any_args).and_return([])
+      end
+      it 'ステータスコード200 OKを返す' do
+        get :artists_import, :id => @ps.id
+        response.should be_success
+      end
+      it '@provider_statusに借受状況を取得している' do
+        get :artists_import, :id => @ps.id
+        assigns(:provider_status).should eq(@ps)
+      end
+      it '@failuresにインポート失敗リストを取得している' do
+        get :artists_import, :id => @ps.id
+        assigns(:failures).should eq([])
+      end
+      context 'html形式' do
+        it 'artists_importテンプレートを描画する' do
+          get :artists_import, :id => @ps.id
+          response.should render_template("artists_import")
+        end
+      end
+      context 'json形式' do
+        it 'jsonデータを返す' do
+          get :artists_import, :id => @ps.id, :format => :json
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+        end
+      end
+    end
+    context '管理者権限がないとき' do
+      before do
+        sign_out @admin
+      end
+      context 'html形式' do
+        it 'ステータスコード302 Foundを返す' do
+          get :artists_import, :id => @ps.id
+          response.status.should eq 302
+        end
+        it 'サインインページへ遷移する' do
+          get :artists_import, :id => @ps.id
+          response.body.should redirect_to '/admins/sign_in'
+        end
+      end
+      context 'json形式' do
+        it 'ステータスコード401 Unauthorizedを返す' do
+          get :artists_import, :id => @ps.id, :format => :json
+          response.status.should eq 401
+        end
+        it '応答メッセージにUnauthorizedを返す' do
+          get :artists_import, :id => @ps.id, :format => :json
+          response.message.should match(/Unauthorized/)
+        end
+      end
+    end
+    context '借受権限がないとき' do
+      before do
+        ProviderStatus.any_instance.stub(:status).and_return(0)
+      end
+      context 'html形式' do
+        it '例外403 forbiddenを返す' do
+          lambda{
+            get :artists_import, :id => @ps.id
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+      context 'json形式' do
+        it '例外403 forbiddenを返す' do
+          lambda{
+            get :artists_import, :id => @ps.id, :format => :json
+          }.should raise_error(ActiveRecord::Forbidden)
+        end
+      end
+    end
+  end
+  
 end
index 2fe602a..254b3c9 100644 (file)
@@ -249,4 +249,11 @@ FactoryGirl.define do
     provider_license.providers_license_id nil
     provider_license.demanders_license_id nil
   end
+
+  factory :provider_artist, :class => ProviderArtist do |provider_artist|
+    provider_artist.provider_id nil
+    provider_artist.providers_artist_id nil
+    provider_artist.demanders_artist_id nil
+  end
+
 end
index 3ea2d45..c555521 100644 (file)
@@ -341,5 +341,22 @@ describe Artist do
       r.should eq 2
     end
   end
+  
+  describe 'エクスポートに於いて' do
+    before do
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+      @artist2 = FactoryGirl.create :artist, :author_id => @other_author.id, :updated_at => Time.now - 3000
+      @artist3 = FactoryGirl.create :artist, :author_id => nil
+    end
+    it '開始日時が省略された場合はすべての内絵師を返す' do
+      r = Artist.export 
+      r.should eq [@artist, @artist2]
+    end
+    it '開始日時以降に更新された内絵師を返す' do
+      r = Artist.export @artist.updated_at - 100
+      r.should eq [@artist]
+    end
+  end
+  
 
 end
index 525e717..638a40a 100644 (file)
@@ -9,7 +9,7 @@ describe Demander do
     @lg = FactoryGirl.create :license_group
     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
     @user = FactoryGirl.create :user_yas
-    @author = @user.author    #ユーザ作成時に連動して作成される
+    @author = FactoryGirl.create :author, :user_id => @user.id
     @demand_user = FactoryGirl.create :demand_user
   end
   describe '検証に於いて' do
@@ -544,4 +544,51 @@ 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
+    end
+    context '事前チェックする' do
+      before do
+        Demander.any_instance.stub(:status).and_return(3)
+      end
+      it '絵師モデルにエクスポートを依頼している' do
+        Artist.stub(:export).with(nil).and_return([@artist])
+        Artist.should_receive(:export).with(nil).exactly(1)
+        r = @demander.artists_export
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        Demander.any_instance.stub(:status).and_return(3)
+      end
+      it '絵師リストを返す' do
+        Artist.stub(:export).with(Time.parse('2011/10/10')).and_return([@artist])
+        r = @demander.artists_export Time.parse('2011/10/10')
+        r.should eq [@artist]
+      end
+    end
+    context '日時指定しないとき' do
+      before do
+        Demander.any_instance.stub(:status).and_return(3)
+      end
+      it 'パラメータなしでリストを取得する' do
+        Artist.stub(:export).with(nil).and_return([@artist])
+        Artist.should_receive(:export).with(nil).exactly(1)
+        r = @demander.artists_export nil
+      end
+    end
+    #例外ケース
+    context '状態が貸与中でないとき' do
+      it '例外403 ActiveRecord::Forbiddenを発生させる' do
+        Demander.any_instance.stub(:status).and_return(2)
+        lambda{
+          r = @demander.artists_export
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+  end
+  
 end
diff --git a/spec/models/provider_artist_spec.rb b/spec/models/provider_artist_spec.rb
new file mode 100644 (file)
index 0000000..1f4e275
--- /dev/null
@@ -0,0 +1,187 @@
+# -*- encoding: utf-8 -*-
+#絵師対照表
+require 'spec_helper'
+
+describe ProviderArtist do
+  before do
+    @admin = FactoryGirl.create :admin
+    @sp = FactoryGirl.create :system_picture
+    @lg = FactoryGirl.create :license_group
+    @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    @user = FactoryGirl.create :user_yas
+    @author = FactoryGirl.create :author, :user_id => @user.id
+    @artist = FactoryGirl.create :artist, :author_id => @author.id
+    @other_user = FactoryGirl.create :user_yas
+    @other_author = FactoryGirl.create :author, :user_id => @other_user.id
+    @provider_status = FactoryGirl.create :provider_status
+    @provider = FactoryGirl.create :provider, :provider_status_id => @provider_status.id
+  end
+  describe '検証に於いて' do
+    before do
+      @pa = FactoryGirl.build :provider_artist, :provider_id => @provider.id, :providers_artist_id => 2, :demanders_artist_id => @artist.id
+    end
+    
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @pa.should be_valid
+      end
+      it '上限データが通る' do
+        @pa.should be_valid
+      end
+    end
+    
+    context 'provider_idを検証するとき' do
+      it 'nullなら失敗する' do
+        @pa.provider_id = nil
+        @pa.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pa.provider_id = 'a'
+        @pa.should_not be_valid
+      end
+      it '存在する貸手でなければ失敗する' do
+        @pa.provider_id = 0
+        @pa.should_not be_valid
+      end
+    end
+    context 'providers_artist_idを検証するとき' do
+      it 'nullなら失敗する' do
+        @pa.providers_artist_id = nil
+        @pa.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pa.providers_artist_id = 'a'
+        @pa.should_not be_valid
+      end
+    end
+    context 'demanders_artist_idを検証するとき' do
+      it 'nullなら失敗する' do
+        @pa.demanders_artist_id = nil
+        @pa.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pa.demanders_artist_id = 'a'
+        @pa.should_not be_valid
+      end
+    end
+  end
+  
+  describe '対照表取得に於いて' do
+    before do
+      @pa = FactoryGirl.create :provider_artist, :provider_id => @provider.id, :providers_artist_id => 2, :demanders_artist_id => @artist.id
+    end
+    it '取得した対照表を返す' do
+      r = ProviderArtist.get_one @provider.id, 2
+      r.should be_true
+    end
+    it '該当する対照表が存在しなかったら、新規に興した対照表オブジェクトを返す' do
+      r = ProviderArtist.get_one @provider.id, 1
+      r.should be_a_new ProviderArtist
+      r = ProviderArtist.get_one 0, 2
+      r.should be_a_new ProviderArtist
+    end
+  end
+  
+  describe '貸手側絵師同期に於いて' do
+    before do
+      @pa = FactoryGirl.create :provider_artist, :provider_id => @provider.id, :providers_artist_id => 2, :demanders_artist_id => @artist.id
+      @newpa = ProviderArtist.new
+    end
+    context '自身に絵師がリンクしてないとき' do
+      before do
+      end
+      it '絵師オブジェクトを新規に興す' do
+        lambda {
+          r = @newpa.modify_artist @artist.attributes
+        }.should change Artist, :count
+      end
+    end
+    context '自身に絵師がリンクしているとき' do
+      it '自身から絵師を取得する' do
+        lambda {
+          r = @pa.modify_artist @artist.attributes
+        }.should_not change Artist, :count
+      end
+    end
+    it '貸手側絵師カラム値から絵師名をセットする 絵師オブジェクトを保存する' do
+      r = @pa.modify_artist :name => 'artist2'
+      @artist.reload
+      @artist.name.should eq 'artist2'
+    end
+    it '作家idをクリアする。(外絵師の証明)' do
+      r = @pa.modify_artist :name => 'artist2'
+      @artist.reload
+      @artist.author_id.should be_blank
+    end
+    it '絵師オブジェクトを返す' do
+      r = @pa.modify_artist @artist.attributes
+      r.should eq @artist
+    end
+  end
+
+  describe 'インポートに於いて' do
+    before do
+      @artist2 = FactoryGirl.create :artist, :author_id => @other_author.id, :name => 'artist2'
+    end
+    context '事前チェックする' do
+      before do
+        ProviderArtist.stub(:get_one).with(any_args).and_return(ProviderArtist.new)
+        ProviderArtist.any_instance.stub(:modify_artist).with(any_args).and_return(@artist)
+        ProviderArtist.any_instance.stub(:save).with(any_args).and_return(true)
+      end
+      it '対照表取得を問い合わせている' do
+        ProviderArtist.should_receive(:get_one).with(any_args).exactly(1)
+        r = ProviderArtist.import @provider.id, [@artist.attributes]
+      end
+      it '対照表に絵師同期を依頼してしている' do
+        ProviderArtist.any_instance.should_receive(:modify_artist).with(any_args).exactly(1)
+        r = ProviderArtist.import @provider.id, [@artist.attributes]
+      end
+      it '対照表オブジェクトを保存している' do
+        ProviderArtist.any_instance.should_receive(:save).with(any_args).exactly(1)
+        r = ProviderArtist.import @provider.id, [@artist.attributes]
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it '空っぽの配列を返す' do
+        r = ProviderArtist.import @provider.id, [@artist.attributes]
+        r.should be_empty
+      end
+      it '対照表が追加される' do
+        lambda {
+          r = ProviderArtist.import @provider.id, [@artist.attributes]
+        }.should change ProviderArtist, :count
+      end
+    end
+    context '複数インポートのとき' do
+      it '空っぽの配列を返す' do
+        r = ProviderArtist.import @provider.id, [@artist.attributes, @artist2.attributes]
+        r.should be_empty
+      end
+      it '対照表が追加される' do
+        lambda {
+          r = ProviderArtist.import @provider.id, [@artist.attributes, @artist2.attributes]
+        }.should change(ProviderArtist, :count).by(2)
+      end
+    end
+    #警告ケース
+    context '対照表オブジェクトの保存に失敗したとき' do
+      before do
+        ProviderArtist.any_instance.stub(:save).with(any_args).and_return(false)
+      end
+     it '結果に貸手側絵師のカラム値を追加している' do
+        r = ProviderArtist.import @provider.id, [@artist.attributes]
+        r.should_not be_empty
+      end
+    end
+    context '絵師オブジェクトの保存に失敗したとき' do
+      before do
+        Artist.any_instance.stub(:save).with(any_args).and_return(false)
+      end
+      it '結果に貸手側絵師のカラム値を追加している' do
+        r = ProviderArtist.import @provider.id, [@artist.attributes]
+        r.should_not be_empty
+      end
+    end
+  end
+end
index 50501fc..3983a3f 100644 (file)
@@ -36,7 +36,7 @@ describe ProviderLicense do
         @pl.provider_id = 'a'
         @pl.should_not be_valid
       end
-      it 'å­\98å\9c¨ã\81\99ã\82\8b貸æ\89\8bã\83¦ã\83¼ã\82¶ã\81§ã\81ªã\81\91ã\82\8cã\81°å¤±æ\95\97ã\81\99ã\82\8b' do
+      it '存在する貸手でなければ失敗する' do
         @pl.provider_id = 0
         @pl.should_not be_valid
       end