OSDN Git Service

adad9faa510230ac24b1c05af257c2652cb92d62
[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     @comic = Comic.play(params[:id])
62     respond_to do |format|
63       format.html # index.html.erb
64       format.json {
65         render :json => @comic.to_json_play
66       }
67       format.jsonp {
68         render :json => "callback(" + @comic.to_json_play + ");"
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.js
93   def new
94     @comic = Comic.new
95     @comic.supply_default
96     respond_to do |format|
97       format.html # new.html.erb
98       format.js
99     end
100   end
101
102   # GET /comics/1/edit
103   # GET /comics/1.js/edit
104   def edit
105     @comic = Comic.show(params[:id], @author)
106     @comic.supply_default
107     respond_to do |format|
108       format.html 
109       format.js
110     end
111   end
112
113   # POST /comics
114   # POST /comics.json
115   def create
116     params[:comic].merge! author_id: @author.id
117     @comic = Comic.new params[:comic]
118     @comic.supply_default 
119
120     respond_to do |format|
121       if @comic.save
122         format.html { redirect_to @comic, notice: 'Comic was successfully created.' }
123         format.json { render json: Comic.show(@comic.id, @author).to_json(Comic.show_json_include_opt), status: :created, location: @comic }
124       else
125         format.html { render action: "new" }
126         format.json { render json: @comic.errors, status: :unprocessable_entity }
127       end
128     end
129   end
130
131   # PUT /comics/1
132   # PUT /comics/1.json
133   def update
134     params[:comic].merge! author_id: @author.id
135     @comic = Comic.show(params[:id], @author)
136     respond_to do |format|
137       raise ActiveRecord::Forbidden unless @comic.own?(@author)
138       if @comic.update_attributes(params[:comic])
139         format.html { redirect_to @comic, notice: 'Comic was successfully updated.' }
140         format.json { head :ok }
141       else
142         format.html { render action: "edit" }
143         format.json { render json: @comic.errors, status: :unprocessable_entity }
144       end
145     end
146   end
147
148   # DELETE /comics/1
149   # DELETE /comics/1.json
150   def destroy
151     @comic = Comic.find(params[:id])
152     if @comic.own? @author
153       @comic.destroy
154       respond_to do |format|
155         format.html { redirect_to comics_url }
156         format.json { head :ok }
157       end
158     else
159       format.html { render action: "edit" }
160       format.json { render json: @comic.errors, status: :unprocessable_entity }
161     end
162   end
163 end