class ComicsController < ApplicationController
- before_filter :authenticate_user!, :only => [:top, :index, :show, :play, :create, :update, :destroy]
+ if Const.run_mode == 0
+ before_filter :authenticate_user!, :only => [:new, :create, :edit, :update, :destroy]
+ else
+ before_filter :authenticate_user!, :only => [:top, :index, :show, :play, :new, :create, :edit, :update, :destroy]
+ end
before_filter :authenticate_admin!, :only => [:list, :browse]
private
def top
@new_comics = Comic.find(:all,
- :include => :author, :conditions => ['visible = 1'], :order => 'updated_at desc', :limit => 5
+ :include => :author, :conditions => ['visible > 0'], :order => 'updated_at desc', :limit => 5
)
@new_pictures = OriginalPicture.find(:all,
:include => [:artist, :license, :resource_picture], :order => 'updated_at', :limit => 5
# GET /comics
# GET /comics.json
def index
- @comics = Comic.list({}, params[:page].to_i, params[:page_size].to_i)
+ @page = Comic.page params[:page]
+ @page_size = Comic.page_size params[:page_size]
+ @comics = Comic.list({}, @page, @page_size)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @comics.to_json(Comic.list_json_opt) }
# GET /comics/1
# GET /comics/1.json
def show
- @comic = Comic.show(params[:id])
+ @comic = Comic.show(params[:id], @author)
respond_to do |format|
- if @comic.own?(@author) or @comic.visible > 0
- format.html # show.html.erb
- format.json { render json: @comic.to_json(Comic.show_json_opt) }
- else
- format.html { render :status => :forbidden, :file => '/403.html'}
- format.json { head :forbidden }
- end
+ format.html # show.html.erb
+ format.json { render json: @comic.to_json(Comic.show_json_include_opt) }
end
end
+ def count
+ @comic = {:count => Comic.visible_count}
+ respond_to do |format|
+ format.json { render json: @comic.to_json }
+ end
+ end
+
def play
- @comic = Comic.find(params[:id], include: [:author, {:panels => [:panel_pictures => :resource_picture, :balloons => :speaches]}], order: 'panels.t')
-
+ @comic = Comic.play(params[:id])
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}
- }}
- }]
- )
+ render :json => @comic.to_json_play
}
format.jsonp {
- render :json => "callback(" + @comic.to_json(
- :include => {
- :panels => {:include => {
- :panel_pictures => {:include => :resource_picture},
- :balloons => {:include => :speaches}
- }}
- }
- ) + ");"
+ render :json => "callback(" + @comic.to_json_play + ");"
}
end
end
# GET /comics/1/edit
# GET /comics/1.js/edit
def edit
- @comic = Comic.find(params[:id])
+ @comic = Comic.show(params[:id], @author)
+ @comic.supply_default
respond_to do |format|
- if @comic.own? @author
- format.html
- format.js
- else
- format.html { render :status => :forbidden, :file => '/403.html'}
- format.js { head :forbidden }
- end
+ format.html
+ format.js
end
end
respond_to do |format|
if @comic.save
format.html { redirect_to @comic, notice: 'Comic was successfully created.' }
- format.json { render json: Comic.show(@comic.id).to_json(Comic.show_json_opt), status: :created, location: @comic }
+ format.json { render json: Comic.show(@comic.id, @author).to_json(Comic.show_json_include_opt), status: :created, location: @comic }
else
format.html { render action: "new" }
format.json { render json: @comic.errors, status: :unprocessable_entity }
# PUT /comics/1.json
def update
params[:comic].merge! author_id: @author.id
- @comic = Comic.find(params[:id])
+ @comic = Comic.show(params[:id], @author)
respond_to do |format|
- if @comic.own? @author
- 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
+ raise ActiveRecord::Forbidden unless @comic.own?(@author)
+ if @comic.update_attributes(params[:comic])
+ format.html { redirect_to @comic, notice: 'Comic was successfully updated.' }
+ format.json { head :ok }
else
- format.html { render :status => :forbidden, :file => '/403.html'}
- format.json { head :forbidden }
+ format.html { render action: "edit" }
+ format.json { render json: @comic.errors, status: :unprocessable_entity }
end
end
end