OSDN Git Service

t#29312:any update
[pettanr/pettanr.git] / app / controllers / comics_controller.rb
1 class ComicsController < ApplicationController
2   layout 'test' if Pettanr::TestLayout
3   if Const.run_mode == 0
4     before_filter :authenticate_user!, :only => [:new, :create, :edit, :update, :destroy]
5   else
6     before_filter :authenticate_user!, :only => [:top, :index, :show, :play, :new, :create, :edit, :update, :destroy]
7   end
8   before_filter :authenticate_admin!, :only => [:list, :browse]
9
10   def top
11     @new_comics = Comic.find(:all, 
12       :include => :author, :conditions => ['visible > 0'], :order => 'updated_at desc', :limit => 5
13     )
14     @new_pictures = OriginalPicture.list @artist.id
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     @page = Comic.page params[:page]
25     @page_size = Comic.page_size params[:page_size]
26     @comics = Comic.list({}, @page, @page_size)
27     respond_to do |format|
28       format.html # index.html.erb
29       format.json { render json: @comics.to_json(Comic.list_json_opt) }
30     end
31   end
32
33   # GET /comics/1
34   # GET /comics/1.json
35   def show
36     @comic = Comic.show(params[:id], @author)
37
38     respond_to do |format|
39       format.html # show.html.erb
40       format.json { render json: @comic.to_json(Comic.show_json_include_opt) }
41     end
42   end
43
44   def count
45     @comic = {:count => Comic.visible_count}
46     respond_to do |format|
47       format.json { render json: @comic.to_json }
48     end
49   end
50   
51   def list
52     @comics = Comic.all
53
54     respond_to do |format|
55       format.html { render layout: 'system' }# index.html.erb
56       format.json { render json: @comics }
57     end
58   end
59
60   def browse
61     @comic = Comic.find(params[:id])
62
63     respond_to do |format|
64       format.html { render layout: 'system' } # show.html.erb
65       format.json { render json: @comic }
66     end
67   end
68   
69   # GET /comics/new
70   # GET /comics/new.js
71   def new
72     @comic = Comic.new
73     @comic.supply_default
74     respond_to do |format|
75       format.html # new.html.erb
76       format.js
77     end
78   end
79
80   # GET /comics/1/edit
81   # GET /comics/1.js/edit
82   def edit
83     @comic = Comic.edit(params[:id], @author)
84     @comic.supply_default
85     respond_to do |format|
86       format.html 
87       format.js
88     end
89   end
90
91   # POST /comics
92   # POST /comics.json
93   def create
94     params[:comic].merge! author_id: @author.id
95     @comic = Comic.new
96     @comic.supply_default 
97     @comic.attributes = params[:comic]
98     @comic.overwrite @author
99
100     respond_to do |format|
101       if @comic.save
102         format.html { redirect_to @comic, notice: 'Comic was successfully created.' }
103         format.json { render json: Comic.show(@comic.id, @author).to_json(Comic.show_json_include_opt), status: :created, location: @comic }
104       else
105         format.html { render action: "new" }
106         format.json { render json: @comic.errors, status: :unprocessable_entity }
107       end
108     end
109   end
110
111   # PUT /comics/1
112   # PUT /comics/1.json
113   def update
114     @comic = Comic.edit(params[:id], @author)
115     @comic.attributes = params[:comic]
116     @comic.overwrite @author
117     respond_to do |format|
118       if @comic.save
119         format.html { redirect_to @comic, notice: 'Comic was successfully updated.' }
120         format.json { head :ok }
121       else
122         format.html { render action: "edit" }
123         format.json { render json: @comic.errors, status: :unprocessable_entity }
124       end
125     end
126   end
127
128   # DELETE /comics/1
129   # DELETE /comics/1.json
130   def destroy
131     @comic = Comic.edit(params[:id], @author)
132     @comic.destroy
133     respond_to do |format|
134       format.html { redirect_to comics_url }
135       format.json { head :ok }
136     end
137   end
138 end