From: yasushiito Date: Thu, 15 Mar 2012 07:43:07 +0000 (+0900) Subject: done @ comic.index X-Git-Url: http://git.osdn.net/view?p=pettanr%2Fpettanr.git;a=commitdiff_plain;h=935088530096e47d286ac201968731ea26d73038;ds=sidebyside done @ comic.index --- diff --git a/app/controllers/comics_controller.rb b/app/controllers/comics_controller.rb index 2af0b306..411f81fd 100644 --- a/app/controllers/comics_controller.rb +++ b/app/controllers/comics_controller.rb @@ -26,13 +26,10 @@ class ComicsController < ApplicationController # 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 - format.json { render json: @comics } + format.json { render json: @comics.to_json(Comic.json_opt) } end end @@ -107,6 +104,15 @@ class ComicsController < ApplicationController # 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 @@ -142,8 +148,8 @@ class ComicsController < ApplicationController 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 diff --git a/app/models/comic.rb b/app/models/comic.rb index 4e6f0f41..7e6743d2 100644 --- a/app/models/comic.rb +++ b/app/models/comic.rb @@ -36,4 +36,30 @@ class Comic < ActiveRecord::Base 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