OSDN Git Service

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