OSDN Git Service

add show
[pettanr/pettanr.git] / app / controllers / comics_controller.rb
index 8a5169c..3ef277e 100644 (file)
 class ComicsController < ApplicationController
-  before_filter :authenticate_author!, :only => [:top, :index, :show, :play, :create, :update, :destroy]
-  before_filter :authenticate_admin!, :only => [:list, :browse]
+  if Manifest.manifest.magic_numbers['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 => [
+      :index, :show, :play, :by_story, :by_author, :count, :count_by_story, :count_by_author
+    ]
+    before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
+    before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
+  end
 
-  private
+  def self.model
+    Comic
+  end
   
-  def treat_param comic
-    comic.author_id = current_author.id
+  def index
+    filer_list
   end
   
-  public
+  def by_story
+    filer_list
+  end
   
-  def top
-    @comics = Comic.all
-
-    respond_to do |format|
-      format.html # index.html.erb
-    end
+  def by_author
+    filer_list
   end
   
-  # GET /comics
-  # GET /comics.json
-  def index
-    @comics = Comic.find(:all, 
-      :include => :author, :conditions => ['visible = 1'], :order => 'updated_at desc', :limit => 20
-    )
-
-    respond_to do |format|
-      format.html # index.html.erb
-      format.json { render json: @comics }
-    end
+  def show_html_format format
+    format.html {
+      play_list = Locmare::ListGroup.list 'comic_story', 'play'
+      @play_count = play_list.count(@operators, 
+        {:id => @item.id, :my_play => @item.own?(@operators)}
+      )
+    }
   end
-
-  # GET /comics/1
-  # GET /comics/1.json
+  
   def show
-    @comic = Comic.find(params[:id])
-
+    set_show
     respond_to do |format|
-#      format.html # show.html.erb
-      format.json { render json: @comic }
+      show_html_format format
+      show_prof_format format
+      show_json_format format
+      format.atom 
+      format.rss 
     end
   end
-
+  
   def play
-    @comic = Comic.find(params[:id], include: [:author, {:panels => [:panel_pictures => :resource_picture, :balloons => :speaches]}], order: 'panels.t')
-
+    @item = self.class.model.show(params[:id], @operators)
+    set_play
+    @items = @list.items(@operators, 
+      {:id => params[:id], :my_play => @item.own?(@operators)},
+      0, -1 # no limit
+    )
+    @count = @items.count
+    # no pager
     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}
-            }}
-          }]
-        )
-      }
-      format.jsonp {
-        render :json => "callback(" + @comic.to_json(
-          :include => {
-            :panels => {:include => {
-              :panel_pictures => {:include => :resource_picture}, 
-              :balloons => {:include => :speaches}
-            }}
-          }
-        ) + ");"
+      format.html {
+        if @item.own? @operators
+          @new_story_items = assist_items('story', 'private')
+        end
       }
+      format.json { render json: @items.to_json }
     end
   end
   
-  def list
-    @comics = Comic.all
-
-    respond_to do |format|
-      format.html { render layout: 'system' }# index.html.erb
-      format.json { render json: @comics }
-    end
+  def count
+    list_count
   end
-
-  def browse
-    @comic = Comic.find(params[:id])
-
-    respond_to do |format|
-      format.html { render layout: 'system' } # show.html.erb
-      format.json { render json: @comic }
-    end
+  
+  def count_by_story
+    list_count
+  end
+  
+  def count_by_author
+    list_count
   end
   
-  # GET /comics/new
-  # GET /comics/new.json
   def new
-    @comic = Comic.new
-
-    respond_to do |format|
-      format.html # new.html.erb
-      format.json { render json: @comic }
-    end
+    form_new
   end
-
-  # GET /comics/1/edit
+  
   def edit
-    @comic = Comic.find(params[:id])
+    form_edit
   end
-
-  # POST /comics
-  # POST /comics.json
+  
   def create
-    @comic = Comic.new(params[:comic])
-    treat_param @comic
-
-    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 }
-      else
-        format.html { render action: "new" }
-        format.json { render json: @comic.errors, status: :unprocessable_entity }
-      end
-    end
+    set_model
+    @item = @my_model_class.new
+    @item.supply_default 
+    @my_model_class.fold_extend_settings params[@my_model_class.item_name]
+    @item.attributes = params[@my_model_class.item_name]
+    @item.overwrite @operators
+    render_create
   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
-      end
-    else
-      format.html { render action: "edit" }
-      format.json { render json: @comic.errors, status: :unprocessable_entity }
-    end
+    set_edit
+    @my_model_class.fold_extend_settings params[@my_model_class.item_name]
+    @item.attributes = params[@my_model_class.item_name]
+    @item.overwrite @operators
+    render_update
   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 }
+    @item = Comic.edit(params[:id], @operators)
+    respond_to do |format|
+      if @item.destroy_with_leafs
+        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 @item }
+        format.json { render json: @item.errors, status: :unprocessable_entity }
       end
-    else
-      format.html { render action: "edit" }
-      format.json { render json: @comic.errors, status: :unprocessable_entity }
     end
   end
 end