# 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
# 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
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
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