OSDN Git Service

done @ comic.index
authoryasushiito <yasushiito@git.sourceforge.jp>
Thu, 15 Mar 2012 07:43:07 +0000 (16:43 +0900)
committeryasushiito <yasushiito@git.sourceforge.jp>
Thu, 15 Mar 2012 07:43:07 +0000 (16:43 +0900)
app/controllers/comics_controller.rb
app/models/comic.rb

index 2af0b30..411f81f 100644 (file)
@@ -26,13 +26,10 @@ class ComicsController < ApplicationController
   # GET /comics
   # GET /comics.json
   def index
   # GET /comics
   # GET /comics.json
   def index
-    @comics = Comic.find(:all, 
-      :include => :author, :conditions => ['visible = 1'], :order => 'updated_at desc', :limit => 20
-    )
-
+    @comics = Comic.list({}, params[:page].to_i, params[:page_size].to_i)
     respond_to do |format|
       format.html # index.html.erb
     respond_to do |format|
       format.html # index.html.erb
-      format.json { render json: @comics }
+      format.json { render json: @comics.to_json(Comic.json_opt) }
     end
   end
 
     end
   end
 
@@ -107,6 +104,15 @@ class ComicsController < ApplicationController
   # GET /comics/1/edit
   def edit
     @comic = Comic.find(params[:id])
   # GET /comics/1/edit
   def edit
     @comic = Comic.find(params[:id])
+    respond_to do |format|
+      if @comic.own? @author
+        format.html 
+        format.json { render json: @comic }
+      else
+        format.html { render :status => :forbidden,  :file => '/403.html'}
+        format.json { head :forbidden }
+      end
+    end
   end
 
   # POST /comics
   end
 
   # POST /comics
@@ -142,8 +148,8 @@ class ComicsController < ApplicationController
           format.json { render json: @comic.errors, status: :unprocessable_entity }
         end
       else
           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 }
+        format.html { render :status => :forbidden,  :file => '/403.html'}
+        format.json { head :forbidden }
       end
     end
   end
       end
     end
   end
index 4e6f0f4..7e6743d 100644 (file)
@@ -36,4 +36,30 @@ class Comic < ActiveRecord::Base
     visible == 1 ? 'O' : 'X'
   end
   
     visible == 1 ? 'O' : 'X'
   end
   
+  def self.default_page_size
+    25
+  end
+  
+  def self.max_page_size
+    100
+  end
+  
+  def self.list opt = {}, page = 1, page_size = self.default_page_size
+    page_size = self.max_page_size if page_size > self.max_page_size
+    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})
+    p opt
+    Comic.find(:all, opt)
+  end
+  
+  def self.list_opt
+    {:include => :author}
+  end
+  
+  def self.json_opt
+    {:include => :author}
+  end
+  
 end
 end