From 5ab69fa3ec6f582bd30a794589f60136141701ea Mon Sep 17 00:00:00 2001 From: yasushiito Date: Sat, 21 Jul 2012 12:21:24 +0900 Subject: [PATCH] t#28985:add credit --- app/controllers/pictures_controller.rb | 18 +- app/controllers/resource_pictures_controller.rb | 4 +- app/models/picture.rb | 6 + app/views/pictures/credit.html.erb | 2 +- app/views/resource_pictures/credit.html.erb | 2 +- app/views/resource_pictures/index.html.erb | 2 +- app/views/resource_pictures/show.html.erb | 2 +- app/views/standard_licenses/_credit.html.erb | 6 +- config/routes.rb | 10 + spec/controllers/pictures_controller_spec.rb | 232 +++++++++++++++++++++ .../resource_pictures_controller_spec.rb | 85 ++++++++ 11 files changed, 358 insertions(+), 11 deletions(-) diff --git a/app/controllers/pictures_controller.rb b/app/controllers/pictures_controller.rb index 5aea2479..4661dbfd 100644 --- a/app/controllers/pictures_controller.rb +++ b/app/controllers/pictures_controller.rb @@ -1,11 +1,25 @@ class PicturesController < ApplicationController layout 'test' if Pettanr::TestLayout + before_filter :authenticate_user!, :only => [:show, :credit] + + def show + @picture = Picture.show(params[:id]) + + respond_to do |format| + opt = {:type => @picture.mime_type, :disposition=>"inline"} + format.png { send_data(@picture.restore(params[:subdir]), opt ) } + format.gif { send_data(@picture.restore(params[:subdir]), opt ) } + format.jpeg { send_data(@picture.restore(params[:subdir]), opt ) } + format.json { render :json => @picture.to_json} + end + end + def credit @picture = Picture.show(params[:id]) respond_to do |format| - format.html # show.html.erb - format.json { render :json => @picture.to_json(Picture.list_json_opt)} + format.html { render :layout => false } # show.html.erb + format.json { render :json => @picture.to_json} end end diff --git a/app/controllers/resource_pictures_controller.rb b/app/controllers/resource_pictures_controller.rb index 55184fd4..b630cdd2 100644 --- a/app/controllers/resource_pictures_controller.rb +++ b/app/controllers/resource_pictures_controller.rb @@ -1,6 +1,6 @@ class ResourcePicturesController < ApplicationController layout 'test' if Pettanr::TestLayout - before_filter :authenticate_user!, :only => [:index, :show, :new, :create, :update] + before_filter :authenticate_user!, :only => [:index, :show, :credit, :new, :create, :update] before_filter :authenticate_admin!, :only => [:list, :browse] # GET /resource_pictures @@ -35,7 +35,7 @@ class ResourcePicturesController < ApplicationController @resource_picture = ResourcePicture.show(params[:id]) respond_to do |format| - format.html # show.html.erb + format.html { render :layout => false } # show.html.erb format.json { render :json => @resource_picture.to_json(ResourcePicture.list_json_opt)} end end diff --git a/app/models/picture.rb b/app/models/picture.rb index ed212f45..e5bbda4b 100644 --- a/app/models/picture.rb +++ b/app/models/picture.rb @@ -103,6 +103,12 @@ class Picture < ActiveRecord::Base PictureIO.resource_picture_io.get self.filename, subdir end + def self.show pid + r = Picture.find(pid) +# raise ActiveRecord::Forbidden unless c.visible?(au) + r + end + def credit_template "#{self.classname.tableize}/credit" end diff --git a/app/views/pictures/credit.html.erb b/app/views/pictures/credit.html.erb index 558fc615..29af791c 100644 --- a/app/views/pictures/credit.html.erb +++ b/app/views/pictures/credit.html.erb @@ -1 +1 @@ -<%= render @picture.credit_template %> +<%= render @picture.credit_template, :picture => @picture %> diff --git a/app/views/resource_pictures/credit.html.erb b/app/views/resource_pictures/credit.html.erb index 0d5f816c..cbd51fd4 100644 --- a/app/views/resource_pictures/credit.html.erb +++ b/app/views/resource_pictures/credit.html.erb @@ -1 +1 @@ -<%= render @resource_picture.credit_template %> +<%= render @resource_picture.credit_template, :picture => @resource_picture %> diff --git a/app/views/resource_pictures/index.html.erb b/app/views/resource_pictures/index.html.erb index 859041c1..daef41ee 100644 --- a/app/views/resource_pictures/index.html.erb +++ b/app/views/resource_pictures/index.html.erb @@ -6,6 +6,6 @@ <%= resource_picture.width %>x<%= resource_picture.height %> <%= resource_picture.filesize %>bytes - <%= render resource_picture.credit_template %> + <%= render resource_picture.credit_template, :picture => resource_picture %> <% end %> diff --git a/app/views/resource_pictures/show.html.erb b/app/views/resource_pictures/show.html.erb index 6e109307..0839b2dd 100644 --- a/app/views/resource_pictures/show.html.erb +++ b/app/views/resource_pictures/show.html.erb @@ -1,5 +1,5 @@

<%= notice %>

-<%= render @resource_picture.classname.tableize + '/credit' %> +<%= render @resource_picture.credit_template, :picture => @resource_picture %>

ノーマル diff --git a/app/views/standard_licenses/_credit.html.erb b/app/views/standard_licenses/_credit.html.erb index 1b5316fc..cfa175fd 100644 --- a/app/views/standard_licenses/_credit.html.erb +++ b/app/views/standard_licenses/_credit.html.erb @@ -1,7 +1,7 @@

<%= content_tag(:a, - tag(:img, :src => @resource_picture.license.system_picture.url, :alt => h(@resource_picture.license.license_group.caption.to_s + '[' + @resource_picture.license.caption.to_s + ']')), - :href => @resource_picture.license.url ) + tag(:img, :src => picture.license.system_picture.url, :alt => h(picture.license.license_group.caption.to_s + '[' + picture.license.caption.to_s + ']')), + :href => picture.license.url ) %>
-著作者:<%= link_to h(@resource_picture.artist_name), :controller => 'artists', :action => :show, :id => @resource_picture.artist.id %> +著作者:<%= link_to h(picture.artist_name), :controller => 'artists', :action => :show, :id => picture.artist.id %> diff --git a/config/routes.rb b/config/routes.rb index 43d500c1..e2e60d66 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -76,6 +76,14 @@ Pettanr::Application.routes.draw do member do end end + resources :pictures do + collection do + get :show + end + member do + get :credit + end + end resources :resource_pictures do collection do get :index @@ -87,6 +95,7 @@ Pettanr::Application.routes.draw do get :browse end member do + get :credit end end resources :original_pictures do @@ -237,6 +246,7 @@ Pettanr::Application.routes.draw do # match 'products/:id' => 'catalog#view' # Keep in mind you can assign values other than :controller and :action match 'resource_pictures/:subdir/:id(.:format)/' => 'resource_pictures#show' + match 'pictures/:subdir/:id(.:format)/' => 'pictures#show' match 'home/(:action)', :controller => 'home' match 'help/(:action)', :controller => 'help' match 'system/(:action)', :controller => 'system' diff --git a/spec/controllers/pictures_controller_spec.rb b/spec/controllers/pictures_controller_spec.rb index d0335daf..b98e86e5 100644 --- a/spec/controllers/pictures_controller_spec.rb +++ b/spec/controllers/pictures_controller_spec.rb @@ -1,5 +1,237 @@ +# -*- encoding: utf-8 -*- +#実素材 require 'spec_helper' describe PicturesController do + before do + Factory :admin + @sp = Factory :system_picture + @lg = Factory :license_group + @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id + @user = Factory( :user_yas) + @author = @user.author + @artist = Factory :artist_yas, :author_id => @author.id + @op = Factory :original_picture, :artist_id => @artist.id + end + + describe '単体表示に於いて' do + before do + @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + sign_in @user + Picture.stub(:show).and_return(@p) + end + context 'つつがなく終わるとき as json' do + it 'ステータスコード200 OKを返す' do + get :show, :id => @p.id, :format => :json + response.should be_success + end + it '実素材モデルに単体取得を問い合わせている' do + Picture.should_receive(:show).exactly(1) + get :show, :id => @p.id, :format => :json + end + it '@pictureにアレを取得している' do + get :show, :id => @p.id, :format => :json + assigns(:picture).id.should eq(@p.id) + end + context 'json形式' do + it 'jsonデータを返す' do + get :show, :id => @p.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがアレになっている' do + get :show, :id => @p.id, :format => :json + json = JSON.parse response.body + json["ext"].should match(/png/) + end + end + #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると + #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に + #スタブをおいてsend_dataがデータを返す体裁でテストする。 + context 'png形式' do + it '画像モデルに画像データを問い合わせる' do + Picture.any_instance.should_receive(:restore).exactly(1) + get :show, :id => @p.id, :format => :png + end + it '画像を送信する' do + Picture.any_instance.stub(:restore).and_return('aaa') + get :show, :id => @p.id, :format => :png + response.body.should eq 'aaa' + end + end + context 'gif形式' do + it '画像モデルに画像データを問い合わせる' do + Picture.any_instance.should_receive(:restore).exactly(1) + get :show, :id => @p.id, :format => :gif + end + it '画像を送信する' do + Picture.any_instance.stub(:restore).and_return('bbb') + get :show, :id => @p.id, :format => :gif + response.body.should eq 'bbb' + end + end + context 'jpeg形式' do + it '画像モデルに画像データを問い合わせる' do + Picture.any_instance.should_receive(:restore).exactly(1) + get :show, :id => @p.id, :format => :jpeg + end + it '画像を送信する' do + Picture.any_instance.stub(:restore).and_return('ccc') + get :show, :id => @p.id, :format => :jpeg + response.body.should eq 'ccc' + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :show, :id => @p.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :show, :id => @p.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :show, :id => @p.id, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :show, :id => @p.id, :format => :json + response.message.should match(/Unauthorized/) + end + end + end +=begin + context '対象素材がないとき' do + before do + Picture.unstub(:show) + end + context 'html形式' do + it '例外404 not_foundを返す' do + lambda{ + get :show, :id => 0 + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + context 'json形式' do + it '例外404 not_foundを返す' do + lambda{ + get :show, :id => 0, :format => :json + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + end + context '他人の素材を見ようとしたとき' do + before do + Picture.stub(:show).and_return(@p) + Picture.any_instance.stub(:own?).with(any_args()).and_return(false) + end + context 'html形式' do + it '例外403 forbiddenを返す' do + lambda{ + get :show, :id => @p.id + }.should raise_error(ActiveRecord::Forbidden) + end + end + context 'json形式' do + it '例外403 forbiddenを返す' do + lambda{ + get :show, :id => @p.id, :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end + end +=end + end + + describe 'クレジット表示に於いて' do + before do + @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + sign_in @user + Picture.stub(:show).and_return(@p) + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :credit, :id => @p.id + response.should be_success + end + it '実素材モデルに単体取得を問い合わせている' do + Picture.should_receive(:show).exactly(1) + get :credit, :id => @p.id + end + it '@pictureにアレを取得している' do + get :credit, :id => @p.id + assigns(:picture).id.should eq(@p.id) + end + context 'html形式' do + it 'creditテンプレートを描画する' do + get :credit, :id => @p.id + response.should render_template("credit") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :credit, :id => @p.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがアレになっている' do + get :credit, :id => @p.id, :format => :json + json = JSON.parse response.body + json["ext"].should match(/png/) + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :credit, :id => @p.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :credit, :id => @p.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :credit, :id => @p.id, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :credit, :id => @p.id, :format => :json + response.message.should match(/Unauthorized/) + end + end + end +=begin + context '対象素材がないとき' do + before do + Picture.unstub(:show) + end + context 'html形式' do + it '例外404 not_foundを返す' do + lambda{ + get :show, :id => 0 + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + context 'json形式' do + it '例外404 not_foundを返す' do + lambda{ + get :show, :id => 0, :format => :json + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + end +=end + end end diff --git a/spec/controllers/resource_pictures_controller_spec.rb b/spec/controllers/resource_pictures_controller_spec.rb index f5f7547f..fa423893 100644 --- a/spec/controllers/resource_pictures_controller_spec.rb +++ b/spec/controllers/resource_pictures_controller_spec.rb @@ -276,4 +276,89 @@ describe ResourcePicturesController do end end + describe 'クレジット表示に於いて' do + before do + @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id + @pic = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id + sign_in @user + ResourcePicture.stub(:show).and_return(@pic) + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :credit, :id => @pic.id + response.should be_success + end + it '素材モデルに単体取得を問い合わせている' do + ResourcePicture.should_receive(:show).exactly(1) + get :credit, :id => @pic.id + end + it '@resource_pictureにアレを取得している' do + get :credit, :id => @pic.id + assigns(:resource_picture).id.should eq(@pic.id) + end + context 'html形式' do + it 'creditテンプレートを描画する' do + get :credit, :id => @pic.id + response.should render_template("credit") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :credit, :id => @pic.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがアレになっている' do + get :credit, :id => @pic.id, :format => :json + json = JSON.parse response.body + json["ext"].should match(/png/) + end + end + end + context '作家権限がないとき' do + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :credit, :id => @pic.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :credit, :id => @pic.id + response.body.should redirect_to '/users/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :credit, :id => @pic.id, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :credit, :id => @pic.id, :format => :json + response.message.should match(/Unauthorized/) + end + end + end +=begin + context '対象素材がないとき' do + before do + ResourcePicture.unstub(:show) + end + context 'html形式' do + it '例外404 not_foundを返す' do + lambda{ + get :show, :id => 0 + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + context 'json形式' do + it '例外404 not_foundを返す' do + lambda{ + get :show, :id => 0, :format => :json + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + end +=end + end end -- 2.11.0