OSDN Git Service

play panel_elements
authoryasushiito <yasushiito@git.sourceforge.jp>
Tue, 20 Mar 2012 02:27:59 +0000 (11:27 +0900)
committeryasushiito <yasushiito@git.sourceforge.jp>
Tue, 20 Mar 2012 02:27:59 +0000 (11:27 +0900)
app/controllers/comics_controller.rb
app/models/comic.rb
app/models/panel.rb
app/views/comics/_form.html.erb
app/views/comics/play.html.erb
app/views/panels/_standard.html.erb
spec/controllers/comics_controller_spec.rb
spec/models/comic_spec.rb

index d5cea53..1a1dc68 100644 (file)
@@ -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
index 78d0c5c..44b1f3b 100644 (file)
@@ -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
index 888d7b5..c383e1a 100644 (file)
@@ -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
index 2991db6..62a9a0a 100644 (file)
   </div>
   <div class="field">
     <%= f.label :visible %><br />
-    <%= f.number_field :visible %>
+    <%= f.collection_select :visible, [['only me', 0], ['everyone', 3]], :last, :first, :html => {:selected => @comic.visible} %>
   </div>
   <div class="field">
     <%= f.label :editable %><br />
-    <%= f.number_field :editable %>
+    <%= f.collection_select :editable, [['only me', 0], ['everyone', 3]], :last, :first, :html => {:selected => @comic.editable} %>
   </div>
 
   <div class="actions">
index 0fac1f7..2134033 100644 (file)
@@ -1,7 +1,10 @@
 <h1><%= h @comic.title %></h1>
 <% if @comic.own? @author -%>
-<div>
-<%= render 'form' %>
+  <div>
+    <%= link_to 'edit comic property', edit_comic_path(@comic), :remote => true %>
+  </div>
+  <div id="newcomic">
+  </div>
 <% end -%>
 
 <% @comic.panels.each do |panel| %>
index 0cf3c24..aada79d 100644 (file)
@@ -2,20 +2,22 @@
 t:<%= @panel.t %>
 </span>
 <div class="panel" style="width:<%= @panel.width %>px;height:<%= @panel.height %>px;overflow:hidden; border:solid black <%= @panel.border %>px; background:white;">
-  <% @panel.panel_pictures.each do |panel_picture| %>
-    <div id="vPicture<%= panel_picture.id -%>" class="panel_picture" style="position:relative;  top:<%= panel_picture.y -%>px; left:<%= panel_picture.x -%>px; z-index:<%= panel_picture.z -%>; ">
-      <img style=" position:absolute; width:<%= panel_picture.width -%>px; height:<%= panel_picture.height -%>px; " src="<%= panel_picture.url -%>">
-    </div>
-  <% end %>
-  <% @panel.balloons.each do |balloon| %>
-    <div id="vballoon<%= balloon.id -%>" class="balloon" style="position:relative;  top:<%= balloon.y -%>px; left:<%= balloon.x -%>px; z-index:<%= balloon.z -%>; ">
-      <img style=" position:absolute; width:<%= balloon.width -%>px; height:<%= balloon.height -%>px; " src="<%= balloon.system_picture.url -%>">
-      <% balloon.speaches.each do |speach| %>
-        <div style="position:relative; top:<%= speach.y -%>px; left:<%= speach.x -%>px;width:<%= speach.width -%>px; height:<%= speach.height -%>px; top:1px; left:1px; z-index:1; ">
-          <%= h speach.content -%>
-        </div>
-      <% end %>
-    </div>
+  <% @panel.each_element do |elm| %>
+    <% if elm.kind_of?(PanelPicture) %>
+      <div id="vPicture<%= elm.id -%>" class="panel_picture" style="position:relative;  top:<%= elm.y -%>px; left:<%= elm.x -%>px; z-index:<%= elm.z -%>; ">
+        <img style=" position:absolute; width:<%= elm.width -%>px; height:<%= elm.height -%>px; " src="<%= elm.url -%>">
+      </div>
+    <% end %>
+    <% if elm.kind_of?(Balloon) %>
+      <div id="vballoon<%= elm.id -%>" class="balloon" style="position:relative;  top:<%= elm.y -%>px; left:<%= elm.x -%>px; z-index:<%= elm.z -%>; ">
+        <img style=" position:absolute; width:<%= elm.width -%>px; height:<%= elm.height -%>px; " src="<%= elm.system_picture.url -%>">
+        <% elm.speaches.each do |speach| %>
+          <div style="position:relative; top:<%= speach.y -%>px; left:<%= speach.x -%>px;width:<%= speach.width -%>px; height:<%= speach.height -%>px; top:1px; left:1px; z-index:1; ">
+            <%= h speach.content -%>
+          </div>
+        <% end %>
+      </div>
+    <% end %>
   <% end %>
 </div>
 <span>
index 7a11c89..4cb92bf 100644 (file)
 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
index 51b9fa8..628f134 100644 (file)
@@ -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