OSDN Git Service

1b6ccb35a2c959b1cb0dc4427c17e63d816efac9
[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_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       format.json { render json: @comic.to_json(Comic.show_json_opt) }
78     end
79   end
80
81   # GET /comics/1/edit
82   # GET /comics/1.js/edit
83   def edit
84     @comic = Comic.edit(params[:id], @author)
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     @comic = Comic.new
95     @comic.supply_default 
96     @comic.attributes = params[:comic]
97     @comic.overwrite @author
98
99     respond_to do |format|
100       if @comic.save
101         format.html { redirect_to @comic, notice: 'Comic was successfully created.' }
102         format.json { render json: @comic.to_json(Comic.show_json_opt), status: :created, location: @comic }
103       else
104         format.html { render action: "new" }
105         format.json { render json: @comic.errors, status: :unprocessable_entity }
106       end
107     end
108   end
109
110   # PUT /comics/1
111   # PUT /comics/1.json
112   def update
113     @comic = Comic.edit(params[:id], @author)
114     @comic.attributes = params[:comic]
115     @comic.overwrite @author
116     respond_to do |format|
117       if @comic.save
118         format.html { redirect_to @comic, notice: 'Comic was successfully updated.' }
119         format.json { head :ok }
120       else
121         format.html { render action: "edit" }
122         format.json { render json: @comic.errors, status: :unprocessable_entity }
123       end
124     end
125   end
126
127   # DELETE /comics/1
128   # DELETE /comics/1.json
129   def destroy
130     @comic = Comic.edit(params[:id], @author)
131     @comic.destroy
132     respond_to do |format|
133       format.html { redirect_to comics_url }
134       format.json { head :ok }
135     end
136   end
137 end