OSDN Git Service

merge v04
[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   private
11   
12   def treat_param comic
13     comic.author_id = @author.id
14   end
15   
16   public
17   
18   def top
19     @new_comics = Comic.find(:all, 
20       :include => :author, :conditions => ['visible > 0'], :order => 'updated_at desc', :limit => 5
21     )
22     @new_pictures = OriginalPicture.list @artist.id
23
24     respond_to do |format|
25       format.html # index.html.erb
26     end
27   end
28   
29   # GET /comics
30   # GET /comics.json
31   def index
32     @page = Comic.page params[:page]
33     @page_size = Comic.page_size params[:page_size]
34     @comics = Comic.list({}, @page, @page_size)
35     respond_to do |format|
36       format.html # index.html.erb
37       format.json { render json: @comics.to_json(Comic.list_json_opt) }
38     end
39   end
40
41   # GET /comics/1
42   # GET /comics/1.json
43   def show
44     @comic = Comic.show(params[:id], @author)
45
46     respond_to do |format|
47       format.html # show.html.erb
48       format.json { render json: @comic.to_json(Comic.show_json_include_opt) }
49     end
50   end
51
52   def count
53     @comic = {:count => Comic.visible_count}
54     respond_to do |format|
55       format.json { render json: @comic.to_json }
56     end
57   end
58   
59   def list
60     @comics = Comic.all
61
62     respond_to do |format|
63       format.html { render layout: 'system' }# index.html.erb
64       format.json { render json: @comics }
65     end
66   end
67
68   def browse
69     @comic = Comic.find(params[:id])
70
71     respond_to do |format|
72       format.html { render layout: 'system' } # show.html.erb
73       format.json { render json: @comic }
74     end
75   end
76   
77   # GET /comics/new
78   # GET /comics/new.js
79   def new
80     @comic = Comic.new
81     @comic.supply_default
82     respond_to do |format|
83       format.html # new.html.erb
84       format.js
85     end
86   end
87
88   # GET /comics/1/edit
89   # GET /comics/1.js/edit
90   def edit
91     @comic = Comic.show(params[:id], @author)
92     @comic.supply_default
93     respond_to do |format|
94       format.html 
95       format.js
96     end
97   end
98
99   # POST /comics
100   # POST /comics.json
101   def create
102     params[:comic].merge! author_id: @author.id
103     @comic = Comic.new params[:comic]
104     @comic.supply_default 
105
106     respond_to do |format|
107       if @comic.save
108         format.html { redirect_to @comic, notice: 'Comic was successfully created.' }
109         format.json { render json: Comic.show(@comic.id, @author).to_json(Comic.show_json_include_opt), status: :created, location: @comic }
110       else
111         format.html { render action: "new" }
112         format.json { render json: @comic.errors, status: :unprocessable_entity }
113       end
114     end
115   end
116
117   # PUT /comics/1
118   # PUT /comics/1.json
119   def update
120     params[:comic].merge! author_id: @author.id
121     @comic = Comic.show(params[:id], @author)
122     respond_to do |format|
123       if @comic.update_attributes(params[:comic])
124         format.html { redirect_to @comic, notice: 'Comic was successfully updated.' }
125         format.json { head :ok }
126       else
127         format.html { render action: "edit" }
128         format.json { render json: @comic.errors, status: :unprocessable_entity }
129       end
130     end
131   end
132
133   # DELETE /comics/1
134   # DELETE /comics/1.json
135   def destroy
136     @comic = Comic.find(params[:id])
137     if @comic.own? @author
138       @comic.destroy
139       respond_to do |format|
140         format.html { redirect_to comics_url }
141         format.json { head :ok }
142       end
143     else
144       format.html { render action: "edit" }
145       format.json { render json: @comic.errors, status: :unprocessable_entity }
146     end
147   end
148 end