OSDN Git Service

done @ comic.index
[pettanr/pettanr.git] / app / controllers / comics_controller.rb
1 class ComicsController < ApplicationController
2   before_filter :authenticate_user!, :only => [:top, :index, :show, :play, :create, :update, :destroy]
3   before_filter :authenticate_admin!, :only => [:list, :browse]
4
5   private
6   
7   def treat_param comic
8     comic.author_id = @author.id
9   end
10   
11   public
12   
13   def top
14     @new_comics = Comic.find(:all, 
15       :include => :author, :conditions => ['visible = 1'], :order => 'updated_at desc', :limit => 5
16     )
17     @new_pictures = OriginalPicture.find(:all, 
18       :include => [:artist, :license, :resource_picture], :order => 'updated_at', :limit => 5
19     )
20
21     respond_to do |format|
22       format.html # index.html.erb
23     end
24   end
25   
26   # GET /comics
27   # GET /comics.json
28   def index
29     @comics = Comic.list({}, params[:page].to_i, params[:page_size].to_i)
30     respond_to do |format|
31       format.html # index.html.erb
32       format.json { render json: @comics.to_json(Comic.json_opt) }
33     end
34   end
35
36   # GET /comics/1
37   # GET /comics/1.json
38   def show
39     @comic = Comic.find(params[:id])
40
41     respond_to do |format|
42 #      format.html # show.html.erb
43       format.json { render json: @comic }
44     end
45   end
46
47   def play
48     @comic = Comic.find(params[:id], include: [:author, {:panels => [:panel_pictures => :resource_picture, :balloons => :speaches]}], order: 'panels.t')
49
50     respond_to do |format|
51       format.html # index.html.erb
52       format.json {
53         render :json => @comic.to_json(
54           :include => [:author, {
55             :panels => {:include => {
56               :panel_pictures => {:include => :resource_picture}, 
57               :balloons => {:include => :speaches}
58             }}
59           }]
60         )
61       }
62       format.jsonp {
63         render :json => "callback(" + @comic.to_json(
64           :include => {
65             :panels => {:include => {
66               :panel_pictures => {:include => :resource_picture}, 
67               :balloons => {:include => :speaches}
68             }}
69           }
70         ) + ");"
71       }
72     end
73   end
74   
75   def list
76     @comics = Comic.all
77
78     respond_to do |format|
79       format.html { render layout: 'system' }# index.html.erb
80       format.json { render json: @comics }
81     end
82   end
83
84   def browse
85     @comic = Comic.find(params[:id])
86
87     respond_to do |format|
88       format.html { render layout: 'system' } # show.html.erb
89       format.json { render json: @comic }
90     end
91   end
92   
93   # GET /comics/new
94   # GET /comics/new.json
95   def new
96     @comic = Comic.new
97     @comic.supply_default
98     respond_to do |format|
99       format.html # new.html.erb
100       format.json { render json: @comic }
101     end
102   end
103
104   # GET /comics/1/edit
105   def edit
106     @comic = Comic.find(params[:id])
107     respond_to do |format|
108       if @comic.own? @author
109         format.html 
110         format.json { render json: @comic }
111       else
112         format.html { render :status => :forbidden,  :file => '/403.html'}
113         format.json { head :forbidden }
114       end
115     end
116   end
117
118   # POST /comics
119   # POST /comics.json
120   def create
121     params[:comic].merge! author_id: @author.id
122     @comic = Comic.new params[:comic]
123     @comic.supply_default 
124
125     respond_to do |format|
126       if @comic.save
127         format.html { redirect_to @comic, notice: 'Comic was successfully created.' }
128         format.json { render json: @comic, status: :created, location: @comic }
129       else
130         format.html { render action: "new" }
131         format.json { render json: @comic.errors, status: :unprocessable_entity }
132       end
133     end
134   end
135
136   # PUT /comics/1
137   # PUT /comics/1.json
138   def update
139     params[:comic].merge! author_id: @author.id
140     @comic = Comic.find(params[:id])
141     respond_to do |format|
142       if @comic.own? @author
143         if @comic.update_attributes(params[:comic])
144           format.html { redirect_to @comic, notice: 'Comic was successfully updated.' }
145           format.json { head :ok }
146         else
147           format.html { render action: "edit" }
148           format.json { render json: @comic.errors, status: :unprocessable_entity }
149         end
150       else
151         format.html { render :status => :forbidden,  :file => '/403.html'}
152         format.json { head :forbidden }
153       end
154     end
155   end
156
157   # DELETE /comics/1
158   # DELETE /comics/1.json
159   def destroy
160     @comic = Comic.find(params[:id])
161     if @comic.own? @author
162       @comic.destroy
163       respond_to do |format|
164         format.html { redirect_to comics_url }
165         format.json { head :ok }
166       end
167     else
168       format.html { render action: "edit" }
169       format.json { render json: @comic.errors, status: :unprocessable_entity }
170     end
171   end
172 end