From: yasushiito Date: Wed, 5 Jun 2013 08:38:58 +0000 (+0900) Subject: try paging X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=commitdiff_plain;h=2bcff4ea2e955de6c21e19239313f00ac323f410 try paging --- 2bcff4ea2e955de6c21e19239313f00ac323f410 diff --cc app/controllers/artists_controller.rb index 51543bb1,51543bb1..9db1452e --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@@ -36,13 -36,13 +36,13 @@@ class ArtistsController < ApplicationCo def resource_pictures @ar = Artist.show(params[:id], [@user, @admin, @demand_user]) -- @page = Author.page params[:page] @page_size = Author.resource_picture_page_size params[:page_size] -- @resource_pictures = ResourcePicture.mylist(@ar, @page, @page_size) -- ++ @resource_pictures = ResourcePicture.himlist(@ar, @page, @page_size) respond_to do |format| -- format.html # index.html.erb ++ format.html { ++ @paginate = ResourcePicture.himlist_paginate(@ar, @page, @page_size) ++ } format.json { render json: @resource_pictures.to_json(ResourcePicture.list_json_opt) } end end diff --cc app/models/resource_picture.rb index 333ca88e,0fc8f4ec..30eb7e69 --- a/app/models/resource_picture.rb +++ b/app/models/resource_picture.rb @@@ -108,12 -108,12 +108,36 @@@ class ResourcePicture < ActiveRecord::B 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 @@@ -124,15 -124,14 +148,6 @@@ {: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.where(['resource_pictures.artist_id = ?', ar.id]).includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc').limit(page_size).offset((page -1) * page_size) - Kaminari.paginate_array(ResourcePicture.where(['resource_pictures.artist_id = ?', ar.id]).includes(ResourcePicture.list_opt).order('resource_pictures.updated_at desc')).page(page).per(page_size) - ResourcePicture.find(:all, opt) -- end -- def self.show rid, roles opt = {} opt.merge!(self.show_opt) diff --cc app/views/artists/resource_pictures.html.erb index ead33de3,ead33de3..f3ee0702 --- a/app/views/artists/resource_pictures.html.erb +++ b/app/views/artists/resource_pictures.html.erb @@@ -36,3 -36,3 +36,4 @@@ <% end %> ++<%= paginate(@paginate) %> diff --cc app/views/resource_pictures/index.html.erb index 0cd90009,40b40c29..fbf4491a --- a/app/views/resource_pictures/index.html.erb +++ b/app/views/resource_pictures/index.html.erb @@@ -9,29 -9,8 +9,9 @@@ <%= t_m 'ResourcePicture.height' -%> <%= t_m 'ResourcePicture.filesize' -%> - <% @resource_pictures.each do |resource_picture| %> - - - <%= link_to tag(:img, resource_picture.tmb_opt_img_tag), resource_picture_path(resource_picture) %> - - - <%= render resource_picture.credit_template, :picture => resource_picture %> - - - <%= link_to(h(truncate(resource_picture.artist.name, :length => 8)), artist_path(resource_picture.artist)) %> - - - <%= h resource_picture.ext %> - - - <%= resource_picture.width %>px - - - <%= resource_picture.height %>px - - - <%= resource_picture.filesize %>bytes - - - <% end %> + + <% @resource_pictures.each do |resource_picture| %> + <%= render 'list_item', :resource_picture => resource_picture %> + <% end %> ++<%= paginate(@resource_pictures) %> diff --cc spec/controllers/artists_controller_spec.rb index f36d7542,f36d7542..1409863d --- a/spec/controllers/artists_controller_spec.rb +++ b/spec/controllers/artists_controller_spec.rb @@@ -264,7 -264,7 +264,8 @@@ if MagicNumber['run_mode'] == @op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @other_artist.id @rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id -- ResourcePicture.stub(:mylist).and_return([@rp, @rp, @rp]) ++ Artist.stub(:show).and_return(@artist) ++ ResourcePicture.stub(:himlist).and_return([@rp, @rp, @rp], [nil, nil, nil]) sign_in @user end context 'パラメータpageについて' do @@@ -303,7 -303,7 +304,7 @@@ get :resource_pictures, :id => @other_artist.id end it '素材モデルに一覧を問い合わせている' do -- ResourcePicture.should_receive(:mylist).exactly(1) ++ ResourcePicture.should_receive(:himlist).exactly(1) get :resource_pictures, :id => @other_artist.id end it '@resource_picturesにリストを取得している' do @@@ -311,6 -311,6 +312,10 @@@ assigns(:resource_pictures).should have_at_least(3).items end context 'html形式' do ++ it '@paginateにページ制御を取得している' do ++ get :resource_pictures, :id => @other_artist.id ++ assigns(:paginate).is_a?(Kaminari::PaginatableArray).should be_true ++ end it 'resource_pictureテンプレートを描画する' do get :resource_pictures, :id => @other_artist.id response.should render_template("resource_pictures") diff --cc spec/models/resource_picture_spec.rb index 49880950,49880950..cccf4c47 --- a/spec/models/resource_picture_spec.rb +++ b/spec/models/resource_picture_spec.rb @@@ -472,73 -472,73 +472,20 @@@ describe ResourcePicture d r.should eq [@rp] end end -- context 'DBに5件あって1ページの件数を2件に変えたとして' do -- before do -- nop2 = FactoryGirl.create :original_picture, :artist_id => @artist.id -- @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 -- nop3 = FactoryGirl.create :original_picture, :artist_id => @artist.id -- @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 -- nop4 = FactoryGirl.create :original_picture, :artist_id => @artist.id -- @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 -- nop5 = FactoryGirl.create :original_picture, :artist_id => @artist.id -- @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 -- ResourcePicture.stub(:default_page_size).and_return(2) -- end -- it '件数0は全件(5件)を返す' do -- r = ResourcePicture.list 5, 0 -- r.should have(5).items -- end -- end -- end -- describe '一覧取得オプションに於いて' do -- it 'includeキーを含んでいる' do -- r = ResourcePicture.list_opt -- r.has_key?(:include).should be_true -- end -- it '3つの項目を含んでいる' do -- r = ResourcePicture.list_opt[:include] -- r.should have(3).items -- end -- it 'ライセンスを含んでいる' do -- r = ResourcePicture.list_opt[:include] -- r.has_key?(:license).should be_true -- end -- it '絵師を含んでいる' do -- r = ResourcePicture.list_opt[:include] -- r.has_key?(:artist).should be_true -- end -- it '実素材を含んでいる' do -- r = ResourcePicture.list_opt[:include] -- r.has_key?(:picture).should be_true -- end end -- describe 'json一覧出力オプションに於いて' do ++ ++ describe '一覧ページ制御に於いて' do before do -- @op = FactoryGirl.create :original_picture, :artist_id => @artist.id -- @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id -- @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id -- @sbt = FactoryGirl.create :speech_balloon_template -- @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1 -- @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1 -- @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id ++ ResourcePicture.stub(:count).with(any_args).and_return(100) end -- it 'ライセンスを含んでいる' do -- r = ResourcePicture.list.to_json ResourcePicture.list_json_opt -- j = JSON.parse r -- i = j.first -- i.has_key?('license').should be_true ++ it 'ページ制御を返す' do ++ r = ResourcePicture.list_paginate ++ r.is_a?(Kaminari::PaginatableArray).should be_true end -- it '絵師を含んでいる' do -- r = ResourcePicture.list.to_json ResourcePicture.list_json_opt -- j = JSON.parse r -- i = j.first -- i.has_key?('artist').should be_true -- end -- it '実素材を含んでいる' do -- r = ResourcePicture.list.to_json ResourcePicture.list_json_opt -- j = JSON.parse r -- i = j.first -- i.has_key?('picture').should be_true ++ it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do ++ r = ResourcePicture.list_paginate 3, 10 ++ r.limit_value.should eq 10 ++ r.offset_value.should eq 20 end end @@@ -600,25 -600,25 +547,153 @@@ c.should eq [@rp] end end -- context 'DBに5件あって1ページの件数を0件に変えたとして' do ++ end ++ ++ describe '自分の素材一覧ページ制御に於いて' do ++ before do ++ ResourcePicture.stub(:count).with(any_args).and_return(100) ++ end ++ it 'ページ制御を返す' do ++ r = ResourcePicture.mylist_paginate @artist ++ r.is_a?(Kaminari::PaginatableArray).should be_true ++ end ++ it '自分の素材一覧の取得条件を利用している' do ++ ResourcePicture.stub(:mylist_where).with(any_args).and_return('') ++ ResourcePicture.should_receive(:mylist_where).with(any_args).exactly(1) ++ r = ResourcePicture.mylist_paginate @artist ++ end ++ it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do ++ r = ResourcePicture.mylist_paginate @artist, 3, 10 ++ r.limit_value.should eq 10 ++ r.offset_value.should eq 20 ++ end ++ end ++ ++ describe '他作家の素材一覧取得に於いて' do ++ before do ++ @op = FactoryGirl.create :original_picture, :artist_id => @artist.id ++ @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id ++ @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id ++ @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id ++ @other_p = FactoryGirl.create :picture, :original_picture_id => @other_op.id, :license_id => @license.id, :artist_id => @other_artist.id ++ @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 ++ end ++ context 'つつがなく終わるとき' do ++ it '一覧取得オプションを利用している' do ++ ResourcePicture.stub(:list_opt).with(any_args).and_return({}) ++ ResourcePicture.should_receive(:list_opt).with(any_args).exactly(1) ++ r = ResourcePicture.himlist @other_artist ++ end ++ end ++ it '指定した作家のリストを返す' do ++ r = ResourcePicture.himlist @other_artist ++ r.should eq [@other_rp] ++ end ++ it '時系列で並んでいる' do ++ nop = FactoryGirl.create :original_picture, :artist_id => @other_artist.id ++ 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 = ResourcePicture.himlist @other_artist ++ r.should eq [nrp, @other_rp] ++ end ++ context 'DBに5件あって1ページの件数を2件に変えたとして' do before do -- @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id -- @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 -- @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id -- @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 -- @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id -- @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 -- @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id -- @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 -- Author.stub(:default_resource_picture_page_size).and_return(2) ++ @op2 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id ++ @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 ++ @op3 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id ++ @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 ++ @op4 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id ++ @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 ++ @op5 = FactoryGirl.create :original_picture, :artist_id => @other_artist.id ++ @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 ++ end ++ it '通常は2件を返す' do ++ r = ResourcePicture.himlist @other_artist, 1, 2 ++ r.should have(2).items ++ end ++ it 'page=1なら末尾2件を返す' do ++ #時系列で並んでいる ++ r = ResourcePicture.himlist(@other_artist, 1, 2) ++ r.should eq [@rp5, @rp4] ++ end ++ it 'page=2なら中間2件を返す' do ++ r = ResourcePicture.himlist(@other_artist, 2, 2) ++ r.should eq [@rp3, @rp2] end -- it '通常は全件(5件)を返す' do -- r = ResourcePicture.mylist @artist, 5, 0 -- r.should have(5).items ++ it 'page=3なら先頭1件を返す' do ++ r = ResourcePicture.himlist(@other_artist, 3, 2) ++ r.should eq [@other_rp] end end end ++ describe '他作家の素材一覧ページ制御に於いて' do ++ before do ++ ResourcePicture.stub(:count).with(any_args).and_return(100) ++ end ++ it 'ページ制御を返す' do ++ r = ResourcePicture.himlist_paginate @artist ++ r.is_a?(Kaminari::PaginatableArray).should be_true ++ end ++ it '自分の素材一覧の取得条件を利用している' do ++ ResourcePicture.stub(:himlist_where).with(any_args).and_return('') ++ ResourcePicture.should_receive(:himlist_where).with(any_args).exactly(1) ++ r = ResourcePicture.himlist_paginate @artist ++ end ++ it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do ++ r = ResourcePicture.himlist_paginate @artist, 3, 10 ++ r.limit_value.should eq 10 ++ r.offset_value.should eq 20 ++ end ++ end ++ ++ describe '一覧取得オプションに於いて' do ++ it '3つの項目を含んでいる' do ++ r = ResourcePicture.list_opt ++ r.should have(3).items ++ end ++ it 'ライセンスを含んでいる' do ++ r = ResourcePicture.list_opt ++ r.has_key?(:license).should be_true ++ end ++ it '絵師を含んでいる' do ++ r = ResourcePicture.list_opt ++ r.has_key?(:artist).should be_true ++ end ++ it '実素材を含んでいる' do ++ r = ResourcePicture.list_opt ++ r.has_key?(:picture).should be_true ++ end ++ end ++ describe 'json一覧出力オプションに於いて' do ++ before do ++ @op = FactoryGirl.create :original_picture, :artist_id => @artist.id ++ @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id ++ @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id ++ @sbt = FactoryGirl.create :speech_balloon_template ++ @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1 ++ @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1 ++ @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id ++ end ++ it 'ライセンスを含んでいる' do ++ r = ResourcePicture.list.to_json ResourcePicture.list_json_opt ++ j = JSON.parse r ++ i = j.first ++ i.has_key?('license').should be_true ++ end ++ it '絵師を含んでいる' do ++ r = ResourcePicture.list.to_json ResourcePicture.list_json_opt ++ j = JSON.parse r ++ i = j.first ++ i.has_key?('artist').should be_true ++ end ++ it '実素材を含んでいる' do ++ r = ResourcePicture.list.to_json ResourcePicture.list_json_opt ++ j = JSON.parse r ++ i = j.first ++ i.has_key?('picture').should be_true ++ end ++ end ++ describe '単体取得に於いて' do before do @op = FactoryGirl.create :original_picture, :artist_id => @artist.id