From 40e107e42d022307e41835506405d7b04d8c7277 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Mon, 24 Dec 2012 13:45:02 +0900 Subject: [PATCH] t30354#:create mylist for rp --- app/controllers/application_controller.rb | 2 +- app/controllers/home_controller.rb | 17 ++- app/controllers/resource_pictures_controller.rb | 2 +- app/models/author.rb | 15 +++ app/models/resource_picture.rb | 8 ++ app/views/home/index.html.erb | 2 +- app/views/home/resource_picture.html.erb | 38 +++++++ config/locales/pettanr.ja.yml | 4 +- spec/controllers/home_controller_spec.rb | 124 +++++++++++++++++++++ .../resource_pictures_controller_spec.rb | 12 +- spec/models/author_spec.rb | 14 +++ spec/models/original_picture_spec.rb | 9 +- spec/models/resource_picture_spec.rb | 77 +++++++++++++ 13 files changed, 299 insertions(+), 25 deletions(-) create mode 100644 app/views/home/resource_picture.html.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5b32d2ee..295a4ec3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -67,7 +67,7 @@ class ApplicationController < ActionController::Base end def authenticate_artist - if @author.artist? + if @author and @author.artist? true else respond_to do |format| diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 9f5c0c1b..2a4c4121 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -2,13 +2,13 @@ class HomeController < ApplicationController layout 'test' if MagicNumber['test_layout'] before_filter :authenticate_user!, :only => [ :index, :show, :profile, :configure, :create_token, :delete_token, - :comic, :story, :panel, :picture, :panel_picture, :panel_color, :ground_picture, :ground_color + :comic, :story, :panel, :resource_picture, :panel_picture, :panel_color, :ground_picture, :ground_color ] before_filter :authenticate_author, :only => [ :index, :show, :profile, :configure, :create_token, :delete_token, - :comic, :story, :panel, :picture, :panel_picture, :panel_color, :ground_picture, :ground_color + :comic, :story, :panel, :resource_picture, :panel_picture, :panel_color, :ground_picture, :ground_color ] - before_filter :authenticate_artist, :only => [:picture] + before_filter :authenticate_artist, :only => [:resource_picture] def index end @@ -120,4 +120,15 @@ class HomeController < ApplicationController end end + def resource_picture + @page = Author.page params[:page] + @page_size = Author.panel_page_size params[:page_size] + @resource_pictures = ResourcePicture.mylist(@author, @page, @page_size) + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @resource_pictures.to_json(ResourcePicture.list_json_opt) } + end + end + end diff --git a/app/controllers/resource_pictures_controller.rb b/app/controllers/resource_pictures_controller.rb index 3fd04cb7..4b8f09e5 100644 --- a/app/controllers/resource_pictures_controller.rb +++ b/app/controllers/resource_pictures_controller.rb @@ -92,7 +92,7 @@ class ResourcePicturesController < ApplicationController respond_to do |format| if @resource_picture.unpublish - format.html { redirect_to resource_pictures_path } + format.html { redirect_to :controller => '/home', :action => :resource_picture } format.json { head :ok } else format.html { redirect_to resource_picture_path(@resource_picture) } diff --git a/app/models/author.rb b/app/models/author.rb index d574bae9..6de0ac87 100644 --- a/app/models/author.rb +++ b/app/models/author.rb @@ -206,6 +206,21 @@ class Author < ActiveRecord::Base page_size end + def self.default_resource_picture_page_size + 25 + end + + def self.resource_picture_max_page_size + 100 + end + + def self.resource_picture_page_size prm = self.default_resource_picture_page_size + page_size = prm.to_i + page_size = self.resource_picture_max_page_size if page_size > self.resource_picture_max_page_size + page_size = self.default_resource_picture_page_size if page_size < 1 + page_size + end + def self.visible_count Author.count end diff --git a/app/models/resource_picture.rb b/app/models/resource_picture.rb index 757140a3..68e22c09 100644 --- a/app/models/resource_picture.rb +++ b/app/models/resource_picture.rb @@ -115,6 +115,14 @@ class ResourcePicture < ActiveRecord::Base {: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, au opt = {} opt.merge!(self.show_opt) diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 749dcc7c..a55e5f0d 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -43,7 +43,7 @@ <%= link_to t_m('OriginalPicture'), main_app.original_pictures_path %> - <%= %> + <%= link_to t_m('ResourcePicture'), '/home/resource_picture' %> <%= %> diff --git a/app/views/home/resource_picture.html.erb b/app/views/home/resource_picture.html.erb new file mode 100644 index 00000000..ead33de3 --- /dev/null +++ b/app/views/home/resource_picture.html.erb @@ -0,0 +1,38 @@ +

<%= t '.title' -%>

+ + + + + + + + + + + +<% @resource_pictures.each do |resource_picture| %> + + + + + + + + + +<% end %> +
<%= t_m 'ResourcePicture.artist_id' -%><%= t_m 'ResourcePicture.ext' -%><%= t_m 'ResourcePicture.width' -%><%= t_m 'ResourcePicture.height' -%><%= t_m 'ResourcePicture.filesize' -%>
+ <%= 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 +
diff --git a/config/locales/pettanr.ja.yml b/config/locales/pettanr.ja.yml index 9d3ade94..16127691 100644 --- a/config/locales/pettanr.ja.yml +++ b/config/locales/pettanr.ja.yml @@ -398,8 +398,8 @@ ja: title: 最近更新したストーリー panel: title: 最近更新したコマ - picture: - title: 最近更新した原画 + resource_picture: + title: 最近更新した素材 panel_picture: title: 最近使ったコマ絵 ground_picture: diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index 0f7f3e7d..b4536e18 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -626,4 +626,128 @@ describe HomeController do 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 + sign_in @user + ResourcePicture.stub(:mylist).and_return([@rp, @rp, @rp]) + end + context 'パラメータpageについて' do + it '@pageに値が入る' do + get :resource_picture, :page => 5 + assigns(:page).should eq 5 + end + it '省略されると@pageに1値が入る' do + get :resource_picture + assigns(:page).should eq 1 + end + it '与えられたpage_sizeがセットされている' do + get :resource_picture, :page_size => 15 + assigns(:page_size).should eq 15 + end + it '省略されると@page_sizeにデフォルト値が入る' do + get :resource_picture + assigns(:page_size).should eq Author.default_comic_page_size + end + it '最大を超えると@page_sizeにデフォルト最大値が入る' do + get :resource_picture, :page_size => 1500 + assigns(:page_size).should eq Author.comic_max_page_size + end + it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do + get :resource_picture, :page_size => 0 + assigns(:page_size).should eq Author.default_comic_page_size + end + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :resource_picture + response.should be_success + end + it '素材モデルに一覧を問い合わせている' do + ResourcePicture.should_receive(:mylist).exactly(1) + get :resource_picture + end + it '@resource_picturesにリストを取得している' do + get :resource_picture + assigns(:resource_pictures).should have_at_least(3).items + end + context 'html形式' do + it 'resource_pictureテンプレートを描画する' do + get :resource_picture + response.should render_template("resource_picture") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :resource_picture, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it '素材モデルにjson一覧出力オプションを問い合わせている' do + ResourcePicture.should_receive(:list_json_opt).exactly(1) + get :resource_picture, :format => :json + end + it 'データがリスト構造になっている' do + get :resource_picture, :format => :json + json = JSON.parse response.body + json.should have_at_least(3).items + end + it 'リストの先頭くらいは素材っぽいものであって欲しい' do + get :resource_picture, :format => :json + json = JSON.parse response.body + json.first.has_key?("original_picture_id").should be_true + json.first.has_key?("license_id").should be_true + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :resource_picture + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :resource_picture + response.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :resource_picture, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :resource_picture, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + context '作家が絵師でないとき' do + before do + Author.any_instance.stub(:artist?).and_return(false) + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :resource_picture + response.status.should eq 302 + end + it '絵師登録ページへ遷移する' do + get :resource_picture + response.should redirect_to new_artist_path + end + end + context 'json形式' do + it '例外403 forbiddenを返す' do + lambda{ + get :resource_picture, :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end + end + end + end diff --git a/spec/controllers/resource_pictures_controller_spec.rb b/spec/controllers/resource_pictures_controller_spec.rb index 09a0e257..ea53ef71 100644 --- a/spec/controllers/resource_pictures_controller_spec.rb +++ b/spec/controllers/resource_pictures_controller_spec.rb @@ -899,15 +899,10 @@ describe ResourcePicturesController do end end context 'つつがなく終わるとき' do - it '削除される' do - lambda { - delete :destroy, :id => @rp.id - }.should change ResourcePicture, :count + before do + ResourcePicture.any_instance.stub(:unpublish).with(any_args()).and_return(true) end context 'html形式' do - before do - ResourcePicture.any_instance.stub(:unpublish).with(any_args()).and_return(true) - end it 'ステータスコード302 Foundを返す' do delete :destroy, :id => @rp.id response.status.should eq 302 @@ -918,9 +913,6 @@ describe ResourcePicturesController do end end context 'json形式' do - before do - ResourcePicture.any_instance.stub(:unpublish).with(any_args()).and_return(true) - end it 'ステータスコード200 OKを返す' do delete :destroy, :id => @rp.id, :format => :json response.should be_success diff --git a/spec/models/author_spec.rb b/spec/models/author_spec.rb index dfc036ec..f8399194 100644 --- a/spec/models/author_spec.rb +++ b/spec/models/author_spec.rb @@ -452,5 +452,19 @@ describe Author do Author.ground_color_page_size('1000').should eq Author.ground_color_max_page_size end end + context '素材page_size補正について' do + it '文字列から数値に変換される' do + Author.resource_picture_page_size('7').should eq 7 + end + it 'nilの場合はAuthor.default_resource_picture_page_sizeになる' do + Author.resource_picture_page_size().should eq Author.default_resource_picture_page_size + end + it '0以下の場合はAuthor.default_resource_picture_page_sizeになる' do + Author.resource_picture_page_size('0').should eq Author.default_resource_picture_page_size + end + it 'Author.resource_picture_max_page_sizeを超えた場合はAuthor.resource_picture_max_page_sizeになる' do + Author.resource_picture_page_size('1000').should eq Author.resource_picture_max_page_size + end + end end end diff --git a/spec/models/original_picture_spec.rb b/spec/models/original_picture_spec.rb index 2a9a964c..ae736ba9 100644 --- a/spec/models/original_picture_spec.rb +++ b/spec/models/original_picture_spec.rb @@ -481,7 +481,7 @@ describe OriginalPicture do end end - describe '自分のコミック一覧取得に於いて' do + describe '自分の原画一覧取得に於いて' do before do @op = FactoryGirl.create :original_picture, :artist_id => @artist.id end @@ -501,16 +501,11 @@ describe OriginalPicture do r = OriginalPicture.mylist @artist r.should eq [nc, @op] end - it '他人のコミックはxxxでも含まない' do + it '他人の原画は含まない' do nc = FactoryGirl.create :original_picture, :artist_id => @other_artist.id r = OriginalPicture.mylist @artist r.should eq [@op] end - it '自分のコミックはxxxでも含んでいる' do - nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100 - r = OriginalPicture.mylist @artist - r.should eq [nc, @op] - end context 'DBに5件あって1ページの件数を2件に変えたとして' do before do @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100 diff --git a/spec/models/resource_picture_spec.rb b/spec/models/resource_picture_spec.rb index 2dd12d77..7b8be1a1 100644 --- a/spec/models/resource_picture_spec.rb +++ b/spec/models/resource_picture_spec.rb @@ -465,6 +465,83 @@ describe ResourcePicture do 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 + 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.mylist @artist + end + end + it 'リストを返す' do + c = ResourcePicture.mylist @artist + c.should eq [@rp] + end + it '時系列で並んでいる' do + nop = FactoryGirl.create :original_picture, :artist_id => @artist.id + nrp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => nop.id, :picture_id => @p.id, :updated_at => Time.now + 100 + cl = ResourcePicture.mylist @artist + cl.should eq [nrp, @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 => @p.id, :updated_at => Time.now + 100 + cl = ResourcePicture.mylist @artist + cl.should eq [@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 + end + it '通常は2件を返す' do + c = ResourcePicture.mylist @artist, 1, 2 + c.should have(2).items + end + it 'page=1なら末尾2件を返す' do + #時系列で並んでいる + c = ResourcePicture.mylist(@artist, 1, 2) + c.should eq [@rp5, @rp4] + end + it 'page=2なら中間2件を返す' do + c = ResourcePicture.mylist(@artist, 2, 2) + c.should eq [@rp3, @rp2] + end + it 'page=3なら先頭1件を返す' do + c = ResourcePicture.mylist(@artist, 3, 2) + c.should eq [@rp] + end + end + context 'DBに5件あって1ページの件数を0件に変えたとして' 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) + end + it '通常は全件(5件)を返す' do + r = ResourcePicture.mylist @artist, 5, 0 + r.should have(5).items + end + end + end + describe '単体取得に於いて' do before do @op = FactoryGirl.create :original_picture, :artist_id => @artist.id -- 2.11.0