OSDN Git Service

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