OSDN Git Service

comic test error
[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])
46
47     respond_to do |format|
48       raise ActiveRecord::Forbidden unless @comic.visible?(@author)
49       format.html # show.html.erb
50       format.json { render json: @comic.to_json(Comic.show_json_include_opt) }
51     end
52   end
53
54   def count
55     @comic = {:count => Comic.visible_count}
56     respond_to do |format|
57       format.json { render json: @comic.to_json }
58     end
59   end
60   
61   def play
62     @comic = Comic.play(params[:id])
63     respond_to do |format|
64       format.html # index.html.erb
65       format.json {
66         render :json => @comic.to_json_play
67       }
68       format.jsonp {
69         render :json => "callback(" + @comic.to_json_play + ");"
70       }
71     end
72   end
73   
74   def list
75     @comics = Comic.all
76
77     respond_to do |format|
78       format.html { render layout: 'system' }# index.html.erb
79       format.json { render json: @comics }
80     end
81   end
82
83   def browse
84     @comic = Comic.find(params[:id])
85
86     respond_to do |format|
87       format.html { render layout: 'system' } # show.html.erb
88       format.json { render json: @comic }
89     end
90   end
91   
92   # GET /comics/new
93   # GET /comics/new.js
94   def new
95     @comic = Comic.new
96     @comic.supply_default
97     respond_to do |format|
98       format.html # new.html.erb
99       format.js
100     end
101   end
102
103   # GET /comics/1/edit
104   # GET /comics/1.js/edit
105   def edit
106     @comic = Comic.find(params[:id])
107     @comic.supply_default
108     respond_to do |format|
109       raise ActiveRecord::Forbidden unless @comic.own?(@author)
110       format.html 
111       format.js
112     end
113   end
114
115   # POST /comics
116   # POST /comics.json
117   def create
118     params[:comic].merge! author_id: @author.id
119     @comic = Comic.new params[:comic]
120     @comic.supply_default 
121
122     respond_to do |format|
123       if @comic.save
124         format.html { redirect_to @comic, notice: 'Comic was successfully created.' }
125         format.json { render json: Comic.show(@comic.id).to_json(Comic.show_json_include_opt), status: :created, location: @comic }
126       else
127         format.html { render action: "new" }
128         format.json { render json: @comic.errors, status: :unprocessable_entity }
129       end
130     end
131   end
132
133   # PUT /comics/1
134   # PUT /comics/1.json
135   def update
136     params[:comic].merge! author_id: @author.id
137     @comic = Comic.find(params[:id])
138     respond_to do |format|
139       raise ActiveRecord::Forbidden unless @comic.own?(@author)
140       if @comic.update_attributes(params[:comic])
141         format.html { redirect_to @comic, notice: 'Comic was successfully updated.' }
142         format.json { head :ok }
143       else
144         format.html { render action: "edit" }
145         format.json { render json: @comic.errors, status: :unprocessable_entity }
146       end
147     end
148   end
149
150   # DELETE /comics/1
151   # DELETE /comics/1.json
152   def destroy
153     @comic = Comic.find(params[:id])
154     if @comic.own? @author
155       @comic.destroy
156       respond_to do |format|
157         format.html { redirect_to comics_url }
158         format.json { head :ok }
159       end
160     else
161       format.html { render action: "edit" }
162       format.json { render json: @comic.errors, status: :unprocessable_entity }
163     end
164   end
165 end