From 5f6e9c1524f9ebcc772a38e476f6254a29e4f768 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Mon, 1 Oct 2012 18:22:05 +0900 Subject: [PATCH] t#29705:create story show --- app/models/story.rb | 19 +++++++ app/views/panels/_standard.html.erb | 2 +- app/views/stories/show.html.erb | 6 +-- spec/models/story_spec.rb | 102 +++++++++++++++++++++++++++++++++++- 4 files changed, 124 insertions(+), 5 deletions(-) diff --git a/app/models/story.rb b/app/models/story.rb index ba150a01..5bfb1958 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -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) diff --git a/app/views/panels/_standard.html.erb b/app/views/panels/_standard.html.erb index 117823b1..3b4c7dc7 100644 --- a/app/views/panels/_standard.html.erb +++ b/app/views/panels/_standard.html.erb @@ -1,5 +1,5 @@
- <% @panel.each_element do |elm| %> + <% @panel.panel_elements.each do |elm| %> <% if elm.kind_of?(PanelPicture) %> <% end %> diff --git a/app/views/stories/show.html.erb b/app/views/stories/show.html.erb index c8b19a8a..9fe43d21 100644 --- a/app/views/stories/show.html.erb +++ b/app/views/stories/show.html.erb @@ -1,4 +1,4 @@ - <% @panel = story.panel %> + <% @panel = @story.panel %> <%= render 'panels/standard' %> <% if @story.author.id == @author.id -%> @@ -11,9 +11,9 @@ t
<% end -%> -<% end %> -<% if @comic.author.id == @author.id -%> +<% if @story.comic.own? @author -%> <%= link_to 'add panel', new_story_path, :remote => true %>
t
+<% end %> diff --git a/spec/models/story_spec.rb b/spec/models/story_spec.rb index 0837d3ab..5983b871 100644 --- a/spec/models/story_spec.rb +++ b/spec/models/story_spec.rb @@ -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 + 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 '検査対象がnil(ゲスト)のとき' do + context 'クローズドモードのとき' do + before do + MagicNumber['run_mode'] = 1 + end + it '不許可を返す。' do + r = @story.visible?(nil) + r.should be_false + end + end + context 'オープンモードのとき' do + before do + MagicNumber['run_mode'] = 0 + end + it '公開コミックのストーリーなら許可する' do + Comic.any_instance.stub(:visible?).with(nil).and_return(true) + r = @story.visible?(nil) + r.should be_true + end + it '非公開コミックのストーリーなら許可しない' do + Comic.any_instance.stub(:visible?).with(nil).and_return(false) + r = @story.visible?(nil) + r.should be_false + end + end + end + context '検査対象が作家のとき' do + it '自分のコミックのストーリーなら許可する' do + Comic.any_instance.stub(:own?).with(@author).and_return(true) + Comic.any_instance.stub(:visible?).with(@author).and_return(true) + r = @story.visible?(@author) + r.should == true + end + it '他人の非公開コミックのストーリーなら許可しない' do + Comic.any_instance.stub(:own?).with(@other_author).and_return(false) + Comic.any_instance.stub(:visible?).with(@other_author).and_return(false) + r = @story.visible?(@other_author) + r.should == false + end + it '他人のコミックのストーリーでも公開なら許可する' do + Comic.any_instance.stub(:own?).with(@other_author).and_return(false) + Comic.any_instance.stub(:visible?).with(@other_author).and_return(true) + r = @story.visible?(@other_author) + r.should == true + end + end + context '検査対象がそれ以外のとき' do + it '不許可を返す。' do + r = @story.visible?(@admin) + r.should be_false + end + end + end + 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 + it '単体取得オプションを利用している' do + Story.stub(:show_opt).with(any_args).and_return({}) + Story.should_receive(:show_opt).with(any_args).exactly(1) + r = Story.show @story.id, @author + end + it '閲覧許可を問い合わせている' do + Story.any_instance.stub(:visible?).with(@author).and_return(true) + Story.any_instance.should_receive(:visible?).with(@author).exactly(1) + r = Story.show @story.id, @author + end + end + 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 -- 2.11.0