end
def self.default_page_size
- 100 #25
+ 25
end
def self.max_page_size
page_size
end
+ def self.mylist_where ar
+ ['resource_pictures.artist_id = ?', ar.id]
+ end
+
+ def self.himlist_where ar
+ ['resource_pictures.artist_id = ?', ar.id]
+ end
+
def self.list page = 1, page_size = self.default_page_size
- opt = {}
- opt.merge!(self.list_opt)
- opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
- opt.merge!({:order => 'updated_at desc'})
- ResourcePicture.find(:all, opt)
+ ResourcePicture.includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.mylist ar, page = 1, page_size = Author.default_resource_picture_page_size
+ ResourcePicture.where(self.mylist_where(ar)).includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.himlist ar, page = 1, page_size = Author.default_resource_picture_page_size
+ ResourcePicture.where(self.himlist_where(ar)).includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').offset((page -1) * page_size).limit(page_size)
+ end
+
+ def self.list_paginate page = 1, page_size = self.default_page_size
+ Kaminari.paginate_array(Array.new(ResourcePicture.count, nil)).page(page).per(page_size)
+ end
+
+ def self.mylist_paginate ar, page = 1, page_size = Author.default_resource_picture_page_size
+ Kaminari.paginate_array(Array.new(ResourcePicture.where(self.mylist_where(ar)).count, nil)).page(page).per(page_size)
+ end
+
+ def self.himlist_paginate ar, page = 1, page_size = Author.default_resource_picture_page_size
+ Kaminari.paginate_array(Array.new(ResourcePicture.where(self.himlist_where(ar)).count, nil)).page(page).per(page_size)
end
def self.list_opt
- {:include => {:license => {}, :artist => {}, :picture => {}} }
+ {:license => {}, :artist => {}, :picture => {} }
end
def self.list_json_opt
{:include => {:license => {}, :artist => {}, :picture => {}} }
end
- def self.mylist ar, page = 1, page_size = Author.default_resource_picture_page_size
- opt = {}
- opt.merge!(ResourcePicture.list_opt)
- opt.merge!({:limit => page_size, :offset => (page -1) * page_size}) if page_size > 0
- opt.merge!({:conditions => ['resource_pictures.artist_id = ?', ar.id], :order => 'resource_pictures.updated_at desc'})
- ResourcePicture.find(:all, opt)
- end
-
def self.show rid, roles
opt = {}
opt.merge!(self.show_opt)
r.should eq [@rp]\r
end\r
end\r
- context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
- before do\r
- nop2 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
- @nrp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop2.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
- nop3 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
- @nrp3 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop3.id, :picture_id => @p.id, :updated_at => Time.now + 200\r
- nop4 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
- @nrp4 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop4.id, :picture_id => @p.id, :updated_at => Time.now + 300\r
- nop5 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
- @nrp5 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop5.id, :picture_id => @p.id, :updated_at => Time.now + 400\r
- ResourcePicture.stub(:default_page_size).and_return(2)\r
- end\r
- it '件数0は全件(5件)を返す' do\r
- r = ResourcePicture.list 5, 0\r
- r.should have(5).items \r
- end\r
- end\r
end\r
- describe '一覧取得オプションに於いて' do\r
- it 'includeキーを含んでいる' do\r
- r = ResourcePicture.list_opt\r
- r.has_key?(:include).should be_true\r
- end\r
- it '3つの項目を含んでいる' do\r
- r = ResourcePicture.list_opt[:include]\r
- r.should have(3).items\r
- end\r
- it 'ライセンスを含んでいる' do\r
- r = ResourcePicture.list_opt[:include]\r
- r.has_key?(:license).should be_true\r
- end\r
- it '絵師を含んでいる' do\r
- r = ResourcePicture.list_opt[:include]\r
- r.has_key?(:artist).should be_true\r
- end\r
- it '実素材を含んでいる' do\r
- r = ResourcePicture.list_opt[:include]\r
- r.has_key?(:picture).should be_true\r
- end\r
- end\r
- describe 'json一覧出力オプションに於いて' do\r
+ \r
+ describe '一覧ページ制御に於いて' do\r
before do\r
- @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
- @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
- @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
- @sbt = FactoryGirl.create :speech_balloon_template\r
- @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1\r
- @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1\r
- @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id\r
+ ResourcePicture.stub(:count).with(any_args).and_return(100)\r
end\r
- it 'ライセンスを含んでいる' do\r
- r = ResourcePicture.list.to_json ResourcePicture.list_json_opt\r
- j = JSON.parse r\r
- i = j.first\r
- i.has_key?('license').should be_true\r
+ it 'ページ制御を返す' do\r
+ r = ResourcePicture.list_paginate\r
+ r.is_a?(Kaminari::PaginatableArray).should be_true\r
end\r
- it '絵師を含んでいる' do\r
- r = ResourcePicture.list.to_json ResourcePicture.list_json_opt\r
- j = JSON.parse r\r
- i = j.first\r
- i.has_key?('artist').should be_true\r
- end\r
- it '実素材を含んでいる' do\r
- r = ResourcePicture.list.to_json ResourcePicture.list_json_opt\r
- j = JSON.parse r\r
- i = j.first\r
- i.has_key?('picture').should be_true\r
+ it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do\r
+ r = ResourcePicture.list_paginate 3, 10\r
+ r.limit_value.should eq 10\r
+ r.offset_value.should eq 20\r
end\r
end\r
\r
c.should eq [@rp]\r
end\r
end\r
- context 'DBに5件あって1ページの件数を0件に変えたとして' do\r
+ end\r
+ \r
+ describe '自分の素材一覧ページ制御に於いて' do\r
+ before do\r
+ ResourcePicture.stub(:count).with(any_args).and_return(100)\r
+ end\r
+ it 'ページ制御を返す' do\r
+ r = ResourcePicture.mylist_paginate @artist\r
+ r.is_a?(Kaminari::PaginatableArray).should be_true\r
+ end\r
+ it '自分の素材一覧の取得条件を利用している' do\r
+ ResourcePicture.stub(:mylist_where).with(any_args).and_return('')\r
+ ResourcePicture.should_receive(:mylist_where).with(any_args).exactly(1)\r
+ r = ResourcePicture.mylist_paginate @artist\r
+ end\r
+ it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do\r
+ r = ResourcePicture.mylist_paginate @artist, 3, 10\r
+ r.limit_value.should eq 10\r
+ r.offset_value.should eq 20\r
+ end\r
+ end\r
+\r
+ describe '他作家の素材一覧取得に於いて' do\r
+ before do\r
+ @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
+ @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
+ @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
+ @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
+ @other_p = FactoryGirl.create :picture, :original_picture_id => @other_op.id, :license_id => @license.id, :artist_id => @other_artist.id\r
+ @other_rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @other_op.id, :picture_id => @other_p.id\r
+ end\r
+ context 'つつがなく終わるとき' do\r
+ it '一覧取得オプションを利用している' do\r
+ ResourcePicture.stub(:list_opt).with(any_args).and_return({})\r
+ ResourcePicture.should_receive(:list_opt).with(any_args).exactly(1)\r
+ r = ResourcePicture.himlist @other_artist\r
+ end\r
+ end\r
+ it '指定した作家のリストを返す' do\r
+ r = ResourcePicture.himlist @other_artist\r
+ r.should eq [@other_rp]\r
+ end\r
+ it '時系列で並んでいる' do\r
+ nop = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
+ nrp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => nop.id, :picture_id => @other_p.id, :updated_at => Time.now + 100\r
+ r = ResourcePicture.himlist @other_artist\r
+ r.should eq [nrp, @other_rp]\r
+ end\r
+ context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
before do\r
- @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
- @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op2.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
- @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
- @rp3 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op3.id, :picture_id => @p.id, :updated_at => Time.now + 200\r
- @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
- @rp4 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op4.id, :picture_id => @p.id, :updated_at => Time.now + 300\r
- @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
- @rp5 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op5.id, :picture_id => @p.id, :updated_at => Time.now + 400\r
- Author.stub(:default_resource_picture_page_size).and_return(2)\r
+ @op2 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
+ @rp2 = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op2.id, :picture_id => @p.id, :updated_at => Time.now + 100\r
+ @op3 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
+ @rp3 = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op3.id, :picture_id => @p.id, :updated_at => Time.now + 200\r
+ @op4 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
+ @rp4 = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op4.id, :picture_id => @p.id, :updated_at => Time.now + 300\r
+ @op5 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id\r
+ @rp5 = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op5.id, :picture_id => @p.id, :updated_at => Time.now + 400\r
+ end\r
+ it '通常は2件を返す' do\r
+ r = ResourcePicture.himlist @other_artist, 1, 2\r
+ r.should have(2).items \r
end\r
- it '通常は全件(5件)を返す' do\r
- r = ResourcePicture.mylist @artist, 5, 0\r
- r.should have(5).items \r
+ it 'page=1なら末尾2件を返す' do\r
+ #時系列で並んでいる\r
+ r = ResourcePicture.himlist(@other_artist, 1, 2)\r
+ r.should eq [@rp5, @rp4]\r
+ end\r
+ it 'page=2なら中間2件を返す' do\r
+ r = ResourcePicture.himlist(@other_artist, 2, 2)\r
+ r.should eq [@rp3, @rp2]\r
end\r
+ it 'page=3なら先頭1件を返す' do\r
+ r = ResourcePicture.himlist(@other_artist, 3, 2)\r
+ r.should eq [@other_rp]\r
+ end\r
+ end\r
+ end\r
+ \r
+ describe '他作家の素材一覧ページ制御に於いて' do\r
+ before do\r
+ ResourcePicture.stub(:count).with(any_args).and_return(100)\r
+ end\r
+ it 'ページ制御を返す' do\r
+ r = ResourcePicture.himlist_paginate @artist\r
+ r.is_a?(Kaminari::PaginatableArray).should be_true\r
+ end\r
+ it '自分の素材一覧の取得条件を利用している' do\r
+ ResourcePicture.stub(:himlist_where).with(any_args).and_return('')\r
+ ResourcePicture.should_receive(:himlist_where).with(any_args).exactly(1)\r
+ r = ResourcePicture.himlist_paginate @artist\r
+ end\r
+ it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do\r
+ r = ResourcePicture.himlist_paginate @artist, 3, 10\r
+ r.limit_value.should eq 10\r
+ r.offset_value.should eq 20\r
+ end\r
+ end\r
+\r
+ describe '一覧取得オプションに於いて' do\r
+ it '3つの項目を含んでいる' do\r
+ r = ResourcePicture.list_opt\r
+ r.should have(3).items\r
+ end\r
+ it 'ライセンスを含んでいる' do\r
+ r = ResourcePicture.list_opt\r
+ r.has_key?(:license).should be_true\r
+ end\r
+ it '絵師を含んでいる' do\r
+ r = ResourcePicture.list_opt\r
+ r.has_key?(:artist).should be_true\r
+ end\r
+ it '実素材を含んでいる' do\r
+ r = ResourcePicture.list_opt\r
+ r.has_key?(:picture).should be_true\r
+ end\r
+ end\r
+ describe 'json一覧出力オプションに於いて' do\r
+ before do\r
+ @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
+ @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
+ @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
+ @sbt = FactoryGirl.create :speech_balloon_template\r
+ @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1\r
+ @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1\r
+ @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id\r
+ end\r
+ it 'ライセンスを含んでいる' do\r
+ r = ResourcePicture.list.to_json ResourcePicture.list_json_opt\r
+ j = JSON.parse r\r
+ i = j.first\r
+ i.has_key?('license').should be_true\r
+ end\r
+ it '絵師を含んでいる' do\r
+ r = ResourcePicture.list.to_json ResourcePicture.list_json_opt\r
+ j = JSON.parse r\r
+ i = j.first\r
+ i.has_key?('artist').should be_true\r
+ end\r
+ it '実素材を含んでいる' do\r
+ r = ResourcePicture.list.to_json ResourcePicture.list_json_opt\r
+ j = JSON.parse r\r
+ i = j.first\r
+ i.has_key?('picture').should be_true\r
end\r
end\r
\r