OSDN Git Service

try paging
authoryasushiito <yas@pen-chan.jp>
Wed, 5 Jun 2013 08:38:58 +0000 (17:38 +0900)
committeryasushiito <yas@pen-chan.jp>
Wed, 5 Jun 2013 08:38:58 +0000 (17:38 +0900)
1  2 
app/controllers/artists_controller.rb
app/models/author.rb
app/models/resource_picture.rb
app/views/artists/resource_pictures.html.erb
app/views/resource_pictures/index.html.erb
spec/controllers/artists_controller_spec.rb
spec/models/resource_picture_spec.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
Simple merge
@@@ -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
      {: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)
@@@ -9,29 -9,8 +9,9 @@@
      <th><%= t_m 'ResourcePicture.height' -%></th>
      <th><%= t_m 'ResourcePicture.filesize' -%></th>
    </tr>
- <% @resource_pictures.each do |resource_picture| %>
-   <tr>
-     <td>
-       <%= link_to tag(:img, resource_picture.tmb_opt_img_tag), resource_picture_path(resource_picture) %>
-     </td>
-     <td>
-       <%= render resource_picture.credit_template, :picture => resource_picture %>
-     </td>
-     <td>
-       <%= link_to(h(truncate(resource_picture.artist.name, :length => 8)), artist_path(resource_picture.artist)) %>
-     </td>
-     <td>
-       <%= h resource_picture.ext %>
-     </td>
-     <td>
-       <%= resource_picture.width %>px
-     </td>
-     <td>
-       <%= resource_picture.height %>px
-     </td>
-     <td>
-       <%= resource_picture.filesize %>bytes
-     </td>
-   </tr>
- <% end %>
+   <% @resource_pictures.each do |resource_picture| %>
+     <%= render 'list_item', :resource_picture => resource_picture %>
+   <% end %>
  </table>
++<%= paginate(@resource_pictures) %>
@@@ -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
          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
          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")
@@@ -472,73 -472,73 +472,20 @@@ describe ResourcePicture d
          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 '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 '通常は全件(5件)を返す' do\r
--        r = ResourcePicture.mylist @artist, 5, 0\r
--        r.should have(5).items \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
    describe '単体取得に於いて' do\r
      before do\r
        @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r