OSDN Git Service

add story
[pettanr/pettanr.git] / app / controllers / stories_controller.rb
index 49799e0..376536e 100644 (file)
@@ -1,2 +1,120 @@
 class StoriesController < ApplicationController
+  layout 'test' if Pettanr::TestLayout
+  if Const.run_mode == 0
+    before_filter :authenticate_user!, :only => [:new, :create, :edit, :update, :destroy]
+  else
+    before_filter :authenticate_user!, :only => [:index, :show, :new, :create, :edit, :update, :destroy]
+  end
+  before_filter :authenticate_admin!, :only => [:list, :browse]
+
+  def show
+    @comic = Comic.show(params[:id], @author)
+    cnt = Story.count(:conditions => ['comic_id = ?', @comic.id]).to_i
+    @offset = Story.offset cnt, params[:offset]
+    @panel_count = Story.panel_count cnt, params[:count]
+    @stories = Story.list(@comic, @author, @offset, @panel_count)
+    respond_to do |format|
+      format.html # index.html.erb
+      format.json {
+        render :json => @stories.to_json_list
+      }
+      format.jsonp {
+        render :json => "callback(" + @stories.to_json_list + ");"
+      }
+    end
+  end
+  
+  def list
+    @stories = Story.all
+
+    respond_to do |format|
+      format.html { render layout: 'system' }# index.html.erb
+      format.json { render json: @stories }
+    end
+  end
+
+  def browse
+    @story = Story.find(params[:id])
+
+    respond_to do |format|
+      format.html { render layout: 'system' } # show.html.erb
+      format.json { render json: @story }
+    end
+  end
+  
+  # GET /stories/new
+  # GET /stories/new.js
+  # GET /stories/new.json
+  def new
+    @story = Story.new 
+    @story.supply_default @author
+    @form_opt = {}
+    respond_to do |format|
+      format.html # new.html.erb
+      format.js { @form_opt = {:remote => true} ;  render action: "new" }
+      format.json { render json: @story }
+    end
+  end
+
+  # GET /stories/1/edit
+  # GET /stories/1.js/edit
+  def edit
+    @story = Story.show(params[:id], @author)
+    respond_to do |format|
+      format.html 
+      format.js
+    end
+  end
+
+  # POST /stories
+  # POST /stories.js
+  # POST /stories.json
+  def create
+    @story = Story.new 
+    @story.supply_default @author
+    @story.attributes = params[:story]
+    
+    respond_to do |format|
+      if @story.store
+        format.html { redirect_to action: :show, id: @story.comic_id }
+        format.js { redirect_to action: :show, id: @story.comic_id }
+        format.json { render json: @story.to_json() }
+      else
+        format.html { render action: "new" }
+        format.json { render json: @story.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+  
+  # PUT /stories/1
+  # PUT /stories/1.js
+  # PUT /stories/1.json
+  def update
+    @story = Story.show(params[:id], @author)
+    @story.author_id = @author.id
+    ot = @story.t
+    @story.attributes = params[:story]
+    respond_to do |format|
+      if @story.store ot
+        format.html { redirect_to action: :show, id: @story.comic_id }
+        format.js { redirect_to action: :show, id: @story.comic_id }
+        format.json { head :ok }
+      else
+        format.html { render action: "edit" }
+        format.json { render json: @story.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # DELETE /stories/1
+  # DELETE /stories/1.js
+  # DELETE /stories/1.json
+  def destroy
+    @story = Story.show(params[:id], @author)
+    @story.destroy_and_shorten
+    respond_to do |format|
+      format.html { redirect_to story_path(@story.comic) }
+      format.json { head :ok }
+    end
+  end
 end