OSDN Git Service

t#29705:create story show
authoryasushiito <yas@pen-chan.jp>
Mon, 1 Oct 2012 09:22:05 +0000 (18:22 +0900)
committeryasushiito <yas@pen-chan.jp>
Mon, 1 Oct 2012 09:22:05 +0000 (18:22 +0900)
app/models/story.rb
app/views/panels/_standard.html.erb
app/views/stories/show.html.erb
spec/models/story_spec.rb

index ba150a0..5bfb195 100644 (file)
@@ -23,6 +23,17 @@ class Story < ActiveRecord::Base
     self.author_id == au.id
   end
   
+  def visible? au
+    if au == nil
+      return false if MagicNumber['run_mode'] == 1
+    elsif au.is_a?(Author)
+      return true if self.comic.own?(au)
+    else
+      return false
+    end
+    self.comic.visible? au
+  end
+  
   def self.default_panel_size
     30
   end
@@ -90,6 +101,14 @@ class Story < ActiveRecord::Base
     Story.find(:all, opt)
   end
   
+  def self.show sid, au
+    opt = {}
+    opt.merge!(Story.show_opt)
+    res = Story.find sid, opt
+    raise ActiveRecord::Forbidden unless res.visible?(au)
+    res
+  end
+  
   def self.edit sid, au
     opt = {}
     opt.merge!(Story.show_opt)
index 117823b..3b4c7dc 100644 (file)
@@ -1,5 +1,5 @@
 <div class="pettanr-comic-panel" style="width:<%= @panel.width %>px;height:<%= @panel.height %>px;border-width: <%= @panel.border %>px; background-color:white;">
-  <% @panel.each_element do |elm| %>
+  <% @panel.panel_elements.each do |elm| %>
     <% if elm.kind_of?(PanelPicture) %>
         <img id="vPicture<%= elm.id -%>" width="<%= elm.width -%>" height="<%= elm.height -%>" style="top:<%= elm.y -%>px; left:<%= elm.x -%>px; z-index:<%= elm.z -%>; " src="<%= elm.url -%>">
     <% end %>
index c8b19a8..9fe43d2 100644 (file)
@@ -1,4 +1,4 @@
-  <% @panel = story.panel %>
+  <% @panel = @story.panel %>
   <%= render 'panels/standard' %>
   <% if @story.author.id == @author.id -%>
     <span>
@@ -11,9 +11,9 @@
         t
       </div>
   <% end -%>
-<% end %>
-<% if @comic.author.id == @author.id -%>
+<% if @story.comic.own? @author -%>
   <%= link_to 'add panel', new_story_path, :remote => true %>
     <div id="story-create">
       t
     </div>
+<% end %>
index 0837d3a..5983b87 100644 (file)
@@ -3,7 +3,7 @@ require 'spec_helper'
 #ストーリー
 describe Story do
   before do
-    FactoryGirl.create :admin
+    @admin = FactoryGirl.create :admin
     @sp = FactoryGirl.create :system_picture
     @lg = FactoryGirl.create :license_group
     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
@@ -151,6 +151,67 @@ describe Story do
       @story.own?(nil).should == false
     end
   end
+  
+  describe '閲覧許可に於いて' do\r
+    before do\r
+      @comic = FactoryGirl.create :comic, :author_id => @author.id
+      @panel = FactoryGirl.create :panel, :author_id => @author.id
+      @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
+    end\r
+    context '検査対象がnil(ゲスト)のとき' do
+      context 'クローズドモードのとき' do
+        before do
+          MagicNumber['run_mode'] = 1
+        end
+        it '不許可を返す。' do\r
+          r = @story.visible?(nil)
+          r.should be_false
+        end\r
+      end\r
+      context 'オープンモードのとき' do
+        before do
+          MagicNumber['run_mode'] = 0
+        end
+        it '公開コミックのストーリーなら許可する' do\r
+          Comic.any_instance.stub(:visible?).with(nil).and_return(true)\r
+          r = @story.visible?(nil)
+          r.should be_true
+        end\r
+        it '非公開コミックのストーリーなら許可しない' do\r
+          Comic.any_instance.stub(:visible?).with(nil).and_return(false)\r
+          r = @story.visible?(nil)
+          r.should be_false
+        end\r
+      end\r
+    end\r
+    context '検査対象が作家のとき' do
+      it '自分のコミックのストーリーなら許可する' do\r
+        Comic.any_instance.stub(:own?).with(@author).and_return(true)\r
+        Comic.any_instance.stub(:visible?).with(@author).and_return(true)\r
+        r = @story.visible?(@author)
+        r.should == true
+      end\r
+      it '他人の非公開コミックのストーリーなら許可しない' do\r
+        Comic.any_instance.stub(:own?).with(@other_author).and_return(false)\r
+        Comic.any_instance.stub(:visible?).with(@other_author).and_return(false)\r
+        r = @story.visible?(@other_author)
+        r.should == false
+      end\r
+      it '他人のコミックのストーリーでも公開なら許可する' do\r
+        Comic.any_instance.stub(:own?).with(@other_author).and_return(false)\r
+        Comic.any_instance.stub(:visible?).with(@other_author).and_return(true)\r
+        r = @story.visible?(@other_author)
+        r.should == true
+      end\r
+    end\r
+    context '検査対象がそれ以外のとき' do
+      it '不許可を返す。' do\r
+        r = @story.visible?(@admin)
+        r.should be_false
+      end\r
+    end\r
+  end\r
+  \r
   describe '一覧取得に於いて' do
     before do
       @comic = FactoryGirl.create :comic, :author_id => @author.id
@@ -376,6 +437,45 @@ describe Story do
     end
   end
   
+  describe '単体取得に於いて' do
+    before do
+      @comic = FactoryGirl.create :comic, :author_id => @author.id
+      @panel = FactoryGirl.create :panel, :author_id => @author.id
+      @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
+    end
+    context 'つつがなく終わるとき' do\r
+      it '単体取得オプションを利用している' do\r
+        Story.stub(:show_opt).with(any_args).and_return({})\r
+        Story.should_receive(:show_opt).with(any_args).exactly(1)\r
+        r = Story.show @story.id, @author
+      end\r
+      it '閲覧許可を問い合わせている' do\r
+        Story.any_instance.stub(:visible?).with(@author).and_return(true)\r
+        Story.any_instance.should_receive(:visible?).with(@author).exactly(1)\r
+        r = Story.show @story.id, @author
+      end\r
+    end\r
+    it '指定のストーリーを返す' do
+      l = Story.show @story.id, @author
+      l.should eq @story
+    end
+    context '他人のストーリーを開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        Story.any_instance.stub(:visible?).with(@other_author).and_return(false)
+        lambda{
+          Story.show @story.id, @other_author
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しないストーリーを開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          Story.show 110, @author
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
+  
   describe '編集取得に於いて' do
     before do
       @comic = FactoryGirl.create :comic, :author_id => @author.id