OSDN Git Service

temp
[pettanr/pettanr.git] / app / controllers / comics_controller.rb
index 7200ab0..f1f2909 100644 (file)
 class ComicsController < ApplicationController
-  before_filter :authenticate_author!, :except => [:index, :show, :play]
-
-  private
-  
-  def treat_param comic
-    comic.author_id = current_author.id
+  layout 'test' if MagicNumber['test_layout']
+  if MagicNumber['run_mode'] == 0
+    before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
+    before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
+  else
+    before_filter :authenticate_reader, :only => [:top, :index, :show]
+    before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
+    before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
   end
-  
-  public
-  
-  def top
+  before_filter :authenticate_admin!, :only => [:list, :browse]
+
+  def self.model
+    Comic
   end
   
-  # GET /comics
-  # GET /comics.json
   def index
-    @comics = Comic.all
+    public_list
+  end
 
+  def show
+    @item = Comic.show(params[:id], @operators)
     respond_to do |format|
-      format.html # index.html.erb
-      format.json { render json: @comics }
+      format.html {
+        @comic = @item
+      }
+      format.prof { 
+        @profiler = @@profiler_manager.open(@item, @operators)
+        render :template => 'system/prof', :locals => {
+          :profiler => @profiler
+        }
+      }
+      format.json { render json: @item.to_json(Comic.show_json_opt) }
+      format.atom 
+      format.rss 
     end
   end
 
-  # GET /comics/1
-  # GET /comics/1.json
-  def show
-    @comic = Comic.find(params[:id])
+  def count
+    @comic = {:count => Comic.visible_count}
+    respond_to do |format|
+      format.json { render json: @comic.to_json }
+    end
+  end
+  
+  def list
+    @comics = Comic.all
 
     respond_to do |format|
-      format.html # show.html.erb
-      format.json { render json: @comic }
+      format.html { render layout: 'system' }# index.html.erb
+      format.json { render json: @comics }
     end
   end
 
-  def play
-    @comic = Comic.find(params[:id], include: {:panels => [:panel_pictures => :resource_picture, :balloons => :speaches]}, order: 'panels.t')
+  def browse
+    @comic = Comic.find(params[:id])
 
     respond_to do |format|
-      format.html # index.html.erb
-      format.json {
-        render :json => @comic.to_json(
-          :include => {
-            :panels => {:include => {
-              :panel_pictures => {:include => :resource_picture}, 
-              :balloons => {:include => :speaches}
-            }}
-          }
-        )
-      }
-      format.jsonp {
-        render :json => "callback(" + @comic.to_json(
-          :include => {
-            :panels => {:include => {
-              :panel_pictures => {:include => :resource_picture}, 
-              :balloons => {:include => :speaches}
-            }}
-          }
-        ) + ");"
-      }
+      format.html { render layout: 'system' } # show.html.erb
+      format.json { render json: @comic }
     end
   end
   
-  # GET /comics/new
-  # GET /comics/new.json
   def new
     @comic = Comic.new
-
+    @comic.supply_default
     respond_to do |format|
       format.html # new.html.erb
-      format.json { render json: @comic }
+      format.js
+      format.json { render json: @comic.to_json(Comic.show_json_opt) }
     end
   end
 
-  # GET /comics/1/edit
   def edit
-    @comic = Comic.find(params[:id])
+    @comic = Comic.edit(params[:id], @author)
+    respond_to do |format|
+      format.html 
+      format.js
+    end
   end
 
-  # POST /comics
-  # POST /comics.json
   def create
-    @comic = Comic.new(params[:comic])
-    treat_param @comic
+    @comic = Comic.new
+    @comic.supply_default 
+    @comic.attributes = params[:comic]
+    @comic.overwrite @operators
 
     respond_to do |format|
       if @comic.save
-        format.html { redirect_to @comic, notice: 'Comic was successfully created.' }
-        format.json { render json: @comic, status: :created, location: @comic }
+        flash[:notice] = I18n.t('flash.notice.created', :model => Comic.model_name.human)
+        format.html { redirect_to @comic }
+        format.json { render json: @comic.to_json(Comic.show_json_opt), status: :created, location: @comic }
       else
+        flash[:notice] = I18n.t('flash.notice.not_created', :model => Comic.model_name.human)
         format.html { render action: "new" }
         format.json { render json: @comic.errors, status: :unprocessable_entity }
       end
     end
   end
 
-  # PUT /comics/1
-  # PUT /comics/1.json
   def update
-    @comic = Comic.find(params[:id])
-    if @comic.own? current_author
-      respond_to do |format|
-        if @comic.update_attributes(params[:comic])
-          format.html { redirect_to @comic, notice: 'Comic was successfully updated.' }
-          format.json { head :ok }
-        else
-          format.html { render action: "edit" }
-          format.json { render json: @comic.errors, status: :unprocessable_entity }
-        end
+    @comic = Comic.edit(params[:id], @operators)
+    @comic.attributes = params[:comic]
+    @comic.overwrite @operators
+    respond_to do |format|
+      if @comic.save
+        flash[:notice] = I18n.t('flash.notice.updated', :model => Comic.model_name.human)
+        format.html { redirect_to @comic }
+        format.json { head :ok }
+      else
+        flash[:notice] = I18n.t('flash.notice.not_updated', :model => Comic.model_name.human)
+        format.html { render action: "edit" }
+        format.json { render json: @comic.errors, status: :unprocessable_entity }
       end
-    else
-      format.html { render action: "edit" }
-      format.json { render json: @comic.errors, status: :unprocessable_entity }
     end
   end
 
-  # DELETE /comics/1
-  # DELETE /comics/1.json
   def destroy
-    @comic = Comic.find(params[:id])
-    if @comic.own? current_author
-      @comic.destroy
-      respond_to do |format|
-        format.html { redirect_to comics_url }
+    @comic = Comic.edit(params[:id], @author)
+    respond_to do |format|
+      if @comic.destroy_with_story
+        flash[:notice] = I18n.t('flash.notice.destroyed', :model => Comic.model_name.human)
+        format.html { redirect_to '/home/comics' }
         format.json { head :ok }
+      else
+        flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Comic.model_name.human)
+        format.html { redirect_to @comic }
+        format.json { render json: @comic.errors, status: :unprocessable_entity }
       end
-    else
-      format.html { render action: "edit" }
-      format.json { render json: @comic.errors, status: :unprocessable_entity }
     end
   end
 end