OSDN Git Service

t#28985:add credit
authoryasushiito <yas@pen-chan.jp>
Sat, 21 Jul 2012 03:21:24 +0000 (12:21 +0900)
committeryasushiito <yas@pen-chan.jp>
Sat, 21 Jul 2012 03:21:24 +0000 (12:21 +0900)
app/controllers/pictures_controller.rb
app/controllers/resource_pictures_controller.rb
app/models/picture.rb
app/views/pictures/credit.html.erb
app/views/resource_pictures/credit.html.erb
app/views/resource_pictures/index.html.erb
app/views/resource_pictures/show.html.erb
app/views/standard_licenses/_credit.html.erb
config/routes.rb
spec/controllers/pictures_controller_spec.rb
spec/controllers/resource_pictures_controller_spec.rb

index 5aea247..4661dbf 100644 (file)
@@ -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
   
index 55184fd..b630cdd 100644 (file)
@@ -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
index ed212f4..e5bbda4 100644 (file)
@@ -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
index 558fc61..29af791 100644 (file)
@@ -1 +1 @@
-<%= render @picture.credit_template %>
+<%= render @picture.credit_template, :picture => @picture %>
index 0d5f816..cbd51fd 100644 (file)
@@ -1 +1 @@
-<%= render @resource_picture.credit_template %>
+<%= render @resource_picture.credit_template, :picture => @resource_picture %>
index 859041c..daef41e 100644 (file)
@@ -6,6 +6,6 @@
     <img src="<%= resource_picture.url -%>">
     <%= resource_picture.width %>x<%= resource_picture.height %>
     <%= resource_picture.filesize %>bytes
-    <%= render resource_picture.credit_template %>
+    <%= render resource_picture.credit_template, :picture => resource_picture %>
   </div>
 <% end %>
index 6e10930..0839b2d 100644 (file)
@@ -1,5 +1,5 @@
 <p id="notice"><%= notice %></p>
-<%= render @resource_picture.classname.tableize + '/credit' %>
+<%= render @resource_picture.credit_template, :picture => @resource_picture %>
 <p>
   <a href="<%= @resource_picture.url %>">ノーマル</a> 
   <img src="<%= @resource_picture.url -%>">
index 1b5316f..cfa175f 100644 (file)
@@ -1,7 +1,7 @@
 <div>
 <%= 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 )
 %>
 </div>
-著作者:<%= 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 %>
index 43d500c..e2e60d6 100644 (file)
@@ -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'
index d0335da..b98e86e 100644 (file)
@@ -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\r
+    before do\r
+      @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\r
+    context 'つつがなく終わるとき' do\r
+      it 'ステータスコード200 OKを返す' do\r
+        get :credit, :id => @p.id\r
+        response.should be_success\r
+      end\r
+      it '実素材モデルに単体取得を問い合わせている' do\r
+        Picture.should_receive(:show).exactly(1)\r
+        get :credit, :id => @p.id\r
+      end\r
+      it '@pictureにアレを取得している' do\r
+        get :credit, :id => @p.id\r
+        assigns(:picture).id.should eq(@p.id)\r
+      end\r
+      context 'html形式' do\r
+        it 'creditテンプレートを描画する' do\r
+          get :credit, :id => @p.id\r
+          response.should render_template("credit")\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'jsonデータを返す' do\r
+          get :credit, :id => @p.id, :format => :json\r
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
+        end\r
+        it 'データがアレになっている' do\r
+          get :credit, :id => @p.id, :format => :json\r
+          json = JSON.parse response.body\r
+          json["ext"].should match(/png/)\r
+        end\r
+      end\r
+    end\r
+    context '作家権限がないとき' do\r
+      before do\r
+        sign_out @user\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          get :credit, :id => @p.id\r
+          response.status.should eq 302\r
+        end\r
+        it 'サインインページへ遷移する' do\r
+          get :credit, :id => @p.id\r
+          response.body.should redirect_to '/users/sign_in'\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'ステータスコード401 Unauthorizedを返す' do\r
+          get :credit, :id => @p.id, :format => :json\r
+          response.status.should eq 401\r
+        end\r
+        it '応答メッセージにUnauthorizedを返す' do\r
+          get :credit, :id => @p.id, :format => :json\r
+          response.message.should match(/Unauthorized/)\r
+        end\r
+      end\r
+    end\r
+=begin\r
+    context '対象素材がないとき' do\r
+      before do\r
+        Picture.unstub(:show)\r
+      end\r
+      context 'html形式' do\r
+        it '例外404 not_foundを返す' do\r
+          lambda{\r
+            get :show, :id => 0\r
+          }.should raise_error(ActiveRecord::RecordNotFound)\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it '例外404 not_foundを返す' do\r
+          lambda{ \r
+            get :show, :id => 0, :format => :json\r
+          }.should raise_error(ActiveRecord::RecordNotFound)\r
+        end\r
+      end\r
+    end\r
+=end\r
+  end\r
 
 end
index f5f7547..fa42389 100644 (file)
@@ -276,4 +276,89 @@ describe ResourcePicturesController do
     end
   end
 
+  describe 'クレジット表示に於いて' do\r
+    before do\r
+      @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\r
+    context 'つつがなく終わるとき' do\r
+      it 'ステータスコード200 OKを返す' do\r
+        get :credit, :id => @pic.id\r
+        response.should be_success\r
+      end\r
+      it '素材モデルに単体取得を問い合わせている' do\r
+        ResourcePicture.should_receive(:show).exactly(1)\r
+        get :credit, :id => @pic.id\r
+      end\r
+      it '@resource_pictureにアレを取得している' do\r
+        get :credit, :id => @pic.id\r
+        assigns(:resource_picture).id.should eq(@pic.id)\r
+      end\r
+      context 'html形式' do\r
+        it 'creditテンプレートを描画する' do\r
+          get :credit, :id => @pic.id\r
+          response.should render_template("credit")\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'jsonデータを返す' do\r
+          get :credit, :id => @pic.id, :format => :json\r
+          lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)\r
+        end\r
+        it 'データがアレになっている' do\r
+          get :credit, :id => @pic.id, :format => :json\r
+          json = JSON.parse response.body\r
+          json["ext"].should match(/png/)\r
+        end\r
+      end\r
+    end\r
+    context '作家権限がないとき' do\r
+      before do\r
+        sign_out @user\r
+      end\r
+      context 'html形式' do\r
+        it 'ステータスコード302 Foundを返す' do\r
+          get :credit, :id => @pic.id\r
+          response.status.should eq 302\r
+        end\r
+        it 'サインインページへ遷移する' do\r
+          get :credit, :id => @pic.id\r
+          response.body.should redirect_to '/users/sign_in'\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it 'ステータスコード401 Unauthorizedを返す' do\r
+          get :credit, :id => @pic.id, :format => :json\r
+          response.status.should eq 401\r
+        end\r
+        it '応答メッセージにUnauthorizedを返す' do\r
+          get :credit, :id => @pic.id, :format => :json\r
+          response.message.should match(/Unauthorized/)\r
+        end\r
+      end\r
+    end\r
+=begin\r
+    context '対象素材がないとき' do\r
+      before do\r
+        ResourcePicture.unstub(:show)\r
+      end\r
+      context 'html形式' do\r
+        it '例外404 not_foundを返す' do\r
+          lambda{\r
+            get :show, :id => 0\r
+          }.should raise_error(ActiveRecord::RecordNotFound)\r
+        end\r
+      end\r
+      context 'json形式' do\r
+        it '例外404 not_foundを返す' do\r
+          lambda{ \r
+            get :show, :id => 0, :format => :json\r
+          }.should raise_error(ActiveRecord::RecordNotFound)\r
+        end\r
+      end\r
+    end\r
+=end\r
+  end\r
 end