From b1213cd571b30627aa9eea353808b920956bd32d Mon Sep 17 00:00:00 2001 From: yasushiito Date: Tue, 20 Mar 2012 11:27:59 +0900 Subject: [PATCH] play panel_elements --- app/controllers/comics_controller.rb | 23 +- app/models/comic.rb | 16 +- app/models/panel.rb | 33 +++ app/views/comics/_form.html.erb | 4 +- app/views/comics/play.html.erb | 7 +- app/views/panels/_standard.html.erb | 30 ++- spec/controllers/comics_controller_spec.rb | 405 ++++++++++++++++++++++++----- spec/models/comic_spec.rb | 47 ++++ 8 files changed, 469 insertions(+), 96 deletions(-) diff --git a/app/controllers/comics_controller.rb b/app/controllers/comics_controller.rb index d5cea53d..1a1dc681 100644 --- a/app/controllers/comics_controller.rb +++ b/app/controllers/comics_controller.rb @@ -12,7 +12,7 @@ class ComicsController < ApplicationController def top @new_comics = Comic.find(:all, - :include => :author, :conditions => ['visible = 1'], :order => 'updated_at desc', :limit => 5 + :include => :author, :conditions => ['visible > 0'], :order => 'updated_at desc', :limit => 5 ) @new_pictures = OriginalPicture.find(:all, :include => [:artist, :license, :resource_picture], :order => 'updated_at', :limit => 5 @@ -50,29 +50,14 @@ class ComicsController < ApplicationController end def play - @comic = Comic.find(params[:id], include: [:author, {:panels => [:panel_pictures => :resource_picture, :balloons => :speaches]}], order: 'panels.t') - + @comic = Comic.play(params[:id]) respond_to do |format| format.html # index.html.erb format.json { - render :json => @comic.to_json( - :include => [:author, { - :panels => {:include => { - :panel_pictures => {:include => :resource_picture}, - :balloons => {:include => :speaches} - }} - }] - ) + render :json => @comic.to_json_play } format.jsonp { - render :json => "callback(" + @comic.to_json( - :include => { - :panels => {:include => { - :panel_pictures => {:include => :resource_picture}, - :balloons => {:include => :speaches} - }} - } - ) + ");" + render :json => "callback(" + @comic.to_json_play + ");" } end end diff --git a/app/models/comic.rb b/app/models/comic.rb index 78d0c5c8..44b1f3b3 100644 --- a/app/models/comic.rb +++ b/app/models/comic.rb @@ -49,7 +49,7 @@ class Comic < ActiveRecord::Base page_size = self.default_page_size if page_size < 1 page = 1 if page < 1 opt.merge!(self.list_opt) unless opt[:include] - opt.merge!({:conditions => ['visible = 1'], :order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size}) + opt.merge!({:conditions => ['visible > 0'], :order => 'updated_at desc', :limit => page_size, :offset => (page -1) * page_size}) Comic.find(:all, opt) end @@ -74,4 +74,18 @@ class Comic < ActiveRecord::Base {:include => :author} end + def self.play cid, opt = {} + Comic.find(cid, include: [ + :author, + :panels => [ + :panel_pictures => :resource_picture, + :balloons => :speaches + ] + ], order: 'panels.t, speaches.t') + end + + def to_json_play + self.to_json( :include => {:author => {}, :panels => {:methods => :panel_element}}) + end + end diff --git a/app/models/panel.rb b/app/models/panel.rb index 888d7b5c..c383e1a2 100644 --- a/app/models/panel.rb +++ b/app/models/panel.rb @@ -52,4 +52,37 @@ class Panel < ActiveRecord::Base self.author_id == author.id end + def sort_by_time + pe = [] + self.panel_pictures.each do |picture| + pe[picture.t] = picture + end + self.balloons.each do |balloon| + pe[balloon.t] = balloon + end + pe + end + + def each_element + self.sort_by_time.each do |e| + yield e + end + end + + def panel_elements + res = [] + self.each_element do |elm| + if elm.kind_of?(PanelPicture) + res[elm.t] = elm.to_json({:include => :resource_picture}) + end + if elm.kind_of?(Balloon) + res[elm.t] = elm.to_json({:include => :speaches}) + end + end + res + end + + def to_json_play + self.to_json :methods => :panel_elements + end end diff --git a/app/views/comics/_form.html.erb b/app/views/comics/_form.html.erb index 2991db63..62a9a0aa 100644 --- a/app/views/comics/_form.html.erb +++ b/app/views/comics/_form.html.erb @@ -25,11 +25,11 @@
<%= f.label :visible %>
- <%= f.number_field :visible %> + <%= f.collection_select :visible, [['only me', 0], ['everyone', 3]], :last, :first, :html => {:selected => @comic.visible} %>
<%= f.label :editable %>
- <%= f.number_field :editable %> + <%= f.collection_select :editable, [['only me', 0], ['everyone', 3]], :last, :first, :html => {:selected => @comic.editable} %>
diff --git a/app/views/comics/play.html.erb b/app/views/comics/play.html.erb index 0fac1f71..2134033b 100644 --- a/app/views/comics/play.html.erb +++ b/app/views/comics/play.html.erb @@ -1,7 +1,10 @@

<%= h @comic.title %>

<% if @comic.own? @author -%> -
-<%= render 'form' %> +
+ <%= link_to 'edit comic property', edit_comic_path(@comic), :remote => true %> +
+
+
<% end -%> <% @comic.panels.each do |panel| %> diff --git a/app/views/panels/_standard.html.erb b/app/views/panels/_standard.html.erb index 0cf3c243..aada79db 100644 --- a/app/views/panels/_standard.html.erb +++ b/app/views/panels/_standard.html.erb @@ -2,20 +2,22 @@ t:<%= @panel.t %>
- <% @panel.panel_pictures.each do |panel_picture| %> -
- -
- <% end %> - <% @panel.balloons.each do |balloon| %> -
- - <% balloon.speaches.each do |speach| %> -
- <%= h speach.content -%> -
- <% end %> -
+ <% @panel.each_element do |elm| %> + <% if elm.kind_of?(PanelPicture) %> +
+ +
+ <% end %> + <% if elm.kind_of?(Balloon) %> +
+ + <% elm.speaches.each do |speach| %> +
+ <%= h speach.content -%> +
+ <% end %> +
+ <% end %> <% end %>
diff --git a/spec/controllers/comics_controller_spec.rb b/spec/controllers/comics_controller_spec.rb index 7a11c895..4cb92bf0 100644 --- a/spec/controllers/comics_controller_spec.rb +++ b/spec/controllers/comics_controller_spec.rb @@ -1,96 +1,385 @@ require 'spec_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. - describe ComicsController do - # This should return the minimal set of attributes required to create a valid # Comic. As you add validations to Comic, be sure to # update the return value of this method accordingly. def valid_attributes - {} + {:title => 'test comic', :width => 100, :height => 50, :visible => 0, :editable => 0} end - describe "GET index" do - it "assigns all comics as @comics" do - comic = Comic.create! valid_attributes - get :index - assigns(:comics).should eq([comic]) + describe '一覧表示に於いて' do + before do + Pettanr.run_mode = 1 + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :index + response.should be_success + end + it '@comicsにリストを取得している' do + get :index + assigns(:comics).should have_at_least(3).items + end + context 'パラメータが' do + it 'indexテンプレートを描画する' do + get :index + response.should render_template("index") + end + end + context 'html形式' do + it 'indexテンプレートを描画する' do + get :index + response.should render_template("index") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :index, :format => :json + json = JSON.parse response.body + json.should match(/error/) + end + it 'データがリスト構造になっている' do + get :index, :format => :json + json = JSON.parse response.body + json.should have_at_least(3).items + end + end + end + context '作家権限がないとき' do + it 'ステータスコード401 Unauthorizedを返す' do + get :index + response.status.should eq 401 + end + context 'html形式' do + it 'サインインページへ遷移する' do + get :index + response.should redirect_to '/' + end + end + context 'json形式' do + it '文字列errorが含まれたjsonデータを返す' do + response.body.should have_tag('json', 'error') + end + end + context 'ただし、公開型のときだけは' do + it 'ステータスコード200 OKを返す' do + Pettanr.run_mode = 0 + get :index + response.should be_success + end + end end end - - describe "GET show" do - it "assigns the requested comic as @comic" do - comic = Comic.create! valid_attributes - get :show, :id => comic.id - assigns(:comic).should eq(comic) + + describe '単体表示に於いて' do + before do + Pettanr.run_mode = 1 + end + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :show, :id => 1 + response.should be_success + end + it '@comicにアレを取得している' do + get :show, :id => 1 + assigns(:comic).should eq(comic) + end + context 'html形式' do + it 'showテンプレートを描画する' do + get :show, :id => 1 + response.should render_template("show") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :show, :id => 1, :format => :json + json = JSON.parse response.body + json.should have_tag('json', 'error') + end + it 'データがアレになっている' do + get :show, :id => 1, :format => :json + json = JSON.parse response.body + json.should have_tag('json', 'error') + end + end + end + context '作家権限がないとき' do + it 'ステータスコード401 Unauthorizedを返す' do + get :show, :id => 1 + response.should eq 401 + end + context 'html形式' do + it 'サインインページへ遷移する' do + get :show, :id => 1 + response.should redirect_to '/' + end + end + context 'json形式' do + it '文字列errorが含まれたjsonデータを返す' do + + end + end + context 'ただし、公開型のときだけは' do + it 'ステータスコード200 OKを返す' do + Pettanr.run_mode = 0 + get :show, :id => 1 + response.should be_success + end + end + end + context '非公開コミックを見ようとしたとき' do + it 'ステータスコード403 forbiddenを返す' do + get :show, :id => 1 + response.status.should eq 403 + end + context 'html形式' do + it 'forbiddenページへ遷移する' do + get :show, :id => 1 + response.should redirect_to '/403.html' + end + end + context 'json形式' do + it 'エラーメッセージを返す' do + + end + end end end - describe "GET new" do - it "assigns a new comic as @comic" do - get :new - assigns(:comic).should be_a_new(Comic) + describe 'コミック数取得に於いて' do + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :count, :format => :json + response.should be_success + end + context 'json形式' do + it 'jsonデータを返す' do + get :count, :format => :json + json = JSON.parse response.body + json.should have_tag('json', 'error') + end + it 'データがリスト構造になっている' do + get :count, :format => :json + json = JSON.parse response.body + json.should have_tag('json', 'error') + end + end end end - describe "GET edit" do - it "assigns the requested comic as @comic" do - comic = Comic.create! valid_attributes - get :edit, :id => comic.id - assigns(:comic).should eq(comic) + describe '新規作成フォーム表示に於いて' do + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + get :new + response.should be_success + end + it '@comicに新規データを用意している' do + get :new + assigns(:comic).should be_a_new(Comic) + end + it '@comicにデフォルト値editable=0, visible=0がセットされている' do + get :new + end + context 'html形式' do + end + context 'js形式' do + it '部分フォームを返す' do + + end + end + end + context '作家権限がないとき' do + it 'ステータスコード401 Unauthorizedを返す' do + + end + context 'js形式' do + it '文字列errorが含まれたjsonデータを返す' do + + end + end end end - describe "POST create" do - describe "with valid params" do - it "creates a new Comic" do + describe '新規作成に於いて' do + context 'つつがなく終わるとき' do + it 'ステータスコード200 OKを返す' do + post :create + response.should be_success + end + it '作成される' do expect { post :create, :comic => valid_attributes }.to change(Comic, :count).by(1) end - - it "assigns a newly created comic as @comic" do + it "@comicに作成されたコミックを保持している" do post :create, :comic => valid_attributes assigns(:comic).should be_a(Comic) assigns(:comic).should be_persisted end - - it "redirects to the created comic" do - post :create, :comic => valid_attributes - response.should redirect_to(Comic.last) + context 'html形式' do + it '作成されたコミックの表示ページへ遷移する' do + post :create, :comic => valid_attributes + response.should redirect_to(Comic.last) + end + end + context 'json形式' do + it '作成されたコミックをjsonデータで返す' do + + end end end - - describe "with invalid params" do - it "assigns a newly created but unsaved comic as @comic" do - # Trigger the behavior that occurs when invalid params are submitted + context '作家権限がないとき' do + context 'html形式' do + it 'サインインページへ遷移する' do + + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + + end + it '文字列errorが含まれたjsonデータを返す' do + + end + end + end + context '検証、保存に失敗した' do + it "未保存のコミックを保持している" do Comic.any_instance.stub(:save).and_return(false) post :create, :comic => {} assigns(:comic).should be_a_new(Comic) end + context 'html形式' do + it '新規ページを描画する' do + Comic.any_instance.stub(:save).and_return(false) + post :create, :comic => {} + response.should render_template("new") + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + + end + it '文字列errorが含まれたjsonデータを返す' do + + end + end + end + end - it "re-renders the 'new' template" do - # Trigger the behavior that occurs when invalid params are submitted - Comic.any_instance.stub(:save).and_return(false) - post :create, :comic => {} - response.should render_template("new") + describe '編集フォーム表示に於いて' do + context 'つつがなく終わるとき' do + context 'js形式' do + it '部分フォームを返す' do + + end + it '指定のデータが入っている' do + + end + end + end + context '作家権限がないとき' do + it 'ステータスコード401 Unauthorizedを返す' do + + end + context 'js形式' do + it '文字列errorが含まれたjsonデータを返す' do + + end + end + end + context '対象コミックがないとき' do + it 'ステータスコード404 not_foundを返す' do + + end + context 'html形式' do + it '404ページへ遷移する' do + + end + end + context 'js形式' do + it '文字列errorが含まれたjsonデータを返す' do + + end + end + end + end + + describe '更新に於いて' do + context 'つつがなく終わるとき' do + it '更新される' do + + end + context 'html形式' do + it '更新されたコミックの表示ページへ遷移する' do + + end + end + context 'json形式' do + it '更新されたコミックをjsonデータで返す' do + + end + end + end + context '作家権限がないとき' do + it 'ステータスコード401 Unauthorizedを返す' do + + end + context 'html形式' do + it 'サインインページへ遷移する' do + + end + end + context 'json形式' do + it '文字列errorが含まれたjsonデータを返す' do + + end + end + end + context '他人のコミックを編集しようとしたとき' do + it 'ステータスコード403 forbiddenを返す' do + + end + context 'html形式' do + it '403ページへ遷移する' do + + end + end + context 'json形式' do + it '文字列errorが含まれたjsonデータを返す' do + + end + end + end + context '対象コミックがないとき' do + it 'ステータスコード404 not_foundを返す' do + + end + context 'html形式' do + it '404ページへ遷移する' do + + end + end + context 'json形式' do + it '文字列errorが含まれたjsonデータを返す' do + + end + end + end + context '検証、保存に失敗したとき' do + context 'html形式' do + it '編集ページを描画する' do + + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + + end + it '文字列errorが含まれたjsonデータを返す' do + + end end end end diff --git a/spec/models/comic_spec.rb b/spec/models/comic_spec.rb index 51b9fa8c..628f134b 100644 --- a/spec/models/comic_spec.rb +++ b/spec/models/comic_spec.rb @@ -2,4 +2,51 @@ require 'spec_helper' describe Comic do pending "add some examples to (or delete) #{__FILE__}" + before do + @comic = Comic.new + end + describe 'データ補充に於いて' do + context '初期値を補充するとき' do + it '空なら0が補充される' do + @comic.supply_default + @comic.editable.should == 0 + @comic.visible.should == 0 + end + it 'editableが空でないなら変化なし' do + @comic.editable = 1 + {@comic.supply_default}.should_not change(@comic, :editable) + end + it 'visibleが空でないなら変化なし' do + @comic.visible = 1 + {@comic.supply_default}.should_not change(@comic, :visible) + end + end + end + describe '作者判定に於いて' do + before do + end + context '同一作家のとき' do + it 'trueを返す' do + @comic.own?(author).should == true + end + end + context '同一作家でないとき' do + it 'falseを返す' do + @comic.own?(author).should == false + end + end + context 'パラメータが不正なとき' do + it 'falseを返す' do + @comic.own?(nil).should == false + end + end + end + describe '保存検証に於いて' do + before do + end + context '同一作家のとき' do + it 'trueを返す' do + end + end + end end -- 2.11.0