OSDN Git Service

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